blob: 47f3c273245e47b7c50a2e76a8b22de312190c5e [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
1672 rcu_read_lock();
1673 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001674 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001675 rcu_read_unlock();
1676 return;
1677 }
1678 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001679 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001680}
1681
Trond Myklebust6ee41262007-07-08 14:11:36 -04001682static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001683{
1684 struct nfs4_state *state = opendata->state;
1685 struct nfs_inode *nfsi = NFS_I(state->inode);
1686 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001687 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001688 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001689 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001690 nfs4_stateid stateid;
1691 int ret = -EAGAIN;
1692
Trond Myklebustaac00a82007-07-05 19:02:21 -04001693 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001694 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001695 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001696 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001697 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001698 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001699 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001700 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001701 rcu_read_lock();
1702 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001703 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001704 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001705 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001706 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001707 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001708 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001709 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001710 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001711 if (!opendata->is_recover) {
1712 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1713 if (ret != 0)
1714 goto out;
1715 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001716 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001717
1718 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001719 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001720 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001721 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001722out:
1723 return ERR_PTR(ret);
1724out_return_state:
1725 atomic_inc(&state->count);
1726 return state;
1727}
1728
Andy Adamsone23008e2012-10-02 21:07:32 -04001729static void
1730nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1731{
1732 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1733 struct nfs_delegation *delegation;
1734 int delegation_flags = 0;
1735
1736 rcu_read_lock();
1737 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1738 if (delegation)
1739 delegation_flags = delegation->flags;
1740 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001741 switch (data->o_arg.claim) {
1742 default:
1743 break;
1744 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1745 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001746 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1747 "returning a delegation for "
1748 "OPEN(CLAIM_DELEGATE_CUR)\n",
1749 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001750 return;
1751 }
1752 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001753 nfs_inode_set_delegation(state->inode,
1754 data->owner->so_cred,
1755 &data->o_res);
1756 else
1757 nfs_inode_reclaim_delegation(state->inode,
1758 data->owner->so_cred,
1759 &data->o_res);
1760}
1761
1762/*
1763 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1764 * and update the nfs4_state.
1765 */
1766static struct nfs4_state *
1767_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1768{
1769 struct inode *inode = data->state->inode;
1770 struct nfs4_state *state = data->state;
1771 int ret;
1772
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001773 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001774 if (data->rpc_status)
1775 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001776 /* cached opens have already been processed */
1777 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001778 }
1779
Andy Adamsone23008e2012-10-02 21:07:32 -04001780 ret = nfs_refresh_inode(inode, &data->f_attr);
1781 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001782 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001783
1784 if (data->o_res.delegation_type != 0)
1785 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001786update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001787 update_open_stateid(state, &data->o_res.stateid, NULL,
1788 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001789 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001790
1791 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001792}
1793
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001794static struct inode *
1795nfs4_opendata_get_inode(struct nfs4_opendata *data)
1796{
1797 struct inode *inode;
1798
1799 switch (data->o_arg.claim) {
1800 case NFS4_OPEN_CLAIM_NULL:
1801 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1802 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1803 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1804 return ERR_PTR(-EAGAIN);
1805 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1806 &data->f_attr, data->f_label);
1807 break;
1808 default:
1809 inode = d_inode(data->dentry);
1810 ihold(inode);
1811 nfs_refresh_inode(inode, &data->f_attr);
1812 }
1813 return inode;
1814}
1815
Andy Adamsone23008e2012-10-02 21:07:32 -04001816static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001817nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1818{
1819 struct nfs4_state *state;
1820 struct inode *inode;
1821
1822 inode = nfs4_opendata_get_inode(data);
1823 if (IS_ERR(inode))
1824 return ERR_CAST(inode);
1825 if (data->state != NULL && data->state->inode == inode) {
1826 state = data->state;
1827 atomic_inc(&state->count);
1828 } else
1829 state = nfs4_get_open_state(inode, data->owner);
1830 iput(inode);
1831 if (state == NULL)
1832 state = ERR_PTR(-ENOMEM);
1833 return state;
1834}
1835
Andy Adamsone23008e2012-10-02 21:07:32 -04001836static struct nfs4_state *
1837_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001838{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001839 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001840
Trond Myklebustaac00a82007-07-05 19:02:21 -04001841 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001842 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001843 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001844 goto out;
1845 }
1846
Trond Myklebust75e8c482017-08-08 10:38:07 -04001847 state = nfs4_opendata_find_nfs4_state(data);
1848 if (IS_ERR(state))
1849 goto out;
1850
Andy Adamsone23008e2012-10-02 21:07:32 -04001851 if (data->o_res.delegation_type != 0)
1852 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001853 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001854 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001855out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001856 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001857 return state;
1858}
1859
Andy Adamsone23008e2012-10-02 21:07:32 -04001860static struct nfs4_state *
1861nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1862{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001863 struct nfs4_state *ret;
1864
Andy Adamsone23008e2012-10-02 21:07:32 -04001865 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001866 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1867 else
1868 ret = _nfs4_opendata_to_nfs4_state(data);
1869 nfs4_sequence_free_slot(&data->o_res.seq_res);
1870 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001871}
1872
Trond Myklebust864472e2006-01-03 09:55:15 +01001873static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1874{
1875 struct nfs_inode *nfsi = NFS_I(state->inode);
1876 struct nfs_open_context *ctx;
1877
1878 spin_lock(&state->inode->i_lock);
1879 list_for_each_entry(ctx, &nfsi->open_files, list) {
1880 if (ctx->state != state)
1881 continue;
1882 get_nfs_open_context(ctx);
1883 spin_unlock(&state->inode->i_lock);
1884 return ctx;
1885 }
1886 spin_unlock(&state->inode->i_lock);
1887 return ERR_PTR(-ENOENT);
1888}
1889
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001890static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1891 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001892{
1893 struct nfs4_opendata *opendata;
1894
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001895 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001896 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001897 if (opendata == NULL)
1898 return ERR_PTR(-ENOMEM);
1899 opendata->state = state;
1900 atomic_inc(&state->count);
1901 return opendata;
1902}
1903
Trond Myklebust24311f82015-09-20 10:50:17 -04001904static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1905 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001906{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001907 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001908 int ret;
1909
Trond Myklebust24311f82015-09-20 10:50:17 -04001910 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001911 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001912 opendata->o_arg.open_flags = 0;
1913 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001914 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1915 NFS_SB(opendata->dentry->d_sb),
1916 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001917 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1918 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1919 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001920 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001921 if (ret != 0)
1922 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001923 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001924 if (IS_ERR(newstate))
1925 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001926 if (newstate != opendata->state)
1927 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001928 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001929 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001930}
1931
1932static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1933{
Trond Myklebust864472e2006-01-03 09:55:15 +01001934 int ret;
1935
Trond Myklebust4f14c192014-02-12 19:15:06 -05001936 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1937 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1938 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1939 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001940 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001941 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001942 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001943 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001944 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1945 if (ret != 0)
1946 return ret;
1947 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1948 if (ret != 0)
1949 return ret;
1950 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1951 if (ret != 0)
1952 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001953 /*
1954 * We may have performed cached opens for all three recoveries.
1955 * Check if we need to update the current stateid.
1956 */
1957 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001958 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001959 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001960 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001961 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001962 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001963 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001964 return 0;
1965}
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967/*
1968 * OPEN_RECLAIM:
1969 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001971static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001973 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001974 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001975 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int status;
1977
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001978 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1979 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001980 if (IS_ERR(opendata))
1981 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001982 rcu_read_lock();
1983 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001984 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001985 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001986 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001987 opendata->o_arg.u.delegation_type = delegation_type;
1988 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001989 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return status;
1991}
1992
Trond Myklebust539cd032007-06-05 11:46:42 -04001993static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct nfs_server *server = NFS_SERVER(state->inode);
1996 struct nfs4_exception exception = { };
1997 int err;
1998 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001999 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002000 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002001 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2002 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002003 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002004 break;
2005 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 } while (exception.retry);
2007 return err;
2008}
2009
Trond Myklebust864472e2006-01-03 09:55:15 +01002010static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2011{
2012 struct nfs_open_context *ctx;
2013 int ret;
2014
2015 ctx = nfs4_state_find_open_context(state);
2016 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002017 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002018 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002019 put_nfs_open_context(ctx);
2020 return ret;
2021}
2022
NeilBrowndce26302017-12-13 09:57:09 +11002023static 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 -07002024{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002025 switch (err) {
2026 default:
2027 printk(KERN_ERR "NFS: %s: unhandled error "
2028 "%d.\n", __func__, err);
2029 case 0:
2030 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002031 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002032 case -ESTALE:
2033 break;
2034 case -NFS4ERR_BADSESSION:
2035 case -NFS4ERR_BADSLOT:
2036 case -NFS4ERR_BAD_HIGH_SLOT:
2037 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2038 case -NFS4ERR_DEADSESSION:
2039 set_bit(NFS_DELEGATED_STATE, &state->flags);
2040 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2041 return -EAGAIN;
2042 case -NFS4ERR_STALE_CLIENTID:
2043 case -NFS4ERR_STALE_STATEID:
2044 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002045 /* Don't recall a delegation if it was lost */
2046 nfs4_schedule_lease_recovery(server->nfs_client);
2047 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002048 case -NFS4ERR_MOVED:
2049 nfs4_schedule_migration_recovery(server);
2050 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002051 case -NFS4ERR_LEASE_MOVED:
2052 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2053 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002054 case -NFS4ERR_DELEG_REVOKED:
2055 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002056 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002057 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002058 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002059 nfs_inode_find_state_and_recover(state->inode,
2060 stateid);
2061 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002062 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002063 case -NFS4ERR_DELAY:
2064 case -NFS4ERR_GRACE:
2065 set_bit(NFS_DELEGATED_STATE, &state->flags);
2066 ssleep(1);
2067 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002068 case -ENOMEM:
2069 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002070 if (fl) {
2071 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2072 if (lsp)
2073 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2074 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002075 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return err;
2078}
2079
Trond Myklebust24311f82015-09-20 10:50:17 -04002080int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2081 struct nfs4_state *state, const nfs4_stateid *stateid,
2082 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002083{
2084 struct nfs_server *server = NFS_SERVER(state->inode);
2085 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002086 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002087
2088 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2089 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2090 if (IS_ERR(opendata))
2091 return PTR_ERR(opendata);
2092 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002093 write_seqlock(&state->seqlock);
2094 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2095 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002096 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2097 switch (type & (FMODE_READ|FMODE_WRITE)) {
2098 case FMODE_READ|FMODE_WRITE:
2099 case FMODE_WRITE:
2100 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2101 if (err)
2102 break;
2103 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2104 if (err)
2105 break;
2106 case FMODE_READ:
2107 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2108 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002109 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002110 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002111}
2112
Chuck Leverbe05c862013-08-09 12:49:47 -04002113static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2114{
2115 struct nfs4_opendata *data = calldata;
2116
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002117 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2118 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002119}
2120
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002121static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2122{
2123 struct nfs4_opendata *data = calldata;
2124
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002125 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002126
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002127 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002129 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002130 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002131 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002132 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002133 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002134}
2135
2136static void nfs4_open_confirm_release(void *calldata)
2137{
2138 struct nfs4_opendata *data = calldata;
2139 struct nfs4_state *state = NULL;
2140
2141 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002142 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002143 goto out_free;
2144 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002145 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002146 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002148 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002149 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002150out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002151 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002152}
2153
2154static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002155 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002156 .rpc_call_done = nfs4_open_confirm_done,
2157 .rpc_release = nfs4_open_confirm_release,
2158};
2159
2160/*
2161 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2162 */
2163static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2164{
David Howells2b0143b2015-03-17 22:25:59 +00002165 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002166 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002167 struct rpc_message msg = {
2168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2169 .rpc_argp = &data->c_arg,
2170 .rpc_resp = &data->c_res,
2171 .rpc_cred = data->owner->so_cred,
2172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 struct rpc_task_setup task_setup_data = {
2174 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .callback_ops = &nfs4_open_confirm_ops,
2177 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002178 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .flags = RPC_TASK_ASYNC,
2180 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 int status;
2182
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002183 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002185 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002186 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002187 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002188 if (data->is_recover)
2189 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002190 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002191 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002192 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002193 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002194 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002195 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002196 smp_wmb();
2197 } else
2198 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002199 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return status;
2201}
2202
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002203static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 struct nfs4_opendata *data = calldata;
2206 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002207 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002208 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002209
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002211 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002212 /*
2213 * Check if we still need to send an OPEN call, or if we can use
2214 * a delegation instead.
2215 */
2216 if (data->state != NULL) {
2217 struct nfs_delegation *delegation;
2218
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002219 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002220 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002221 rcu_read_lock();
2222 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002223 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002224 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002225 rcu_read_unlock();
2226 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002227 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002228 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002229 switch (claim) {
2230 default:
2231 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002232 case NFS4_OPEN_CLAIM_PREVIOUS:
2233 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2234 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002235 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002236 case NFS4_OPEN_CLAIM_FH:
2237 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002238 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002239 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002240 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002241 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002242 &data->o_res.seq_res,
2243 task) != 0)
2244 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002245
2246 /* Set the create mode (note dependency on the session type) */
2247 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2248 if (data->o_arg.open_flags & O_EXCL) {
2249 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2250 if (nfs4_has_persistent_session(clp))
2251 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2252 else if (clp->cl_mvops->minor_version > 0)
2253 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2254 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002255 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002256unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002257 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002258 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002259out_no_action:
2260 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002261out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002262 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002263}
2264
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002265static void nfs4_open_done(struct rpc_task *task, void *calldata)
2266{
2267 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002269 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002270
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002271 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002272 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002273
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002274 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002275 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2276 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002277 case S_IFREG:
2278 break;
2279 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002280 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002281 break;
2282 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002284 break;
2285 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002286 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002287 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002288 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002289 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002290 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2291 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002292 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002293 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002294}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002295
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002296static void nfs4_open_release(void *calldata)
2297{
2298 struct nfs4_opendata *data = calldata;
2299 struct nfs4_state *state = NULL;
2300
2301 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002302 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303 goto out_free;
2304 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002305 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002306 goto out_free;
2307 /* In case we need an open_confirm, no cleanup! */
2308 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2309 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002311 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002312 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002313out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002314 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315}
2316
2317static const struct rpc_call_ops nfs4_open_ops = {
2318 .rpc_call_prepare = nfs4_open_prepare,
2319 .rpc_call_done = nfs4_open_done,
2320 .rpc_release = nfs4_open_release,
2321};
2322
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002323static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002324{
David Howells2b0143b2015-03-17 22:25:59 +00002325 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002326 struct nfs_server *server = NFS_SERVER(dir);
2327 struct nfs_openargs *o_arg = &data->o_arg;
2328 struct nfs_openres *o_res = &data->o_res;
2329 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002330 struct rpc_message msg = {
2331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2332 .rpc_argp = o_arg,
2333 .rpc_resp = o_res,
2334 .rpc_cred = data->owner->so_cred,
2335 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002336 struct rpc_task_setup task_setup_data = {
2337 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002338 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002339 .callback_ops = &nfs4_open_ops,
2340 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002341 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002342 .flags = RPC_TASK_ASYNC,
2343 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002344 int status;
2345
Chuck Levera9c92d62013-08-09 12:48:18 -04002346 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002347 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002348 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002349 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002350 data->cancelled = false;
2351 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002352 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002353 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002354 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002355 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002356 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002357 if (IS_ERR(task))
2358 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002359 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002360 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002361 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002362 smp_wmb();
2363 } else
2364 status = data->rpc_status;
2365 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002366
2367 return status;
2368}
2369
2370static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2371{
David Howells2b0143b2015-03-17 22:25:59 +00002372 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002373 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002374 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002375
2376 status = nfs4_run_open_task(data, 1);
2377 if (status != 0 || !data->rpc_done)
2378 return status;
2379
Trond Myklebust6926afd12012-01-07 13:22:46 -05002380 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2381
Anna Schumakerd7e98252017-01-11 16:13:29 -05002382 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002383 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002384
2385 return status;
2386}
2387
Trond Myklebustf3792d62014-07-10 08:54:32 -04002388/*
2389 * Additional permission checks in order to distinguish between an
2390 * open for read, and an open for execute. This works around the
2391 * fact that NFSv4 OPEN treats read and execute permissions as being
2392 * the same.
2393 * Note that in the non-execute case, we want to turn off permission
2394 * checking if we just created a new file (POSIX open() semantics).
2395 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002396static int nfs4_opendata_access(struct rpc_cred *cred,
2397 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002398 struct nfs4_state *state, fmode_t fmode,
2399 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002400{
2401 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002402 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002403
2404 /* access call failed or for some reason the server doesn't
2405 * support any access modes -- defer access call until later */
2406 if (opendata->o_res.access_supported == 0)
2407 return 0;
2408
2409 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002410 /*
2411 * Use openflags to check for exec, because fmode won't
2412 * always have FMODE_EXEC set when file open for exec.
2413 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002414 if (openflags & __FMODE_EXEC) {
2415 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002416 if (S_ISDIR(state->inode->i_mode))
2417 mask = NFS4_ACCESS_LOOKUP;
2418 else
2419 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002420 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002421 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002422
2423 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2425 nfs_access_add_cache(state->inode, &cache);
2426
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002427 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2428 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002429 return 0;
2430
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002431 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002432}
2433
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002434/*
2435 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2436 */
2437static int _nfs4_proc_open(struct nfs4_opendata *data)
2438{
David Howells2b0143b2015-03-17 22:25:59 +00002439 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002440 struct nfs_server *server = NFS_SERVER(dir);
2441 struct nfs_openargs *o_arg = &data->o_arg;
2442 struct nfs_openres *o_res = &data->o_res;
2443 int status;
2444
2445 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002446 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002447 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002448 if (status != 0) {
2449 if (status == -NFS4ERR_BADNAME &&
2450 !(o_arg->open_flags & O_CREAT))
2451 return -ENOENT;
2452 return status;
2453 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002454
Trond Myklebust6926afd12012-01-07 13:22:46 -05002455 nfs_fattr_map_and_free_names(server, &data->f_attr);
2456
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002457 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002458 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002459 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002460 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002461 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002462 if (data->file_created ||
2463 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002464 update_changeattr(dir, &o_res->cinfo,
2465 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002466 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002467 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2468 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002470 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002474 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2475 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002476 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002477 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481/*
2482 * OPEN_EXPIRED:
2483 * reclaim state on the server after a network partition.
2484 * Assumes caller holds the appropriate lock
2485 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002486static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002488 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002489 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002491 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002492 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002493 if (IS_ERR(opendata))
2494 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002495 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002496 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002497 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002498 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002499 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002502static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002503{
Trond Myklebust539cd032007-06-05 11:46:42 -04002504 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002505 struct nfs4_exception exception = { };
2506 int err;
2507
2508 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002509 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002510 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002511 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2512 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002513 switch (err) {
2514 default:
2515 goto out;
2516 case -NFS4ERR_GRACE:
2517 case -NFS4ERR_DELAY:
2518 nfs4_handle_exception(server, err, &exception);
2519 err = 0;
2520 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002521 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002522out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002523 return err;
2524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002529 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Trond Myklebust864472e2006-01-03 09:55:15 +01002531 ctx = nfs4_state_find_open_context(state);
2532 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002533 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002534 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002535 put_nfs_open_context(ctx);
2536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Trond Myklebust41020b62016-09-22 13:38:58 -04002539static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2540 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002541{
Trond Myklebust41020b62016-09-22 13:38:58 -04002542 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002543 write_seqlock(&state->seqlock);
2544 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2545 write_sequnlock(&state->seqlock);
2546 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2547}
2548
2549static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2550{
2551 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002552 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002553}
2554
2555static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2556{
2557 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2558 nfs40_clear_delegation_stateid(state);
2559 return nfs4_open_expired(sp, state);
2560}
2561
Trond Myklebust45870d62016-09-22 13:38:59 -04002562static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2563 nfs4_stateid *stateid,
2564 struct rpc_cred *cred)
2565{
2566 return -NFS4ERR_BAD_STATEID;
2567}
2568
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002569#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002570static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2571 nfs4_stateid *stateid,
2572 struct rpc_cred *cred)
2573{
2574 int status;
2575
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002576 switch (stateid->type) {
2577 default:
2578 break;
2579 case NFS4_INVALID_STATEID_TYPE:
2580 case NFS4_SPECIAL_STATEID_TYPE:
2581 return -NFS4ERR_BAD_STATEID;
2582 case NFS4_REVOKED_STATEID_TYPE:
2583 goto out_free;
2584 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002585
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002586 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002587 switch (status) {
2588 case -NFS4ERR_EXPIRED:
2589 case -NFS4ERR_ADMIN_REVOKED:
2590 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002591 break;
2592 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002593 return status;
2594 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002595out_free:
2596 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002597 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002598 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002599}
2600
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002601static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002602{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002603 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002604 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002605 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002606 struct rpc_cred *cred;
2607 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002608
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002609 /* Get the delegation credential for use by test/free_stateid */
2610 rcu_read_lock();
2611 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002612 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002613 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002614 return;
2615 }
2616
2617 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002618 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2619 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2620 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002621 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002622 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002623 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002624 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002625
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002626 cred = get_rpccred(delegation->cred);
2627 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002628 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002629 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002630 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002631 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002632
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002633 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002634}
2635
2636/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002637 * nfs41_check_expired_locks - possibly free a lock stateid
2638 *
2639 * @state: NFSv4 state for an inode
2640 *
2641 * Returns NFS_OK if recovery for this stateid is now finished.
2642 * Otherwise a negative NFS4ERR value is returned.
2643 */
2644static int nfs41_check_expired_locks(struct nfs4_state *state)
2645{
2646 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002647 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002648 struct nfs_server *server = NFS_SERVER(state->inode);
2649
2650 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2651 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002652
2653 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002654 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2655 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2656 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2657
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002658 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002659 spin_unlock(&state->state_lock);
2660
2661 nfs4_put_lock_state(prev);
2662 prev = lsp;
2663
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002664 status = nfs41_test_and_free_expired_stateid(server,
2665 &lsp->ls_stateid,
2666 cred);
2667 trace_nfs4_test_lock_stateid(state, lsp, status);
2668 if (status == -NFS4ERR_EXPIRED ||
2669 status == -NFS4ERR_BAD_STATEID) {
2670 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002671 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002672 if (!recover_lost_locks)
2673 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2674 } else if (status != NFS_OK) {
2675 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002676 nfs4_put_lock_state(prev);
2677 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002678 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002679 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002680 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002681 }
2682 spin_unlock(&state->state_lock);
2683 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002684out:
2685 return ret;
2686}
2687
2688/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002689 * nfs41_check_open_stateid - possibly free an open stateid
2690 *
2691 * @state: NFSv4 state for an inode
2692 *
2693 * Returns NFS_OK if recovery for this stateid is now finished.
2694 * Otherwise a negative NFS4ERR value is returned.
2695 */
2696static int nfs41_check_open_stateid(struct nfs4_state *state)
2697{
2698 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002699 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002700 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002701 int status;
2702
Trond Myklebustb134fc42016-09-22 13:39:16 -04002703 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002704 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2705 if (nfs4_have_delegation(state->inode, state->state))
2706 return NFS_OK;
2707 return -NFS4ERR_OPENMODE;
2708 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002709 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002710 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002711 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002712 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002713 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002714 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2715 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2716 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002717 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002718 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002719 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002720 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002721 if (nfs_open_stateid_recover_openmode(state))
2722 return -NFS4ERR_OPENMODE;
2723 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002724}
2725
2726static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2727{
Chuck Levereb64cf92012-07-11 16:30:05 -04002728 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002729
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002730 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002731 status = nfs41_check_expired_locks(state);
2732 if (status != NFS_OK)
2733 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002734 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002735 if (status != NFS_OK)
2736 status = nfs4_open_expired(sp, state);
2737 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002738}
2739#endif
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002742 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2743 * fields corresponding to attributes that were used to store the verifier.
2744 * Make sure we clobber those fields in the later setattr call
2745 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002746static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2747 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002748{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002749 const u32 *attrset = opendata->o_res.attrset;
2750
2751 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002752 !(sattr->ia_valid & ATTR_ATIME_SET))
2753 sattr->ia_valid |= ATTR_ATIME;
2754
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002755 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002756 !(sattr->ia_valid & ATTR_MTIME_SET))
2757 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002758
2759 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002760 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002761 (attrset[1] & FATTR4_WORD1_MODE ||
2762 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002763 sattr->ia_valid &= ~ATTR_MODE;
2764
2765 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2766 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002767}
2768
Trond Myklebustc21443c2013-02-07 14:26:21 -05002769static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2770 fmode_t fmode,
2771 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002772 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002773{
2774 struct nfs4_state_owner *sp = opendata->owner;
2775 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002776 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002777 struct nfs4_state *state;
2778 unsigned int seq;
2779 int ret;
2780
2781 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2782
2783 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002784 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002785 goto out;
2786
2787 state = nfs4_opendata_to_nfs4_state(opendata);
2788 ret = PTR_ERR(state);
2789 if (IS_ERR(state))
2790 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002791 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002792 if (server->caps & NFS_CAP_POSIX_LOCK)
2793 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002794 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2795 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002796
Trond Myklebust275bb302013-05-29 13:11:28 -04002797 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002798 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002799 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002800 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002801 alias = d_exact_alias(dentry, state->inode);
2802 if (!alias)
2803 alias = d_splice_alias(igrab(state->inode), dentry);
2804 /* d_splice_alias() can't fail here - it's a non-directory */
2805 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002806 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002807 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002808 }
2809 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002810 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002811 }
2812
Trond Myklebustc21443c2013-02-07 14:26:21 -05002813 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2814 if (ret != 0)
2815 goto out;
2816
David Howells2b0143b2015-03-17 22:25:59 +00002817 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002818 nfs_inode_attach_open_context(ctx);
2819 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2820 nfs4_schedule_stateid_recovery(server, state);
2821 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002822out:
2823 return ret;
2824}
2825
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002826/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002827 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 */
Andy Adamson82be4172012-05-23 05:02:35 -04002829static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002830 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002831 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002832 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002833 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
2835 struct nfs4_state_owner *sp;
2836 struct nfs4_state *state = NULL;
2837 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002838 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002839 struct dentry *dentry = ctx->dentry;
2840 struct rpc_cred *cred = ctx->cred;
2841 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2842 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002843 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002844 struct iattr *sattr = c->sattr;
2845 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002846 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002847 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002851 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2852 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2854 goto out_err;
2855 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002856 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002857 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002858 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002859 if (d_really_is_positive(dentry))
2860 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002861 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002862 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002863 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002864 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2865 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002866 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002867 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
David Quigley14c43f72013-05-22 12:50:43 -04002869 if (label) {
2870 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2871 if (IS_ERR(olabel)) {
2872 status = PTR_ERR(olabel);
2873 goto err_opendata_put;
2874 }
2875 }
2876
Trond Myklebuste911b812014-03-26 13:24:37 -07002877 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2878 if (!opendata->f_attr.mdsthreshold) {
2879 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2880 if (!opendata->f_attr.mdsthreshold)
2881 goto err_free_label;
2882 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002883 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002884 }
David Howells2b0143b2015-03-17 22:25:59 +00002885 if (d_really_is_positive(dentry))
2886 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002887
Trond Myklebust3efb9722013-05-29 13:17:04 -04002888 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002889 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002890 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002891 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002892
NeilBrownefcbc042015-07-30 13:00:56 +10002893 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002894 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002895 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002896 /*
2897 * send create attributes which was not set by open
2898 * with an extra setattr.
2899 */
2900 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2901 nfs_fattr_init(opendata->o_res.f_attr);
2902 status = nfs4_do_setattr(state->inode, cred,
2903 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002904 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002905 if (status == 0) {
2906 nfs_setattr_update_inode(state->inode, sattr,
2907 opendata->o_res.f_attr);
2908 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2909 }
David Quigley1775fd32013-05-22 12:50:42 -04002910 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002911 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002912 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002913 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002914
Trond Myklebuste911b812014-03-26 13:24:37 -07002915 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002916 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002917 opendata->f_attr.mdsthreshold = NULL;
2918 }
Andy Adamson82be4172012-05-23 05:02:35 -04002919
David Quigley14c43f72013-05-22 12:50:43 -04002920 nfs4_label_free(olabel);
2921
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002922 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002925err_free_label:
2926 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002927err_opendata_put:
2928 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002929err_put_state_owner:
2930 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return status;
2933}
2934
2935
Andy Adamson82be4172012-05-23 05:02:35 -04002936static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002937 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002938 int flags,
2939 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002940 struct nfs4_label *label,
2941 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002943 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 struct nfs4_exception exception = { };
2945 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002946 struct nfs4_open_createattrs c = {
2947 .label = label,
2948 .sattr = sattr,
2949 .verf = {
2950 [0] = (__u32)jiffies,
2951 [1] = (__u32)current->pid,
2952 },
2953 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 int status;
2955
2956 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002957 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002958 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002959 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (status == 0)
2961 break;
2962 /* NOTE: BAD_SEQID means the server and client disagree about the
2963 * book-keeping w.r.t. state-changing operations
2964 * (OPEN/CLOSE/LOCK/LOCKU...)
2965 * It is actually a sign of a bug on the client or on the server.
2966 *
2967 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002968 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 * have unhashed the old state_owner for us, and that we can
2970 * therefore safely retry using a new one. We should still warn
2971 * the user though...
2972 */
2973 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002974 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002975 " returned a bad sequence-id error!\n",
2976 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 exception.retry = 1;
2978 continue;
2979 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002980 /*
2981 * BAD_STATEID on OPEN means that the server cancelled our
2982 * state before it received the OPEN_CONFIRM.
2983 * Recover by retrying the request as per the discussion
2984 * on Page 181 of RFC3530.
2985 */
2986 if (status == -NFS4ERR_BAD_STATEID) {
2987 exception.retry = 1;
2988 continue;
2989 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002990 if (status == -EAGAIN) {
2991 /* We must have found a delegation */
2992 exception.retry = 1;
2993 continue;
2994 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002995 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2996 continue;
2997 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 status, &exception));
2999 } while (exception.retry);
3000 return res;
3001}
3002
Trond Myklebust8487c472016-06-26 08:44:35 -04003003static int _nfs4_do_setattr(struct inode *inode,
3004 struct nfs_setattrargs *arg,
3005 struct nfs_setattrres *res,
3006 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003007 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003008{
3009 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003010 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3012 .rpc_argp = arg,
3013 .rpc_resp = res,
3014 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003015 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003016 struct rpc_cred *delegation_cred = NULL;
3017 unsigned long timestamp = jiffies;
3018 fmode_t fmode;
3019 bool truncate;
3020 int status;
3021
3022 nfs_fattr_init(res->fattr);
3023
3024 /* Servers should only apply open mode checks for file size changes */
3025 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3026 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3027
3028 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3029 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003030 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003031 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003032 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003033 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003034 l_ctx = nfs_get_lock_context(ctx);
3035 if (IS_ERR(l_ctx))
3036 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003037 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3038 &arg->stateid, &delegation_cred);
3039 nfs_put_lock_context(l_ctx);
3040 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003041 return -EBADF;
3042 } else
3043 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3044 if (delegation_cred)
3045 msg.rpc_cred = delegation_cred;
3046
3047 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3048
3049 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003050 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003051 renew_lease(server, timestamp);
3052 trace_nfs4_setattr(inode, &arg->stateid, status);
3053 return status;
3054}
3055
3056static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3057 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003058 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003059 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003061 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003062 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003063 struct nfs_setattrargs arg = {
3064 .fh = NFS_FH(inode),
3065 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 .server = server,
3067 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003068 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003069 };
3070 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003072 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003074 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003075 struct nfs4_exception exception = {
3076 .state = state,
3077 .inode = inode,
3078 .stateid = &arg.stateid,
3079 };
3080 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
David Quigleyaa9c2662013-05-22 12:50:44 -04003082 arg.bitmask = nfs4_bitmask(server, ilabel);
3083 if (ilabel)
3084 arg.bitmask = nfs4_bitmask(server, olabel);
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003087 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003088 switch (err) {
3089 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003090 if (!(sattr->ia_valid & ATTR_SIZE)) {
3091 pr_warn_once("NFSv4: server %s is incorrectly "
3092 "applying open mode checks to "
3093 "a SETATTR that is not "
3094 "changing file size.\n",
3095 server->nfs_client->cl_hostname);
3096 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003097 if (state && !(state->state & FMODE_WRITE)) {
3098 err = -EBADF;
3099 if (sattr->ia_valid & ATTR_OPEN)
3100 err = -EACCES;
3101 goto out;
3102 }
3103 }
3104 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003106out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return err;
3108}
3109
Peng Tao500d7012015-09-22 11:35:22 +08003110static bool
3111nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3112{
3113 if (inode == NULL || !nfs_have_layout(inode))
3114 return false;
3115
3116 return pnfs_wait_on_layoutreturn(inode, task);
3117}
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119struct nfs4_closedata {
3120 struct inode *inode;
3121 struct nfs4_state *state;
3122 struct nfs_closeargs arg;
3123 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003124 struct {
3125 struct nfs4_layoutreturn_args arg;
3126 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003127 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003128 u32 roc_barrier;
3129 bool roc;
3130 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003131 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003132 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133};
3134
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003135static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003136{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003137 struct nfs4_closedata *calldata = data;
3138 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003139 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003140
Trond Myklebustcf805162016-11-15 14:56:07 -05003141 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003142 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3143 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003144 nfs4_put_open_state(calldata->state);
3145 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003146 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003147 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003148 kfree(calldata);
3149}
3150
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003151static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003153 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003156 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003157 struct nfs4_exception exception = {
3158 .state = state,
3159 .inode = calldata->inode,
3160 .stateid = &calldata->arg.stateid,
3161 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Chuck Levera3ca5652012-03-01 17:00:40 -05003163 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003164 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3165 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003166 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003167
3168 /* Handle Layoutreturn errors */
3169 if (calldata->arg.lr_args && task->tk_status != 0) {
3170 switch (calldata->res.lr_ret) {
3171 default:
3172 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3173 break;
3174 case 0:
3175 calldata->arg.lr_args = NULL;
3176 calldata->res.lr_res = NULL;
3177 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003178 case -NFS4ERR_OLD_STATEID:
3179 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003180 calldata->inode))
3181 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003182 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003183 case -NFS4ERR_ADMIN_REVOKED:
3184 case -NFS4ERR_DELEG_REVOKED:
3185 case -NFS4ERR_EXPIRED:
3186 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003187 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3188 case -NFS4ERR_WRONG_CRED:
3189 calldata->arg.lr_args = NULL;
3190 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003191 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003192 }
3193 }
3194
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003195 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 * the state_owner. we keep this around to process errors
3197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 switch (task->tk_status) {
3199 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003200 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003201 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003202 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003203 case -NFS4ERR_ACCESS:
3204 if (calldata->arg.bitmask != NULL) {
3205 calldata->arg.bitmask = NULL;
3206 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003207 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003208
3209 }
3210 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003211 case -NFS4ERR_OLD_STATEID:
3212 /* Did we race with OPEN? */
3213 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003214 state))
3215 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003216 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003217 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003219 case -NFS4ERR_EXPIRED:
3220 nfs4_free_revoked_stateid(server,
3221 &calldata->arg.stateid,
3222 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003223 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003224 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003227 task->tk_status = nfs4_async_handle_exception(task,
3228 server, task->tk_status, &exception);
3229 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003230 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003232 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3233 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003234out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003235 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003236 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003237 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003238 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003239 return;
3240lr_restart:
3241 calldata->res.lr_ret = 0;
3242out_restart:
3243 task->tk_status = 0;
3244 rpc_restart_call_prepare(task);
3245 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003248static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003250 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003251 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003252 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003253 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003254 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003255
Chuck Levera3ca5652012-03-01 17:00:40 -05003256 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003257 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003258 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003259
Trond Myklebust88069f72009-12-08 08:33:16 -05003260 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003261 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003262 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3263 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3264 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003265 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003266 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003267 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003268 if (state->n_rdonly == 0)
3269 call_close |= is_rdonly;
3270 else if (is_rdonly)
3271 calldata->arg.fmode |= FMODE_READ;
3272 if (state->n_wronly == 0)
3273 call_close |= is_wronly;
3274 else if (is_wronly)
3275 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003276 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3277 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003278 } else if (is_rdwr)
3279 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3280
Trond Myklebust5cc78612016-11-14 11:19:56 -05003281 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003282 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003283 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003284 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003285
3286 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003287 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003288 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003289 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003290
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003291 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003292 nfs_release_seqid(calldata->arg.seqid);
3293 goto out_wait;
3294 }
3295
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003296 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003297 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003298
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003299 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003300 /* Close-to-open cache consistency revalidation */
3301 if (!nfs4_have_delegation(inode, FMODE_READ))
3302 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3303 else
3304 calldata->arg.bitmask = NULL;
3305 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003306
Trond Myklebust6ae37332015-01-30 14:21:14 -05003307 calldata->arg.share_access =
3308 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3309 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003310
Trond Myklebustd8d84982016-12-19 12:14:44 -05003311 if (calldata->res.fattr == NULL)
3312 calldata->arg.bitmask = NULL;
3313 else if (calldata->arg.bitmask == NULL)
3314 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003315 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003316 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003317 &calldata->arg.seq_args,
3318 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003319 task) != 0)
3320 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003321 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003322 return;
3323out_no_action:
3324 task->tk_action = NULL;
3325out_wait:
3326 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003329static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003330 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003331 .rpc_call_done = nfs4_close_done,
3332 .rpc_release = nfs4_free_closedata,
3333};
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335/*
3336 * It is possible for data to be read/written from a mem-mapped file
3337 * after the sys_close call (which hits the vfs layer as a flush).
3338 * This means that we can't safely call nfsv4 close on a file until
3339 * the inode is cleared. This in turn means that we are not good
3340 * NFSv4 citizens - we do not indicate to the server to update the file's
3341 * share state even when we are done with one of the three share
3342 * stateid's in the inode.
3343 *
3344 * NOTE: Caller must be holding the sp->so_owner semaphore!
3345 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003346int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003348 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003349 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003351 struct nfs4_state_owner *sp = state->owner;
3352 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003353 struct rpc_message msg = {
3354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3355 .rpc_cred = state->owner->so_cred,
3356 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003357 struct rpc_task_setup task_setup_data = {
3358 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003359 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003360 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003361 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003362 .flags = RPC_TASK_ASYNC,
3363 };
Trond Myklebust95121352005-10-18 14:20:12 -07003364 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003366 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3367 &task_setup_data.rpc_client, &msg);
3368
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003369 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003371 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003372 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003373 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003375 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003376 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3377 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003379 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3380 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003381 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003382 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003383 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003384 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003385 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003386 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003387 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003388 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003389 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003390 calldata->lr.roc = pnfs_roc(state->inode,
3391 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3392 if (calldata->lr.roc) {
3393 calldata->arg.lr_args = &calldata->lr.arg;
3394 calldata->res.lr_res = &calldata->lr.res;
3395 }
Al Viro643168c2011-06-22 18:20:23 -04003396 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003397
Trond Myklebust1174dd12010-12-21 10:52:24 -05003398 msg.rpc_argp = &calldata->arg;
3399 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003400 task_setup_data.callback_data = calldata;
3401 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003402 if (IS_ERR(task))
3403 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003404 status = 0;
3405 if (wait)
3406 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003407 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003408 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003409out_free_calldata:
3410 kfree(calldata);
3411out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003412 nfs4_put_open_state(state);
3413 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003414 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Trond Myklebust2b484292010-09-17 10:56:51 -04003417static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003418nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3419 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003422 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3423
3424 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Trond Myklebust565277f2007-10-15 18:17:53 -04003426 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003427 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003428
3429 nfs4_label_release_security(label);
3430
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003431 if (IS_ERR(state))
3432 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003433 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Trond Myklebust1185a552009-12-03 15:54:02 -05003436static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003437{
3438 if (ctx->state == NULL)
3439 return;
3440 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003441 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003442 else
Al Viro643168c2011-06-22 18:20:23 -04003443 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003444}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Trond Myklebustb944dba2013-11-04 15:20:20 -05003446#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3447#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003448#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3451{
Kinglong Mee8c612822015-08-26 21:12:58 +08003452 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003453 struct nfs4_server_caps_arg args = {
3454 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003455 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003456 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 struct nfs4_server_caps_res res = {};
3458 struct rpc_message msg = {
3459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003460 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 .rpc_resp = &res,
3462 };
3463 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003464 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Kinglong Mee8c612822015-08-26 21:12:58 +08003466 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3467 FATTR4_WORD0_FH_EXPIRE_TYPE |
3468 FATTR4_WORD0_LINK_SUPPORT |
3469 FATTR4_WORD0_SYMLINK_SUPPORT |
3470 FATTR4_WORD0_ACLSUPPORT;
3471 if (minorversion)
3472 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3473
Bryan Schumaker7c513052011-03-24 17:12:24 +00003474 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003476 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003477 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003478 case 0:
3479 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3480 res.attr_bitmask[2] = 0;
3481 break;
3482 case 1:
3483 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3484 break;
3485 case 2:
3486 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003489 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3490 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3491 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3492 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003493 NFS_CAP_CTIME|NFS_CAP_MTIME|
3494 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003495 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3496 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 server->caps |= NFS_CAP_ACLS;
3498 if (res.has_links != 0)
3499 server->caps |= NFS_CAP_HARDLINKS;
3500 if (res.has_symlinks != 0)
3501 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003502 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3503 server->caps |= NFS_CAP_FILEID;
3504 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3505 server->caps |= NFS_CAP_MODE;
3506 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3507 server->caps |= NFS_CAP_NLINK;
3508 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3509 server->caps |= NFS_CAP_OWNER;
3510 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3511 server->caps |= NFS_CAP_OWNER_GROUP;
3512 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3513 server->caps |= NFS_CAP_ATIME;
3514 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3515 server->caps |= NFS_CAP_CTIME;
3516 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3517 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003518#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3519 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3520 server->caps |= NFS_CAP_SECURITY_LABEL;
3521#endif
3522 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3523 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003524 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003525
Trond Myklebusta65318b2009-03-11 14:10:28 -04003526 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3527 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3528 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003529 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003530
3531 /* Avoid a regression due to buggy server */
3532 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3533 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003534 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3535 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003538 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return status;
3542}
3543
Trond Myklebust55a97592006-06-09 09:34:19 -04003544int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
3546 struct nfs4_exception exception = { };
3547 int err;
3548 do {
3549 err = nfs4_handle_exception(server,
3550 _nfs4_server_capabilities(server, fhandle),
3551 &exception);
3552 } while (exception.retry);
3553 return err;
3554}
3555
3556static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3557 struct nfs_fsinfo *info)
3558{
David Quigleyaa9c2662013-05-22 12:50:44 -04003559 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003561 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 };
3563 struct nfs4_lookup_res res = {
3564 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003565 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 .fh = fhandle,
3567 };
3568 struct rpc_message msg = {
3569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3570 .rpc_argp = &args,
3571 .rpc_resp = &res,
3572 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003573
David Quigleyaa9c2662013-05-22 12:50:44 -04003574 bitmask[0] = nfs4_fattr_bitmap[0];
3575 bitmask[1] = nfs4_fattr_bitmap[1];
3576 /*
3577 * Process the label in the upcoming getfattr
3578 */
3579 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3580
Trond Myklebust0e574af2005-10-27 22:12:38 -04003581 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003582 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
3585static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3586 struct nfs_fsinfo *info)
3587{
3588 struct nfs4_exception exception = { };
3589 int err;
3590 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003591 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003592 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003593 switch (err) {
3594 case 0:
3595 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003596 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003597 default:
3598 err = nfs4_handle_exception(server, err, &exception);
3599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003601out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 return err;
3603}
3604
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003605static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3606 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3607{
Trond Myklebustc2190662013-08-26 19:23:04 -04003608 struct rpc_auth_create_args auth_args = {
3609 .pseudoflavor = flavor,
3610 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003611 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003612
Trond Myklebustc2190662013-08-26 19:23:04 -04003613 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003614 if (IS_ERR(auth))
3615 return -EACCES;
3616 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003617}
3618
Chuck Lever9a744ba2013-03-16 15:56:02 -04003619/*
3620 * Retry pseudoroot lookup with various security flavors. We do this when:
3621 *
3622 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3623 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3624 *
3625 * Returns zero on success, or a negative NFS4ERR value, or a
3626 * negative errno value.
3627 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003628static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003629 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003631 /* Per 3530bis 15.33.5 */
3632 static const rpc_authflavor_t flav_array[] = {
3633 RPC_AUTH_GSS_KRB5P,
3634 RPC_AUTH_GSS_KRB5I,
3635 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003636 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003637 RPC_AUTH_NULL,
3638 };
3639 int status = -EPERM;
3640 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003642 if (server->auth_info.flavor_len > 0) {
3643 /* try each flavor specified by user */
3644 for (i = 0; i < server->auth_info.flavor_len; i++) {
3645 status = nfs4_lookup_root_sec(server, fhandle, info,
3646 server->auth_info.flavors[i]);
3647 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3648 continue;
3649 break;
3650 }
3651 } else {
3652 /* no flavors specified by user, try default list */
3653 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3654 status = nfs4_lookup_root_sec(server, fhandle, info,
3655 flav_array[i]);
3656 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3657 continue;
3658 break;
3659 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003660 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003661
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003662 /*
3663 * -EACCESS could mean that the user doesn't have correct permissions
3664 * to access the mount. It could also mean that we tried to mount
3665 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3666 * existing mount programs don't handle -EACCES very well so it should
3667 * be mapped to -EPERM instead.
3668 */
3669 if (status == -EACCES)
3670 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003671 return status;
3672}
3673
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003674/**
3675 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3676 * @server: initialized nfs_server handle
3677 * @fhandle: we fill in the pseudo-fs root file handle
3678 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003679 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003680 *
3681 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003682 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003683int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003684 struct nfs_fsinfo *info,
3685 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003686{
Andre Przywarac7757072015-04-23 17:17:40 +01003687 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003688
Andre Przywarac7757072015-04-23 17:17:40 +01003689 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003690 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003691
3692 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003693 status = server->nfs_client->cl_mvops->find_root_sec(server,
3694 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 if (status == 0)
3697 status = nfs4_server_capabilities(server, fhandle);
3698 if (status == 0)
3699 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003700
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003701 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
Bryan Schumakerbae36242012-05-10 15:07:31 -04003704static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3705 struct nfs_fsinfo *info)
3706{
3707 int error;
3708 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003709 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003710
3711 error = nfs4_server_capabilities(server, mntfh);
3712 if (error < 0) {
3713 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3714 return error;
3715 }
3716
David Quigley14c43f72013-05-22 12:50:43 -04003717 label = nfs4_label_alloc(server, GFP_KERNEL);
3718 if (IS_ERR(label))
3719 return PTR_ERR(label);
3720
David Quigley1775fd32013-05-22 12:50:42 -04003721 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003722 if (error < 0) {
3723 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003724 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003725 }
3726
3727 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3728 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3729 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3730
David Quigley14c43f72013-05-22 12:50:43 -04003731err_free_label:
3732 nfs4_label_free(label);
3733
Bryan Schumakerbae36242012-05-10 15:07:31 -04003734 return error;
3735}
3736
Manoj Naik6b97fd32006-06-09 09:34:29 -04003737/*
3738 * Get locations and (maybe) other attributes of a referral.
3739 * Note that we'll actually follow the referral later when
3740 * we detect fsid mismatch in inode revalidation
3741 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003742static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3743 const struct qstr *name, struct nfs_fattr *fattr,
3744 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003745{
3746 int status = -ENOMEM;
3747 struct page *page = NULL;
3748 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003749
3750 page = alloc_page(GFP_KERNEL);
3751 if (page == NULL)
3752 goto out;
3753 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3754 if (locations == NULL)
3755 goto out;
3756
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003757 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003758 if (status != 0)
3759 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003760
3761 /*
3762 * If the fsid didn't change, this is a migration event, not a
3763 * referral. Cause us to drop into the exception handler, which
3764 * will kick off migration recovery.
3765 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003766 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003767 dprintk("%s: server did not return a different fsid for"
3768 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003769 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003770 goto out;
3771 }
Andy Adamson533eb462011-06-13 18:25:56 -04003772 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3773 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003774
Andy Adamson533eb462011-06-13 18:25:56 -04003775 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003776 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003777 memset(fhandle, 0, sizeof(struct nfs_fh));
3778out:
3779 if (page)
3780 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003781 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003782 return status;
3783}
3784
David Quigley1775fd32013-05-22 12:50:42 -04003785static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3786 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
3788 struct nfs4_getattr_arg args = {
3789 .fh = fhandle,
3790 .bitmask = server->attr_bitmask,
3791 };
3792 struct nfs4_getattr_res res = {
3793 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003794 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 .server = server,
3796 };
3797 struct rpc_message msg = {
3798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3799 .rpc_argp = &args,
3800 .rpc_resp = &res,
3801 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003802
3803 args.bitmask = nfs4_bitmask(server, label);
3804
Trond Myklebust0e574af2005-10-27 22:12:38 -04003805 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003806 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
David Quigley1775fd32013-05-22 12:50:42 -04003809static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3810 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
3812 struct nfs4_exception exception = { };
3813 int err;
3814 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003815 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3816 trace_nfs4_getattr(server, fhandle, fattr, err);
3817 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 &exception);
3819 } while (exception.retry);
3820 return err;
3821}
3822
3823/*
3824 * The file is not closed if it is opened due to the a request to change
3825 * the size of the file. The open call will not be needed once the
3826 * VFS layer lookup-intents are implemented.
3827 *
3828 * Close is called when the inode is destroyed.
3829 * If we haven't opened the file for O_WRONLY, we
3830 * need to in the size_change case to obtain a stateid.
3831 *
3832 * Got race?
3833 * Because OPEN is always done by name in nfsv4, it is
3834 * possible that we opened a different file by the same
3835 * name. We can recognize this race condition, but we
3836 * can't do anything about it besides returning an error.
3837 *
3838 * This will be fixed with VFS changes (lookup-intent).
3839 */
3840static int
3841nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3842 struct iattr *sattr)
3843{
David Howells2b0143b2015-03-17 22:25:59 +00003844 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003845 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003846 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003847 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 int status;
3849
Peng Tao88ac8152014-09-12 11:04:10 +08003850 if (pnfs_ld_layoutret_on_setattr(inode) &&
3851 sattr->ia_valid & ATTR_SIZE &&
3852 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003853 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003854
Trond Myklebust0e574af2005-10-27 22:12:38 -04003855 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Andy Adamson26699402012-05-30 16:12:24 -04003857 /* Deal with open(O_TRUNC) */
3858 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003859 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003860
3861 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003862 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003863 return 0;
3864
Trond Myklebustd5308382005-11-04 15:33:38 -05003865 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003866 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003867
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003868 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003869 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003870 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003871 }
3872
David Quigley14c43f72013-05-22 12:50:43 -04003873 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3874 if (IS_ERR(label))
3875 return PTR_ERR(label);
3876
NeilBrown29b59f92016-10-13 15:26:47 +11003877 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003878 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003879 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003880 nfs_setsecurity(inode, fattr, label);
3881 }
David Quigley14c43f72013-05-22 12:50:43 -04003882 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 return status;
3884}
3885
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003886static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3887 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003888 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003889{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003890 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003891 int status;
3892 struct nfs4_lookup_arg args = {
3893 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003894 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003895 .name = name,
3896 };
3897 struct nfs4_lookup_res res = {
3898 .server = server,
3899 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003900 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003901 .fh = fhandle,
3902 };
3903 struct rpc_message msg = {
3904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3905 .rpc_argp = &args,
3906 .rpc_resp = &res,
3907 };
3908
David Quigleyaa9c2662013-05-22 12:50:44 -04003909 args.bitmask = nfs4_bitmask(server, label);
3910
David Howells2b3de442006-08-22 20:06:09 -04003911 nfs_fattr_init(fattr);
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003914 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 dprintk("NFS reply lookup: %d\n", status);
3916 return status;
3917}
3918
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003919static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003920{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003921 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003922 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003923 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3924 fattr->nlink = 2;
3925}
3926
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003927static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003928 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003929 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003930{
3931 struct nfs4_exception exception = { };
3932 struct rpc_clnt *client = *clnt;
3933 int err;
3934 do {
David Quigley1775fd32013-05-22 12:50:42 -04003935 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003936 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003937 switch (err) {
3938 case -NFS4ERR_BADNAME:
3939 err = -ENOENT;
3940 goto out;
3941 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003942 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003943 if (err == -NFS4ERR_MOVED)
3944 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003945 goto out;
3946 case -NFS4ERR_WRONGSEC:
3947 err = -EPERM;
3948 if (client != *clnt)
3949 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003950 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003951 if (IS_ERR(client))
3952 return PTR_ERR(client);
3953
3954 exception.retry = 1;
3955 break;
3956 default:
3957 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3958 }
3959 } while (exception.retry);
3960
3961out:
3962 if (err == 0)
3963 *clnt = client;
3964 else if (client != *clnt)
3965 rpc_shutdown_client(client);
3966
3967 return err;
3968}
3969
Al Virobeffb8f2016-07-20 16:34:42 -04003970static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003971 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3972 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003974 int status;
3975 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003976
David Quigley1775fd32013-05-22 12:50:42 -04003977 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003978 if (client != NFS_CLIENT(dir)) {
3979 rpc_shutdown_client(client);
3980 nfs_fixup_secinfo_attributes(fattr);
3981 }
3982 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003985struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003986nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003987 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3988{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003989 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003990 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003991
David Quigley1775fd32013-05-22 12:50:42 -04003992 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003993 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003994 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003995 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003996}
3997
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003998static int _nfs4_proc_lookupp(struct inode *inode,
3999 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4000 struct nfs4_label *label)
4001{
4002 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4003 struct nfs_server *server = NFS_SERVER(inode);
4004 int status;
4005 struct nfs4_lookupp_arg args = {
4006 .bitmask = server->attr_bitmask,
4007 .fh = NFS_FH(inode),
4008 };
4009 struct nfs4_lookupp_res res = {
4010 .server = server,
4011 .fattr = fattr,
4012 .label = label,
4013 .fh = fhandle,
4014 };
4015 struct rpc_message msg = {
4016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4017 .rpc_argp = &args,
4018 .rpc_resp = &res,
4019 };
4020
4021 args.bitmask = nfs4_bitmask(server, label);
4022
4023 nfs_fattr_init(fattr);
4024
4025 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4026 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4027 &res.seq_res, 0);
4028 dprintk("NFS reply lookupp: %d\n", status);
4029 return status;
4030}
4031
4032static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4033 struct nfs_fattr *fattr, struct nfs4_label *label)
4034{
4035 struct nfs4_exception exception = { };
4036 int err;
4037 do {
4038 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4039 trace_nfs4_lookupp(inode, err);
4040 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4041 &exception);
4042 } while (exception.retry);
4043 return err;
4044}
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4047{
Trond Myklebust76b32992007-08-10 17:45:11 -04004048 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 struct nfs4_accessargs args = {
4050 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004051 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004052 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004054 struct nfs4_accessres res = {
4055 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004056 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 struct rpc_message msg = {
4058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4059 .rpc_argp = &args,
4060 .rpc_resp = &res,
4061 .rpc_cred = entry->cred,
4062 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004063 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Trond Myklebustc407d412010-04-16 16:22:48 -04004065 res.fattr = nfs_alloc_fattr();
4066 if (res.fattr == NULL)
4067 return -ENOMEM;
4068
Bryan Schumaker7c513052011-03-24 17:12:24 +00004069 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004071 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004072 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004074 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return status;
4076}
4077
4078static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4079{
4080 struct nfs4_exception exception = { };
4081 int err;
4082 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004083 err = _nfs4_proc_access(inode, entry);
4084 trace_nfs4_access(inode, err);
4085 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 &exception);
4087 } while (exception.retry);
4088 return err;
4089}
4090
4091/*
4092 * TODO: For the time being, we don't try to get any attributes
4093 * along with any of the zero-copy operations READ, READDIR,
4094 * READLINK, WRITE.
4095 *
4096 * In the case of the first three, we want to put the GETATTR
4097 * after the read-type operation -- this is because it is hard
4098 * to predict the length of a GETATTR response in v4, and thus
4099 * align the READ data correctly. This means that the GETATTR
4100 * may end up partially falling into the page cache, and we should
4101 * shift it into the 'tail' of the xdr_buf before processing.
4102 * To do this efficiently, we need to know the total length
4103 * of data received, which doesn't seem to be available outside
4104 * of the RPC layer.
4105 *
4106 * In the case of WRITE, we also want to put the GETATTR after
4107 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004108 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 *
4110 * Both of these changes to the XDR layer would in fact be quite
4111 * minor, but I decided to leave them for a subsequent patch.
4112 */
4113static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4114 unsigned int pgbase, unsigned int pglen)
4115{
4116 struct nfs4_readlink args = {
4117 .fh = NFS_FH(inode),
4118 .pgbase = pgbase,
4119 .pglen = pglen,
4120 .pages = &page,
4121 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004122 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 struct rpc_message msg = {
4124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4125 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004126 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 };
4128
Bryan Schumaker7c513052011-03-24 17:12:24 +00004129 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 -07004130}
4131
4132static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4133 unsigned int pgbase, unsigned int pglen)
4134{
4135 struct nfs4_exception exception = { };
4136 int err;
4137 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004138 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4139 trace_nfs4_readlink(inode, err);
4140 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 &exception);
4142 } while (exception.retry);
4143 return err;
4144}
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004147 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149static int
4150nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004151 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004153 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004154 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004155 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 int status = 0;
4158
NeilBrown532d4de2016-10-13 15:26:47 +11004159 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004160 if (IS_ERR(ctx))
4161 return PTR_ERR(ctx);
4162
David Quigleyaa9c2662013-05-22 12:50:44 -04004163 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4164
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004165 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4166 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004167 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 if (IS_ERR(state)) {
4169 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004173 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004174 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 return status;
4176}
4177
Al Virobeffb8f2016-07-20 16:34:42 -04004178static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
Trond Myklebust16e42952005-10-27 22:12:44 -04004180 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004181 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004183 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004185 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004186 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4190 .rpc_argp = &args,
4191 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004193 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004194 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004195
Bryan Schumaker7c513052011-03-24 17:12:24 +00004196 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004197 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004198 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return status;
4200}
4201
Al Virobeffb8f2016-07-20 16:34:42 -04004202static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
4204 struct nfs4_exception exception = { };
4205 int err;
4206 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004207 err = _nfs4_proc_remove(dir, name);
4208 trace_nfs4_remove(dir, name, err);
4209 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 &exception);
4211 } while (exception.retry);
4212 return err;
4213}
4214
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004215static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004217 struct nfs_server *server = NFS_SERVER(dir);
4218 struct nfs_removeargs *args = msg->rpc_argp;
4219 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004221 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004223 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004224
4225 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226}
4227
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004228static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4229{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004230 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004231 &data->args.seq_args,
4232 &data->res.seq_res,
4233 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234}
4235
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004236static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004238 struct nfs_unlinkdata *data = task->tk_calldata;
4239 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004240
Trond Myklebust14516c32010-07-31 14:29:06 -04004241 if (!nfs4_sequence_done(task, &res->seq_res))
4242 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004243 if (nfs4_async_handle_error(task, res->server, NULL,
4244 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004245 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004246 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004247 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004248 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Jeff Laytond3d41522010-09-17 17:31:57 -04004251static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4252{
4253 struct nfs_server *server = NFS_SERVER(dir);
4254 struct nfs_renameargs *arg = msg->rpc_argp;
4255 struct nfs_renameres *res = msg->rpc_resp;
4256
4257 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004258 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004259 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004260}
4261
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004262static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4263{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004264 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004265 &data->args.seq_args,
4266 &data->res.seq_res,
4267 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004268}
4269
4270static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4271 struct inode *new_dir)
4272{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004273 struct nfs_renamedata *data = task->tk_calldata;
4274 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004275
4276 if (!nfs4_sequence_done(task, &res->seq_res))
4277 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004278 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004279 return 0;
4280
Trond Myklebustc733c492017-01-11 12:32:26 -05004281 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004282 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004283 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004284 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004285 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004286 return 1;
4287}
4288
Al Virobeffb8f2016-07-20 16:34:42 -04004289static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004291 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 struct nfs4_link_arg arg = {
4293 .fh = NFS_FH(inode),
4294 .dir_fh = NFS_FH(dir),
4295 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004296 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004298 struct nfs4_link_res res = {
4299 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004300 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004301 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 struct rpc_message msg = {
4303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4304 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004305 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004307 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Trond Myklebust136f2622010-04-16 16:22:49 -04004309 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004310 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004311 goto out;
4312
David Quigley14c43f72013-05-22 12:50:43 -04004313 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4314 if (IS_ERR(res.label)) {
4315 status = PTR_ERR(res.label);
4316 goto out;
4317 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004318 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004319
Bryan Schumaker7c513052011-03-24 17:12:24 +00004320 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004321 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004322 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004323 status = nfs_post_op_update_inode(inode, res.fattr);
4324 if (!status)
4325 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004326 }
David Quigley14c43f72013-05-22 12:50:43 -04004327
4328
4329 nfs4_label_free(res.label);
4330
Trond Myklebust136f2622010-04-16 16:22:49 -04004331out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004332 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return status;
4334}
4335
Al Virobeffb8f2016-07-20 16:34:42 -04004336static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
4338 struct nfs4_exception exception = { };
4339 int err;
4340 do {
4341 err = nfs4_handle_exception(NFS_SERVER(inode),
4342 _nfs4_proc_link(inode, dir, name),
4343 &exception);
4344 } while (exception.retry);
4345 return err;
4346}
4347
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348struct nfs4_createdata {
4349 struct rpc_message msg;
4350 struct nfs4_create_arg arg;
4351 struct nfs4_create_res res;
4352 struct nfs_fh fh;
4353 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004354 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004355};
4356
4357static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004358 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004359{
4360 struct nfs4_createdata *data;
4361
4362 data = kzalloc(sizeof(*data), GFP_KERNEL);
4363 if (data != NULL) {
4364 struct nfs_server *server = NFS_SERVER(dir);
4365
David Quigley14c43f72013-05-22 12:50:43 -04004366 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4367 if (IS_ERR(data->label))
4368 goto out_free;
4369
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004370 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4371 data->msg.rpc_argp = &data->arg;
4372 data->msg.rpc_resp = &data->res;
4373 data->arg.dir_fh = NFS_FH(dir);
4374 data->arg.server = server;
4375 data->arg.name = name;
4376 data->arg.attrs = sattr;
4377 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004378 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004379 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004380 data->res.server = server;
4381 data->res.fh = &data->fh;
4382 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004383 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004384 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004385 }
4386 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004387out_free:
4388 kfree(data);
4389 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004390}
4391
4392static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4393{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004394 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004395 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004397 update_changeattr(dir, &data->res.dir_cinfo,
4398 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004399 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004400 }
4401 return status;
4402}
4403
4404static void nfs4_free_createdata(struct nfs4_createdata *data)
4405{
David Quigley14c43f72013-05-22 12:50:43 -04004406 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004407 kfree(data);
4408}
4409
Chuck Lever4f390c12006-08-22 20:06:22 -04004410static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004411 struct page *page, unsigned int len, struct iattr *sattr,
4412 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414 struct nfs4_createdata *data;
4415 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Chuck Lever94a6d752006-08-22 20:06:23 -04004417 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004418 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004419
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004420 status = -ENOMEM;
4421 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4422 if (data == NULL)
4423 goto out;
4424
4425 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4426 data->arg.u.symlink.pages = &page;
4427 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004428 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004430 status = nfs4_do_create(dir, dentry, data);
4431
4432 nfs4_free_createdata(data);
4433out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 return status;
4435}
4436
Chuck Lever4f390c12006-08-22 20:06:22 -04004437static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004438 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
4440 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004441 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004443
4444 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004447 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4448 trace_nfs4_symlink(dir, &dentry->d_name, err);
4449 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 &exception);
4451 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004452
4453 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return err;
4455}
4456
4457static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004458 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004460 struct nfs4_createdata *data;
4461 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004463 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4464 if (data == NULL)
4465 goto out;
4466
David Quigley1775fd32013-05-22 12:50:42 -04004467 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004468 status = nfs4_do_create(dir, dentry, data);
4469
4470 nfs4_free_createdata(data);
4471out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 return status;
4473}
4474
4475static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4476 struct iattr *sattr)
4477{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004478 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004480 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004482
David Quigleyaa9c2662013-05-22 12:50:44 -04004483 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4484
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004485 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4486 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004488 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4489 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4490 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 &exception);
4492 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004493 nfs4_label_release_security(label);
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return err;
4496}
4497
4498static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004499 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
David Howells2b0143b2015-03-17 22:25:59 +00004501 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 struct nfs4_readdir_arg args = {
4503 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004504 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 .pgbase = 0,
4506 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004507 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004508 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 };
4510 struct nfs4_readdir_res res;
4511 struct rpc_message msg = {
4512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4513 .rpc_argp = &args,
4514 .rpc_resp = &res,
4515 .rpc_cred = cred,
4516 };
4517 int status;
4518
Al Viro6de14722013-09-16 10:53:17 -04004519 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4520 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004521 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004522 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004524 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 -05004525 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004526 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004527 status += args.pgbase;
4528 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004529
4530 nfs_invalidate_atime(dir);
4531
Harvey Harrison3110ff82008-05-02 13:42:44 -07004532 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 return status;
4534}
4535
4536static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004537 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
4539 struct nfs4_exception exception = { };
4540 int err;
4541 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004542 err = _nfs4_proc_readdir(dentry, cred, cookie,
4543 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004544 trace_nfs4_readdir(d_inode(dentry), err);
4545 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 &exception);
4547 } while (exception.retry);
4548 return err;
4549}
4550
4551static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004552 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004554 struct nfs4_createdata *data;
4555 int mode = sattr->ia_mode;
4556 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004558 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4559 if (data == NULL)
4560 goto out;
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004563 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004565 data->arg.ftype = NF4BLK;
4566 data->arg.u.device.specdata1 = MAJOR(rdev);
4567 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004570 data->arg.ftype = NF4CHR;
4571 data->arg.u.device.specdata1 = MAJOR(rdev);
4572 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004573 } else if (!S_ISSOCK(mode)) {
4574 status = -EINVAL;
4575 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 }
David Quigley1775fd32013-05-22 12:50:42 -04004577
David Quigleyaa9c2662013-05-22 12:50:44 -04004578 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004579 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004580out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004581 nfs4_free_createdata(data);
4582out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 return status;
4584}
4585
4586static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4587 struct iattr *sattr, dev_t rdev)
4588{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004589 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004591 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004593
David Quigleyaa9c2662013-05-22 12:50:44 -04004594 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4595
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004596 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4597 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004599 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4600 trace_nfs4_mknod(dir, &dentry->d_name, err);
4601 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 &exception);
4603 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004604
4605 nfs4_label_release_security(label);
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 return err;
4608}
4609
4610static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4611 struct nfs_fsstat *fsstat)
4612{
4613 struct nfs4_statfs_arg args = {
4614 .fh = fhandle,
4615 .bitmask = server->attr_bitmask,
4616 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004617 struct nfs4_statfs_res res = {
4618 .fsstat = fsstat,
4619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 struct rpc_message msg = {
4621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4622 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004623 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 };
4625
Trond Myklebust0e574af2005-10-27 22:12:38 -04004626 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004627 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
4630static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4631{
4632 struct nfs4_exception exception = { };
4633 int err;
4634 do {
4635 err = nfs4_handle_exception(server,
4636 _nfs4_proc_statfs(server, fhandle, fsstat),
4637 &exception);
4638 } while (exception.retry);
4639 return err;
4640}
4641
4642static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4643 struct nfs_fsinfo *fsinfo)
4644{
4645 struct nfs4_fsinfo_arg args = {
4646 .fh = fhandle,
4647 .bitmask = server->attr_bitmask,
4648 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004649 struct nfs4_fsinfo_res res = {
4650 .fsinfo = fsinfo,
4651 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 struct rpc_message msg = {
4653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4654 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004655 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 };
4657
Bryan Schumaker7c513052011-03-24 17:12:24 +00004658 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
4661static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4662{
4663 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004664 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 int err;
4666
4667 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004668 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004669 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004670 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004671 nfs4_set_lease_period(server->nfs_client,
4672 fsinfo->lease_time * HZ,
4673 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004674 break;
4675 }
4676 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 } while (exception.retry);
4678 return err;
4679}
4680
4681static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4682{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004683 int error;
4684
Trond Myklebust0e574af2005-10-27 22:12:38 -04004685 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004686 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004687 if (error == 0) {
4688 /* block layout checks this! */
4689 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004690 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004691 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004692
4693 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
4696static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4697 struct nfs_pathconf *pathconf)
4698{
4699 struct nfs4_pathconf_arg args = {
4700 .fh = fhandle,
4701 .bitmask = server->attr_bitmask,
4702 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004703 struct nfs4_pathconf_res res = {
4704 .pathconf = pathconf,
4705 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 struct rpc_message msg = {
4707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4708 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004709 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 };
4711
4712 /* None of the pathconf attributes are mandatory to implement */
4713 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4714 memset(pathconf, 0, sizeof(*pathconf));
4715 return 0;
4716 }
4717
Trond Myklebust0e574af2005-10-27 22:12:38 -04004718 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004719 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
4722static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4723 struct nfs_pathconf *pathconf)
4724{
4725 struct nfs4_exception exception = { };
4726 int err;
4727
4728 do {
4729 err = nfs4_handle_exception(server,
4730 _nfs4_proc_pathconf(server, fhandle, pathconf),
4731 &exception);
4732 } while (exception.retry);
4733 return err;
4734}
4735
Trond Myklebust5521abf2013-03-16 20:54:34 -04004736int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004737 const struct nfs_open_context *ctx,
4738 const struct nfs_lock_context *l_ctx,
4739 fmode_t fmode)
4740{
NeilBrown17393472016-10-13 15:26:47 +11004741 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004742}
4743EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4744
Trond Myklebust5521abf2013-03-16 20:54:34 -04004745static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4746 const struct nfs_open_context *ctx,
4747 const struct nfs_lock_context *l_ctx,
4748 fmode_t fmode)
4749{
4750 nfs4_stateid current_stateid;
4751
Trond Myklebuste1253be2014-03-05 08:44:23 -05004752 /* If the current stateid represents a lost lock, then exit */
4753 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4754 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004755 return nfs4_stateid_match(stateid, &current_stateid);
4756}
4757
4758static bool nfs4_error_stateid_expired(int err)
4759{
4760 switch (err) {
4761 case -NFS4ERR_DELEG_REVOKED:
4762 case -NFS4ERR_ADMIN_REVOKED:
4763 case -NFS4ERR_BAD_STATEID:
4764 case -NFS4ERR_STALE_STATEID:
4765 case -NFS4ERR_OLD_STATEID:
4766 case -NFS4ERR_OPENMODE:
4767 case -NFS4ERR_EXPIRED:
4768 return true;
4769 }
4770 return false;
4771}
4772
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004773static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004775 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004777 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004778 if (task->tk_status < 0) {
4779 struct nfs4_exception exception = {
4780 .inode = hdr->inode,
4781 .state = hdr->args.context->state,
4782 .stateid = &hdr->args.stateid,
4783 };
4784 task->tk_status = nfs4_async_handle_exception(task,
4785 server, task->tk_status, &exception);
4786 if (exception.retry) {
4787 rpc_restart_call_prepare(task);
4788 return -EAGAIN;
4789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004793 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796
Trond Myklebust5521abf2013-03-16 20:54:34 -04004797static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004798 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004799{
4800
4801 if (!nfs4_error_stateid_expired(task->tk_status) ||
4802 nfs4_stateid_is_current(&args->stateid,
4803 args->context,
4804 args->lock_context,
4805 FMODE_READ))
4806 return false;
4807 rpc_restart_call_prepare(task);
4808 return true;
4809}
4810
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004811static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004812{
4813
4814 dprintk("--> %s\n", __func__);
4815
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004816 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004817 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004818 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004819 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004820 if (task->tk_status > 0)
4821 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004822 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4823 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004824}
4825
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004826static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4827 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004829 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004830 if (!hdr->pgio_done_cb)
4831 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004832 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004833 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834}
4835
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004836static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4837 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004838{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004839 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004840 &hdr->args.seq_args,
4841 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004842 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004843 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004844 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4845 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004846 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004847 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004848 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004849 return -EIO;
4850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004853static int nfs4_write_done_cb(struct rpc_task *task,
4854 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004856 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004857
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004858 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004859 if (task->tk_status < 0) {
4860 struct nfs4_exception exception = {
4861 .inode = hdr->inode,
4862 .state = hdr->args.context->state,
4863 .stateid = &hdr->args.stateid,
4864 };
4865 task->tk_status = nfs4_async_handle_exception(task,
4866 NFS_SERVER(inode), task->tk_status,
4867 &exception);
4868 if (exception.retry) {
4869 rpc_restart_call_prepare(task);
4870 return -EAGAIN;
4871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004873 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004874 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004875 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004876 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
4879
Trond Myklebust5521abf2013-03-16 20:54:34 -04004880static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004881 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004882{
4883
4884 if (!nfs4_error_stateid_expired(task->tk_status) ||
4885 nfs4_stateid_is_current(&args->stateid,
4886 args->context,
4887 args->lock_context,
4888 FMODE_WRITE))
4889 return false;
4890 rpc_restart_call_prepare(task);
4891 return true;
4892}
4893
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004894static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004895{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004896 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004897 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004898 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004899 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004900 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4901 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004902}
4903
Trond Myklebust5a37f852012-04-28 14:55:16 -04004904static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004905bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004906{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004907 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004909 return false;
4910 /* Otherwise, request attributes if and only if we don't hold
4911 * a delegation
4912 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004913 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004914}
Fred Isamana69aef12011-03-03 15:13:47 +00004915
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004916static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4917 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004919 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004920
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004921 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4922 hdr->args.bitmask = NULL;
4923 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004924 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004925 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004926
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004927 if (!hdr->pgio_done_cb)
4928 hdr->pgio_done_cb = nfs4_write_done_cb;
4929 hdr->res.server = server;
4930 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004932 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004933 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934}
4935
Fred Isaman0b7c0152012-04-20 14:47:39 -04004936static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4937{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004938 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004939 &data->args.seq_args,
4940 &data->res.seq_res,
4941 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942}
4943
Fred Isaman0b7c0152012-04-20 14:47:39 -04004944static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004947
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004948 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004949 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4950 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004951 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004952 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955}
4956
Fred Isaman0b7c0152012-04-20 14:47:39 -04004957static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004958{
4959 if (!nfs4_sequence_done(task, &data->res.seq_res))
4960 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004961 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004962}
4963
Fred Isaman0b7c0152012-04-20 14:47:39 -04004964static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004966 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004967
Fred Isaman0b7c0152012-04-20 14:47:39 -04004968 if (data->commit_done_cb == NULL)
4969 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004970 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004971 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004972 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
4974
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004975struct nfs4_renewdata {
4976 struct nfs_client *client;
4977 unsigned long timestamp;
4978};
4979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980/*
4981 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4982 * standalone procedure for queueing an asynchronous RENEW.
4983 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004984static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004985{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004986 struct nfs4_renewdata *data = calldata;
4987 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004988
Elena Reshetova212bf412017-10-20 12:53:38 +03004989 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004990 nfs4_schedule_state_renewal(clp);
4991 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004992 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004993}
4994
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004995static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004997 struct nfs4_renewdata *data = calldata;
4998 struct nfs_client *clp = data->client;
4999 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005001 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005002 switch (task->tk_status) {
5003 case 0:
5004 break;
5005 case -NFS4ERR_LEASE_MOVED:
5006 nfs4_schedule_lease_moved_recovery(clp);
5007 break;
5008 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005009 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005010 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5011 return;
5012 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005013 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005014 return;
5015 }
5016 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005018 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019}
5020
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005021static const struct rpc_call_ops nfs4_renew_ops = {
5022 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005023 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005024};
5025
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005026static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
5028 struct rpc_message msg = {
5029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5030 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005031 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005033 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005035 if (renew_flags == 0)
5036 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005037 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005038 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005039 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005040 if (data == NULL) {
5041 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005042 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005043 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005044 data->client = clp;
5045 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005046 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005047 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005050static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051{
5052 struct rpc_message msg = {
5053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5054 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005055 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 };
5057 unsigned long now = jiffies;
5058 int status;
5059
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005060 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 if (status < 0)
5062 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005063 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 return 0;
5065}
5066
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005067static inline int nfs4_server_supports_acls(struct nfs_server *server)
5068{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005069 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005070}
5071
Trond Myklebust21f498c2012-08-24 10:59:25 -04005072/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5073 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005074 * the stack.
5075 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005076#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005077
Neil Hormane9e3d722011-03-04 19:26:03 -05005078static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005079 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005080{
5081 struct page *newpage, **spages;
5082 int rc = 0;
5083 size_t len;
5084 spages = pages;
5085
5086 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005087 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005088 newpage = alloc_page(GFP_KERNEL);
5089
5090 if (newpage == NULL)
5091 goto unwind;
5092 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005093 buf += len;
5094 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005095 *pages++ = newpage;
5096 rc++;
5097 } while (buflen != 0);
5098
5099 return rc;
5100
5101unwind:
5102 for(; rc > 0; rc--)
5103 __free_page(spages[rc-1]);
5104 return -ENOMEM;
5105}
5106
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005107struct nfs4_cached_acl {
5108 int cached;
5109 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005110 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005111};
5112
5113static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005114{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005115 struct nfs_inode *nfsi = NFS_I(inode);
5116
5117 spin_lock(&inode->i_lock);
5118 kfree(nfsi->nfs4_acl);
5119 nfsi->nfs4_acl = acl;
5120 spin_unlock(&inode->i_lock);
5121}
5122
5123static void nfs4_zap_acl_attr(struct inode *inode)
5124{
5125 nfs4_set_cached_acl(inode, NULL);
5126}
5127
5128static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5129{
5130 struct nfs_inode *nfsi = NFS_I(inode);
5131 struct nfs4_cached_acl *acl;
5132 int ret = -ENOENT;
5133
5134 spin_lock(&inode->i_lock);
5135 acl = nfsi->nfs4_acl;
5136 if (acl == NULL)
5137 goto out;
5138 if (buf == NULL) /* user is just asking for length */
5139 goto out_len;
5140 if (acl->cached == 0)
5141 goto out;
5142 ret = -ERANGE; /* see getxattr(2) man page */
5143 if (acl->len > buflen)
5144 goto out;
5145 memcpy(buf, acl->data, acl->len);
5146out_len:
5147 ret = acl->len;
5148out:
5149 spin_unlock(&inode->i_lock);
5150 return ret;
5151}
5152
Sachin Prabhu5794d212012-04-17 14:36:40 +01005153static 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 +00005154{
5155 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005156 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005157
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005158 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005159 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005160 if (acl == NULL)
5161 goto out;
5162 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005163 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005164 } else {
5165 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5166 if (acl == NULL)
5167 goto out;
5168 acl->cached = 0;
5169 }
5170 acl->len = acl_len;
5171out:
5172 nfs4_set_cached_acl(inode, acl);
5173}
5174
Andy Adamsonbf118a32011-12-07 11:55:27 -05005175/*
5176 * The getxattr API returns the required buffer length when called with a
5177 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5178 * the required buf. On a NULL buf, we send a page of data to the server
5179 * guessing that the ACL request can be serviced by a page. If so, we cache
5180 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5181 * the cache. If not so, we throw away the page, and cache the required
5182 * length. The next getxattr call will then produce another round trip to
5183 * the server, this time with the input buf of the required size.
5184 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005185static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005186{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005187 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005188 struct nfs_getaclargs args = {
5189 .fh = NFS_FH(inode),
5190 .acl_pages = pages,
5191 .acl_len = buflen,
5192 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005193 struct nfs_getaclres res = {
5194 .acl_len = buflen,
5195 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005196 struct rpc_message msg = {
5197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5198 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005199 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005200 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005201 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005202 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005203
Trond Myklebust21f498c2012-08-24 10:59:25 -04005204 if (npages > ARRAY_SIZE(pages))
5205 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005206
Andy Adamsonbf118a32011-12-07 11:55:27 -05005207 for (i = 0; i < npages; i++) {
5208 pages[i] = alloc_page(GFP_KERNEL);
5209 if (!pages[i])
5210 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005211 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005212
5213 /* for decoding across pages */
5214 res.acl_scratch = alloc_page(GFP_KERNEL);
5215 if (!res.acl_scratch)
5216 goto out_free;
5217
Andy Adamsonbf118a32011-12-07 11:55:27 -05005218 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005219
Peng Taode040be2012-01-10 22:42:47 +08005220 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005221 __func__, buf, buflen, npages, args.acl_len);
5222 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5223 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005224 if (ret)
5225 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005226
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005227 /* Handle the case where the passed-in buffer is too short */
5228 if (res.acl_flags & NFS4_ACL_TRUNC) {
5229 /* Did the user only issue a request for the acl length? */
5230 if (buf == NULL)
5231 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005232 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005233 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005234 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005235 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005236 if (buf) {
5237 if (res.acl_len > buflen) {
5238 ret = -ERANGE;
5239 goto out_free;
5240 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005241 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005242 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005243out_ok:
5244 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005245out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005246 for (i = 0; i < npages; i++)
5247 if (pages[i])
5248 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005249 if (res.acl_scratch)
5250 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005251 return ret;
5252}
5253
Trond Myklebust16b42892006-08-24 12:27:15 -04005254static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5255{
5256 struct nfs4_exception exception = { };
5257 ssize_t ret;
5258 do {
5259 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005260 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005261 if (ret >= 0)
5262 break;
5263 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5264 } while (exception.retry);
5265 return ret;
5266}
5267
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005268static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5269{
5270 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005271 int ret;
5272
5273 if (!nfs4_server_supports_acls(server))
5274 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005275 ret = nfs_revalidate_inode(server, inode);
5276 if (ret < 0)
5277 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005278 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5279 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005280 ret = nfs4_read_cached_acl(inode, buf, buflen);
5281 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005282 /* -ENOENT is returned if there is no ACL or if there is an ACL
5283 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005284 return ret;
5285 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005286}
5287
Trond Myklebust16b42892006-08-24 12:27:15 -04005288static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005289{
5290 struct nfs_server *server = NFS_SERVER(inode);
5291 struct page *pages[NFS4ACL_MAXPAGES];
5292 struct nfs_setaclargs arg = {
5293 .fh = NFS_FH(inode),
5294 .acl_pages = pages,
5295 .acl_len = buflen,
5296 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005297 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005298 struct rpc_message msg = {
5299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5300 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005301 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005302 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005303 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005304 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005305
5306 if (!nfs4_server_supports_acls(server))
5307 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005308 if (npages > ARRAY_SIZE(pages))
5309 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005310 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005311 if (i < 0)
5312 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005313 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005314 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005315
5316 /*
5317 * Free each page after tx, so the only ref left is
5318 * held by the network stack
5319 */
5320 for (; i > 0; i--)
5321 put_page(pages[i-1]);
5322
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005323 /*
5324 * Acl update can result in inode attribute update.
5325 * so mark the attribute cache invalid.
5326 */
5327 spin_lock(&inode->i_lock);
5328 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5329 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005330 nfs_access_zap_cache(inode);
5331 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005332 return ret;
5333}
5334
Trond Myklebust16b42892006-08-24 12:27:15 -04005335static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5336{
5337 struct nfs4_exception exception = { };
5338 int err;
5339 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005340 err = __nfs4_proc_set_acl(inode, buf, buflen);
5341 trace_nfs4_set_acl(inode, err);
5342 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005343 &exception);
5344 } while (exception.retry);
5345 return err;
5346}
5347
David Quigleyaa9c2662013-05-22 12:50:44 -04005348#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5349static int _nfs4_get_security_label(struct inode *inode, void *buf,
5350 size_t buflen)
5351{
5352 struct nfs_server *server = NFS_SERVER(inode);
5353 struct nfs_fattr fattr;
5354 struct nfs4_label label = {0, 0, buflen, buf};
5355
5356 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005357 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005358 .fh = NFS_FH(inode),
5359 .bitmask = bitmask,
5360 };
5361 struct nfs4_getattr_res res = {
5362 .fattr = &fattr,
5363 .label = &label,
5364 .server = server,
5365 };
5366 struct rpc_message msg = {
5367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005368 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005369 .rpc_resp = &res,
5370 };
5371 int ret;
5372
5373 nfs_fattr_init(&fattr);
5374
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005375 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005376 if (ret)
5377 return ret;
5378 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5379 return -ENOENT;
5380 if (buflen < label.len)
5381 return -ERANGE;
5382 return 0;
5383}
5384
5385static int nfs4_get_security_label(struct inode *inode, void *buf,
5386 size_t buflen)
5387{
5388 struct nfs4_exception exception = { };
5389 int err;
5390
5391 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5392 return -EOPNOTSUPP;
5393
5394 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005395 err = _nfs4_get_security_label(inode, buf, buflen);
5396 trace_nfs4_get_security_label(inode, err);
5397 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005398 &exception);
5399 } while (exception.retry);
5400 return err;
5401}
5402
5403static int _nfs4_do_set_security_label(struct inode *inode,
5404 struct nfs4_label *ilabel,
5405 struct nfs_fattr *fattr,
5406 struct nfs4_label *olabel)
5407{
5408
5409 struct iattr sattr = {0};
5410 struct nfs_server *server = NFS_SERVER(inode);
5411 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005412 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005413 .fh = NFS_FH(inode),
5414 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005415 .server = server,
5416 .bitmask = bitmask,
5417 .label = ilabel,
5418 };
5419 struct nfs_setattrres res = {
5420 .fattr = fattr,
5421 .label = olabel,
5422 .server = server,
5423 };
5424 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5426 .rpc_argp = &arg,
5427 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005428 };
5429 int status;
5430
Jeff Layton12207f62013-11-01 10:49:32 -04005431 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005432
Jeff Layton12207f62013-11-01 10:49:32 -04005433 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005434 if (status)
5435 dprintk("%s failed: %d\n", __func__, status);
5436
5437 return status;
5438}
5439
5440static int nfs4_do_set_security_label(struct inode *inode,
5441 struct nfs4_label *ilabel,
5442 struct nfs_fattr *fattr,
5443 struct nfs4_label *olabel)
5444{
5445 struct nfs4_exception exception = { };
5446 int err;
5447
5448 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005449 err = _nfs4_do_set_security_label(inode, ilabel,
5450 fattr, olabel);
5451 trace_nfs4_set_security_label(inode, err);
5452 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005453 &exception);
5454 } while (exception.retry);
5455 return err;
5456}
5457
5458static int
Al Viro59301222016-05-27 10:19:30 -04005459nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005460{
5461 struct nfs4_label ilabel, *olabel = NULL;
5462 struct nfs_fattr fattr;
5463 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005464 int status;
5465
5466 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5467 return -EOPNOTSUPP;
5468
5469 nfs_fattr_init(&fattr);
5470
5471 ilabel.pi = 0;
5472 ilabel.lfs = 0;
5473 ilabel.label = (char *)buf;
5474 ilabel.len = buflen;
5475
5476 cred = rpc_lookup_cred();
5477 if (IS_ERR(cred))
5478 return PTR_ERR(cred);
5479
5480 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5481 if (IS_ERR(olabel)) {
5482 status = -PTR_ERR(olabel);
5483 goto out;
5484 }
5485
5486 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5487 if (status == 0)
5488 nfs_setsecurity(inode, &fattr, olabel);
5489
5490 nfs4_label_free(olabel);
5491out:
5492 put_rpccred(cred);
5493 return status;
5494}
5495#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5496
5497
Chuck Leverf0920752012-05-21 22:45:41 -04005498static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5499 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005500{
5501 __be32 verf[2];
5502
Chuck Lever2c820d92012-05-21 22:45:33 -04005503 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5504 /* An impossible timestamp guarantees this value
5505 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005506 verf[0] = cpu_to_be32(U32_MAX);
5507 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005508 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005509 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005510 u64 ns = ktime_to_ns(nn->boot_time);
5511
5512 verf[0] = cpu_to_be32(ns >> 32);
5513 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005514 }
Chuck Levercd937102012-03-02 17:14:31 -05005515 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5516}
5517
Jeff Laytona3192682015-06-09 19:43:59 -04005518static int
5519nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005520{
Jeff Laytona3192682015-06-09 19:43:59 -04005521 size_t len;
5522 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005523
Trond Myklebustceb3a162015-01-03 15:16:04 -05005524 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005525 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005526
Jeff Laytona3192682015-06-09 19:43:59 -04005527 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005528 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005529 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5530 1 +
5531 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5532 1;
5533 rcu_read_unlock();
5534
5535 if (len > NFS4_OPAQUE_LIMIT + 1)
5536 return -EINVAL;
5537
5538 /*
5539 * Since this string is allocated at mount time, and held until the
5540 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5541 * about a memory-reclaim deadlock.
5542 */
5543 str = kmalloc(len, GFP_KERNEL);
5544 if (!str)
5545 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005546
Chuck Levere984a552012-09-14 17:24:21 -04005547 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005548 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005549 clp->cl_ipaddr,
5550 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5551 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005552 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005553
Jeff Laytona3192682015-06-09 19:43:59 -04005554 clp->cl_owner_id = str;
5555 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005556}
5557
Jeff Layton873e3852015-06-09 19:44:00 -04005558static int
5559nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005560{
Jeff Layton873e3852015-06-09 19:44:00 -04005561 size_t len;
5562 char *str;
5563
5564 len = 10 + 10 + 1 + 10 + 1 +
5565 strlen(nfs4_client_id_uniquifier) + 1 +
5566 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5567
5568 if (len > NFS4_OPAQUE_LIMIT + 1)
5569 return -EINVAL;
5570
5571 /*
5572 * Since this string is allocated at mount time, and held until the
5573 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5574 * about a memory-reclaim deadlock.
5575 */
5576 str = kmalloc(len, GFP_KERNEL);
5577 if (!str)
5578 return -ENOMEM;
5579
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005580 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005581 clp->rpc_ops->version, clp->cl_minorversion,
5582 nfs4_client_id_uniquifier,
5583 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005584 clp->cl_owner_id = str;
5585 return 0;
5586}
5587
5588static int
5589nfs4_init_uniform_client_string(struct nfs_client *clp)
5590{
Jeff Layton873e3852015-06-09 19:44:00 -04005591 size_t len;
5592 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005593
5594 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005595 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005596
5597 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005598 return nfs4_init_uniquifier_client_string(clp);
5599
5600 len = 10 + 10 + 1 + 10 + 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",
Jeff Layton873e3852015-06-09 19:44:00 -04005616 clp->rpc_ops->version, clp->cl_minorversion,
5617 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005618 clp->cl_owner_id = str;
5619 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005620}
5621
Chuck Lever706cb8d2014-03-12 12:51:47 -04005622/*
5623 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5624 * services. Advertise one based on the address family of the
5625 * clientaddr.
5626 */
5627static unsigned int
5628nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5629{
5630 if (strchr(clp->cl_ipaddr, ':') != NULL)
5631 return scnprintf(buf, len, "tcp6");
5632 else
5633 return scnprintf(buf, len, "tcp");
5634}
5635
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005636static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5637{
5638 struct nfs4_setclientid *sc = calldata;
5639
5640 if (task->tk_status == 0)
5641 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5642}
5643
5644static const struct rpc_call_ops nfs4_setclientid_ops = {
5645 .rpc_call_done = nfs4_setclientid_done,
5646};
5647
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005648/**
5649 * nfs4_proc_setclientid - Negotiate client ID
5650 * @clp: state data structure
5651 * @program: RPC program for NFSv4 callback service
5652 * @port: IP port number for NFS4 callback service
5653 * @cred: RPC credential to use for this call
5654 * @res: where to place the result
5655 *
5656 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5657 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005658int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5659 unsigned short port, struct rpc_cred *cred,
5660 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
5662 nfs4_verifier sc_verifier;
5663 struct nfs4_setclientid setclientid = {
5664 .sc_verifier = &sc_verifier,
5665 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005666 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 };
5668 struct rpc_message msg = {
5669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5670 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005671 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005672 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005674 struct rpc_task *task;
5675 struct rpc_task_setup task_setup_data = {
5676 .rpc_client = clp->cl_rpcclient,
5677 .rpc_message = &msg,
5678 .callback_ops = &nfs4_setclientid_ops,
5679 .callback_data = &setclientid,
5680 .flags = RPC_TASK_TIMEOUT,
5681 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005682 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Chuck Leverde734832012-07-11 16:30:50 -04005684 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005685 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005686
5687 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5688 status = nfs4_init_uniform_client_string(clp);
5689 else
Jeff Laytona3192682015-06-09 19:43:59 -04005690 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005691
5692 if (status)
5693 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005694
Chuck Leverde734832012-07-11 16:30:50 -04005695 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005696 setclientid.sc_netid_len =
5697 nfs4_init_callback_netid(clp,
5698 setclientid.sc_netid,
5699 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005700 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005701 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 clp->cl_ipaddr, port >> 8, port & 255);
5703
Jeff Layton3a6bb732015-06-09 19:43:57 -04005704 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005705 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005706 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005707 task = rpc_run_task(&task_setup_data);
5708 if (IS_ERR(task)) {
5709 status = PTR_ERR(task);
5710 goto out;
5711 }
5712 status = task->tk_status;
5713 if (setclientid.sc_cred) {
5714 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5715 put_rpccred(setclientid.sc_cred);
5716 }
5717 rpc_put_task(task);
5718out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005719 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005720 dprintk("NFS reply setclientid: %d\n", status);
5721 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722}
5723
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005724/**
5725 * nfs4_proc_setclientid_confirm - Confirm client ID
5726 * @clp: state data structure
5727 * @res: result of a previous SETCLIENTID
5728 * @cred: RPC credential to use for this call
5729 *
5730 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5731 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005732int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005733 struct nfs4_setclientid_res *arg,
5734 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 struct rpc_message msg = {
5737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005738 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005739 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 int status;
5742
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005743 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5744 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5745 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005746 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005747 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005748 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 return status;
5750}
5751
Trond Myklebustfe650402006-01-03 09:55:18 +01005752struct nfs4_delegreturndata {
5753 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005754 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005755 struct nfs_fh fh;
5756 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005757 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005758 struct {
5759 struct nfs4_layoutreturn_args arg;
5760 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005761 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005762 u32 roc_barrier;
5763 bool roc;
5764 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005765 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005766 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005767 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005768};
5769
Trond Myklebustfe650402006-01-03 09:55:18 +01005770static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5771{
5772 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005773 struct nfs4_exception exception = {
5774 .inode = data->inode,
5775 .stateid = &data->stateid,
5776 };
Andy Adamson938e1012009-04-01 09:22:28 -04005777
Trond Myklebust14516c32010-07-31 14:29:06 -04005778 if (!nfs4_sequence_done(task, &data->res.seq_res))
5779 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005780
Trond Myklebustca8acf82013-08-13 10:36:56 -04005781 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005782
5783 /* Handle Layoutreturn errors */
5784 if (data->args.lr_args && task->tk_status != 0) {
5785 switch(data->res.lr_ret) {
5786 default:
5787 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5788 break;
5789 case 0:
5790 data->args.lr_args = NULL;
5791 data->res.lr_res = NULL;
5792 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005793 case -NFS4ERR_OLD_STATEID:
5794 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005795 data->inode))
5796 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005797 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005798 case -NFS4ERR_ADMIN_REVOKED:
5799 case -NFS4ERR_DELEG_REVOKED:
5800 case -NFS4ERR_EXPIRED:
5801 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005802 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5803 case -NFS4ERR_WRONG_CRED:
5804 data->args.lr_args = NULL;
5805 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005806 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005807 }
5808 }
5809
Ricardo Labiaga79708862009-12-07 09:23:21 -05005810 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005811 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005812 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005813 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005814 case -NFS4ERR_ADMIN_REVOKED:
5815 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005816 case -NFS4ERR_EXPIRED:
5817 nfs4_free_revoked_stateid(data->res.server,
5818 data->args.stateid,
5819 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005820 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005821 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005822 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005823 task->tk_status = 0;
5824 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005825 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005826 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5827 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005828 task->tk_status = 0;
5829 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005830 case -NFS4ERR_ACCESS:
5831 if (data->args.bitmask) {
5832 data->args.bitmask = NULL;
5833 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005834 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005835 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005836 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005837 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005838 task->tk_status = nfs4_async_handle_exception(task,
5839 data->res.server, task->tk_status,
5840 &exception);
5841 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005842 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005843 }
5844 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005845 return;
5846lr_restart:
5847 data->res.lr_ret = 0;
5848out_restart:
5849 task->tk_status = 0;
5850 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005851}
5852
5853static void nfs4_delegreturn_release(void *calldata)
5854{
Peng Tao039b7562014-07-03 13:05:02 +08005855 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005856 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005857
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005858 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005859 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005860 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5861 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005862 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005863 nfs_iput_and_deactive(inode);
5864 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005865 kfree(calldata);
5866}
5867
Andy Adamson938e1012009-04-01 09:22:28 -04005868static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5869{
5870 struct nfs4_delegreturndata *d_data;
5871
5872 d_data = (struct nfs4_delegreturndata *)data;
5873
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005874 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005875 return;
5876
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005877 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005878 &d_data->args.seq_args,
5879 &d_data->res.seq_res,
5880 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005881}
Andy Adamson938e1012009-04-01 09:22:28 -04005882
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005883static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005884 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005885 .rpc_call_done = nfs4_delegreturn_done,
5886 .rpc_release = nfs4_delegreturn_release,
5887};
5888
Trond Myklebuste6f81072008-01-24 18:14:34 -05005889static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005890{
5891 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005892 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005893 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005894 struct rpc_message msg = {
5895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5896 .rpc_cred = cred,
5897 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005898 struct rpc_task_setup task_setup_data = {
5899 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005900 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005901 .callback_ops = &nfs4_delegreturn_ops,
5902 .flags = RPC_TASK_ASYNC,
5903 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005904 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005905
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005906 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005907 if (data == NULL)
5908 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005909 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005910
5911 nfs4_state_protect(server->nfs_client,
5912 NFS_SP4_MACH_CRED_CLEANUP,
5913 &task_setup_data.rpc_client, &msg);
5914
Trond Myklebustfe650402006-01-03 09:55:18 +01005915 data->args.fhandle = &data->fh;
5916 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005917 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005918 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005919 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005920 data->res.fattr = &data->fattr;
5921 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005922 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005923 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005924 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005925 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005926 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005927 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005928 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005929 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005930 if (data->lr.roc) {
5931 data->args.lr_args = &data->lr.arg;
5932 data->res.lr_res = &data->lr.res;
5933 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005934 } else if (data->lr.roc) {
5935 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5936 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005937 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005938
Trond Myklebustc970aa82007-07-14 15:39:59 -04005939 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005940 msg.rpc_argp = &data->args;
5941 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005942 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005943 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005944 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005945 if (!issync)
5946 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005947 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005948 if (status != 0)
5949 goto out;
5950 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005951out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005952 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005953 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954}
5955
Trond Myklebuste6f81072008-01-24 18:14:34 -05005956int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957{
5958 struct nfs_server *server = NFS_SERVER(inode);
5959 struct nfs4_exception exception = { };
5960 int err;
5961 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005962 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005963 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 switch (err) {
5965 case -NFS4ERR_STALE_STATEID:
5966 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 case 0:
5968 return 0;
5969 }
5970 err = nfs4_handle_exception(server, err, &exception);
5971 } while (exception.retry);
5972 return err;
5973}
5974
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5976{
5977 struct inode *inode = state->inode;
5978 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005979 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005980 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005982 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005984 struct nfs_lockt_res res = {
5985 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 };
5987 struct rpc_message msg = {
5988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005989 .rpc_argp = &arg,
5990 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 .rpc_cred = state->owner->so_cred,
5992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 struct nfs4_lock_state *lsp;
5994 int status;
5995
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005996 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005997 status = nfs4_set_lock_state(state, request);
5998 if (status != 0)
5999 goto out;
6000 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006001 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006002 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006003 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006004 switch (status) {
6005 case 0:
6006 request->fl_type = F_UNLCK;
6007 break;
6008 case -NFS4ERR_DENIED:
6009 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006011 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006012 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006013out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 return status;
6015}
6016
6017static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6018{
6019 struct nfs4_exception exception = { };
6020 int err;
6021
6022 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006023 err = _nfs4_proc_getlk(state, cmd, request);
6024 trace_nfs4_get_lock(request, state, cmd, err);
6025 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 &exception);
6027 } while (exception.retry);
6028 return err;
6029}
6030
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006031struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006032 struct nfs_locku_args arg;
6033 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006034 struct nfs4_lock_state *lsp;
6035 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006036 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006037 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006038 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006039 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006040};
6041
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006042static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6043 struct nfs_open_context *ctx,
6044 struct nfs4_lock_state *lsp,
6045 struct nfs_seqid *seqid)
6046{
6047 struct nfs4_unlockdata *p;
6048 struct inode *inode = lsp->ls_state->inode;
6049
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006050 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006051 if (p == NULL)
6052 return NULL;
6053 p->arg.fh = NFS_FH(inode);
6054 p->arg.fl = &p->fl;
6055 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006056 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006057 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006058 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006059 /* Ensure we don't close file until we're done freeing locks! */
6060 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006061 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006062 memcpy(&p->fl, fl, sizeof(p->fl));
6063 p->server = NFS_SERVER(inode);
6064 return p;
6065}
6066
Trond Myklebust06f814a2006-01-03 09:55:07 +01006067static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006068{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006069 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006070 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006071 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006072 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006073 put_nfs_open_context(calldata->ctx);
6074 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006075}
6076
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006077static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006078{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006079 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006080 struct nfs4_exception exception = {
6081 .inode = calldata->lsp->ls_state->inode,
6082 .stateid = &calldata->arg.stateid,
6083 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006084
Trond Myklebust14516c32010-07-31 14:29:06 -04006085 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6086 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006087 switch (task->tk_status) {
6088 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006089 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006090 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006091 if (nfs4_update_lock_stateid(calldata->lsp,
6092 &calldata->res.stateid))
6093 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006094 case -NFS4ERR_ADMIN_REVOKED:
6095 case -NFS4ERR_EXPIRED:
6096 nfs4_free_revoked_stateid(calldata->server,
6097 &calldata->arg.stateid,
6098 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006099 case -NFS4ERR_BAD_STATEID:
6100 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006101 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006102 if (!nfs4_stateid_match(&calldata->arg.stateid,
6103 &calldata->lsp->ls_stateid))
6104 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006105 break;
6106 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006107 task->tk_status = nfs4_async_handle_exception(task,
6108 calldata->server, task->tk_status,
6109 &exception);
6110 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006111 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006112 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006113 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006114}
6115
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006116static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006117{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006118 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006120 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6121 nfs_async_iocounter_wait(task, calldata->l_ctx))
6122 return;
6123
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006124 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006125 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006126 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006127 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006128 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006129 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006130 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006131 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006132 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006133 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006134 &calldata->res.seq_res,
6135 task) != 0)
6136 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006137 return;
6138out_no_action:
6139 task->tk_action = NULL;
6140out_wait:
6141 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142}
6143
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006144static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006145 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006146 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006147 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006148};
6149
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006150static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6151 struct nfs_open_context *ctx,
6152 struct nfs4_lock_state *lsp,
6153 struct nfs_seqid *seqid)
6154{
6155 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006156 struct rpc_message msg = {
6157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6158 .rpc_cred = ctx->cred,
6159 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006160 struct rpc_task_setup task_setup_data = {
6161 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006162 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006163 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006164 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006165 .flags = RPC_TASK_ASYNC,
6166 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006167
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006168 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6169 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6170
Frank Filz137d6ac2007-07-09 15:32:29 -07006171 /* Ensure this is an unlock - when canceling a lock, the
6172 * canceled lock is passed in, and it won't be an unlock.
6173 */
6174 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006175 if (fl->fl_flags & FL_CLOSE)
6176 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006177
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006178 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6179 if (data == NULL) {
6180 nfs_free_seqid(seqid);
6181 return ERR_PTR(-ENOMEM);
6182 }
6183
Chuck Levera9c92d62013-08-09 12:48:18 -04006184 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006185 msg.rpc_argp = &data->arg;
6186 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006187 task_setup_data.callback_data = data;
6188 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006189}
6190
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6192{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006193 struct inode *inode = state->inode;
6194 struct nfs4_state_owner *sp = state->owner;
6195 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006196 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006197 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006198 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006199 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006200 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006201 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202
Trond Myklebust9b073572006-06-29 16:38:34 -04006203 status = nfs4_set_lock_state(state, request);
6204 /* Unlock _before_ we do the RPC call */
6205 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006206 /* Exclude nfs_delegation_claim_locks() */
6207 mutex_lock(&sp->so_delegreturn_mutex);
6208 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006209 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006210 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006211 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006212 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006213 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006214 }
6215 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006216 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006217 if (status != 0)
6218 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006219 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006220 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006221 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6222 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006223 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6224 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006225 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006226 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006227 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006228 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006229 status = PTR_ERR(task);
6230 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006231 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006232 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006233 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006234out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006235 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006236 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006237 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238}
6239
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006240struct nfs4_lockdata {
6241 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006242 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006243 struct nfs4_lock_state *lsp;
6244 struct nfs_open_context *ctx;
6245 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006246 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006247 int rpc_status;
6248 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006249 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006250};
6251
6252static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006253 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6254 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006255{
6256 struct nfs4_lockdata *p;
6257 struct inode *inode = lsp->ls_state->inode;
6258 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006259 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006260
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006261 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006262 if (p == NULL)
6263 return NULL;
6264
6265 p->arg.fh = NFS_FH(inode);
6266 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006267 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006268 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006269 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006270 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6271 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006272 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006273 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006274 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006275 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006276 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006277 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006278 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006279 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006280 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006281 p->ctx = get_nfs_open_context(ctx);
6282 memcpy(&p->fl, fl, sizeof(p->fl));
6283 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006284out_free_seqid:
6285 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006286out_free:
6287 kfree(p);
6288 return NULL;
6289}
6290
6291static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6292{
6293 struct nfs4_lockdata *data = calldata;
6294 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
Harvey Harrison3110ff82008-05-02 13:42:44 -07006296 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006297 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006298 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006299 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006300 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006301 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006302 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006303 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006304 nfs4_stateid_copy(&data->arg.open_stateid,
6305 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006306 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006307 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006308 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006309 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006310 nfs4_stateid_copy(&data->arg.lock_stateid,
6311 &data->lsp->ls_stateid);
6312 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006313 if (!nfs4_valid_open_stateid(state)) {
6314 data->rpc_status = -EBADF;
6315 task->tk_action = NULL;
6316 goto out_release_open_seqid;
6317 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006318 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006319 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006320 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006321 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006322 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006323 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006324out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006325 nfs_release_seqid(data->arg.open_seqid);
6326out_release_lock_seqid:
6327 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006328out_wait:
6329 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006330 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006331}
6332
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006333static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6334{
6335 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006336 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006337
Harvey Harrison3110ff82008-05-02 13:42:44 -07006338 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006339
Trond Myklebust14516c32010-07-31 14:29:06 -04006340 if (!nfs4_sequence_done(task, &data->res.seq_res))
6341 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006342
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006343 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006344 switch (task->tk_status) {
6345 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006346 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006347 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006348 if (data->arg.new_lock) {
6349 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006350 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006351 rpc_restart_call_prepare(task);
6352 break;
6353 }
6354 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006355 if (data->arg.new_lock_owner != 0) {
6356 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6357 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6358 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6359 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6360 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006361 break;
6362 case -NFS4ERR_BAD_STATEID:
6363 case -NFS4ERR_OLD_STATEID:
6364 case -NFS4ERR_STALE_STATEID:
6365 case -NFS4ERR_EXPIRED:
6366 if (data->arg.new_lock_owner != 0) {
6367 if (!nfs4_stateid_match(&data->arg.open_stateid,
6368 &lsp->ls_state->open_stateid))
6369 rpc_restart_call_prepare(task);
6370 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6371 &lsp->ls_stateid))
6372 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006373 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006374 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006375}
6376
6377static void nfs4_lock_release(void *calldata)
6378{
6379 struct nfs4_lockdata *data = calldata;
6380
Harvey Harrison3110ff82008-05-02 13:42:44 -07006381 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006382 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006383 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006384 struct rpc_task *task;
6385 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6386 data->arg.lock_seqid);
6387 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006388 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006389 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006390 } else
6391 nfs_free_seqid(data->arg.lock_seqid);
6392 nfs4_put_lock_state(data->lsp);
6393 put_nfs_open_context(data->ctx);
6394 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006395 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006396}
6397
6398static const struct rpc_call_ops nfs4_lock_ops = {
6399 .rpc_call_prepare = nfs4_lock_prepare,
6400 .rpc_call_done = nfs4_lock_done,
6401 .rpc_release = nfs4_lock_release,
6402};
6403
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006404static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6405{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006406 switch (error) {
6407 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006408 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006409 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006410 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006411 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006412 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006413 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006414 break;
6415 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006416 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006417 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006418 };
6419}
6420
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006421static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006422{
6423 struct nfs4_lockdata *data;
6424 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006425 struct rpc_message msg = {
6426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6427 .rpc_cred = state->owner->so_cred,
6428 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006429 struct rpc_task_setup task_setup_data = {
6430 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006431 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006432 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006433 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006434 .flags = RPC_TASK_ASYNC,
6435 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006436 int ret;
6437
Harvey Harrison3110ff82008-05-02 13:42:44 -07006438 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006439 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006440 fl->fl_u.nfs4_fl.owner,
6441 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006442 if (data == NULL)
6443 return -ENOMEM;
6444 if (IS_SETLKW(cmd))
6445 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006446 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006447 msg.rpc_argp = &data->arg;
6448 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006449 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006450 if (recovery_type > NFS_LOCK_NEW) {
6451 if (recovery_type == NFS_LOCK_RECLAIM)
6452 data->arg.reclaim = NFS_LOCK_RECLAIM;
6453 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006454 } else
6455 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006456 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006457 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006458 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006459 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006460 if (ret == 0) {
6461 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006462 if (ret)
6463 nfs4_handle_setlk_error(data->server, data->lsp,
6464 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006465 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006466 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006467 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006468 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006469 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471}
6472
6473static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6474{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006475 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006476 struct nfs4_exception exception = {
6477 .inode = state->inode,
6478 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006479 int err;
6480
6481 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006482 /* Cache the lock if possible... */
6483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6484 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006485 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006486 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006487 break;
6488 nfs4_handle_exception(server, err, &exception);
6489 } while (exception.retry);
6490 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491}
6492
6493static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6494{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006495 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006496 struct nfs4_exception exception = {
6497 .inode = state->inode,
6498 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006499 int err;
6500
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006501 err = nfs4_set_lock_state(state, request);
6502 if (err != 0)
6503 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006504 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006505 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6506 return 0;
6507 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006508 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006509 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6510 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006511 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006512 switch (err) {
6513 default:
6514 goto out;
6515 case -NFS4ERR_GRACE:
6516 case -NFS4ERR_DELAY:
6517 nfs4_handle_exception(server, err, &exception);
6518 err = 0;
6519 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006520 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006521out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523}
6524
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006525#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006526static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6527{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006528 struct nfs4_lock_state *lsp;
6529 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006530
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006531 status = nfs4_set_lock_state(state, request);
6532 if (status != 0)
6533 return status;
6534 lsp = request->fl_u.nfs4_fl.owner;
6535 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6536 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6537 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006538 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006539}
6540#endif
6541
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6543{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006544 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006545 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006546 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006547 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548
Trond Myklebust01c3b862006-06-29 16:38:39 -04006549 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006550 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006551 if (status < 0)
6552 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006553 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006554 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006555 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006556 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006557 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006558 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006559 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006560 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006561 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006562 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006563 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006564 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006565 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006566 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006567out:
6568 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 return status;
6570}
6571
6572static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6573{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006574 struct nfs4_exception exception = {
6575 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006576 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 int err;
6579
6580 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006581 err = _nfs4_proc_setlk(state, cmd, request);
6582 if (err == -NFS4ERR_DENIED)
6583 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006585 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 } while (exception.retry);
6587 return err;
6588}
6589
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006590#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6591#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6592
6593static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006594nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6595 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006596{
6597 int status = -ERESTARTSYS;
6598 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6599
6600 while(!signalled()) {
6601 status = nfs4_proc_setlk(state, cmd, request);
6602 if ((status != -EAGAIN) || IS_SETLK(cmd))
6603 break;
6604 freezable_schedule_timeout_interruptible(timeout);
6605 timeout *= 2;
6606 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6607 status = -ERESTARTSYS;
6608 }
6609 return status;
6610}
6611
Jeff Laytona1d617d82016-09-17 18:17:39 -04006612#ifdef CONFIG_NFS_V4_1
6613struct nfs4_lock_waiter {
6614 struct task_struct *task;
6615 struct inode *inode;
6616 struct nfs_lowner *owner;
6617 bool notified;
6618};
6619
6620static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006621nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006622{
6623 int ret;
6624 struct cb_notify_lock_args *cbnl = key;
6625 struct nfs4_lock_waiter *waiter = wait->private;
6626 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6627 *wowner = waiter->owner;
6628
6629 /* Only wake if the callback was for the same owner */
6630 if (lowner->clientid != wowner->clientid ||
6631 lowner->id != wowner->id ||
6632 lowner->s_dev != wowner->s_dev)
6633 return 0;
6634
6635 /* Make sure it's for the right inode */
6636 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6637 return 0;
6638
6639 waiter->notified = true;
6640
6641 /* override "private" so we can use default_wake_function */
6642 wait->private = waiter->task;
6643 ret = autoremove_wake_function(wait, mode, flags, key);
6644 wait->private = waiter;
6645 return ret;
6646}
6647
6648static int
6649nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6650{
6651 int status = -ERESTARTSYS;
6652 unsigned long flags;
6653 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6654 struct nfs_server *server = NFS_SERVER(state->inode);
6655 struct nfs_client *clp = server->nfs_client;
6656 wait_queue_head_t *q = &clp->cl_lock_waitq;
6657 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6658 .id = lsp->ls_seqid.owner_id,
6659 .s_dev = server->s_dev };
6660 struct nfs4_lock_waiter waiter = { .task = current,
6661 .inode = state->inode,
6662 .owner = &owner,
6663 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006664 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006665
6666 /* Don't bother with waitqueue if we don't expect a callback */
6667 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6668 return nfs4_retry_setlk_simple(state, cmd, request);
6669
6670 init_wait(&wait);
6671 wait.private = &waiter;
6672 wait.func = nfs4_wake_lock_waiter;
6673 add_wait_queue(q, &wait);
6674
6675 while(!signalled()) {
6676 status = nfs4_proc_setlk(state, cmd, request);
6677 if ((status != -EAGAIN) || IS_SETLK(cmd))
6678 break;
6679
6680 status = -ERESTARTSYS;
6681 spin_lock_irqsave(&q->lock, flags);
6682 if (waiter.notified) {
6683 spin_unlock_irqrestore(&q->lock, flags);
6684 continue;
6685 }
6686 set_current_state(TASK_INTERRUPTIBLE);
6687 spin_unlock_irqrestore(&q->lock, flags);
6688
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006689 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006690 }
6691
6692 finish_wait(q, &wait);
6693 return status;
6694}
6695#else /* !CONFIG_NFS_V4_1 */
6696static inline int
6697nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6698{
6699 return nfs4_retry_setlk_simple(state, cmd, request);
6700}
6701#endif
6702
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703static int
6704nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6705{
6706 struct nfs_open_context *ctx;
6707 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 int status;
6709
6710 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006711 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 state = ctx->state;
6713
Trond Myklebustd9531262009-07-21 19:22:38 -04006714 if (IS_GETLK(cmd)) {
6715 if (state != NULL)
6716 return nfs4_proc_getlk(state, F_GETLK, request);
6717 return 0;
6718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
6720 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6721 return -EINVAL;
6722
Trond Myklebustd9531262009-07-21 19:22:38 -04006723 if (request->fl_type == F_UNLCK) {
6724 if (state != NULL)
6725 return nfs4_proc_unlck(state, cmd, request);
6726 return 0;
6727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
Trond Myklebustd9531262009-07-21 19:22:38 -04006729 if (state == NULL)
6730 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006731
6732 if ((request->fl_flags & FL_POSIX) &&
6733 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6734 return -ENOLCK;
6735
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006736 /*
6737 * Don't rely on the VFS having checked the file open mode,
6738 * since it won't do this for flock() locks.
6739 */
6740 switch (request->fl_type) {
6741 case F_RDLCK:
6742 if (!(filp->f_mode & FMODE_READ))
6743 return -EBADF;
6744 break;
6745 case F_WRLCK:
6746 if (!(filp->f_mode & FMODE_WRITE))
6747 return -EBADF;
6748 }
6749
Jeff Layton1ea67db2016-09-17 18:17:37 -04006750 status = nfs4_set_lock_state(state, request);
6751 if (status != 0)
6752 return status;
6753
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006754 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755}
6756
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006757int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006758{
6759 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006760 int err;
6761
6762 err = nfs4_set_lock_state(state, fl);
6763 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006764 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006765 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006766 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006767}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006768
Trond Myklebustcf470c32012-03-07 13:49:12 -05006769struct nfs_release_lockowner_data {
6770 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006771 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006772 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006773 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006774 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006775};
6776
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006777static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6778{
6779 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006780 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006781 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6782 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006783 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006784 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006785}
6786
6787static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6788{
6789 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006790 struct nfs_server *server = data->server;
6791
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006792 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006793
6794 switch (task->tk_status) {
6795 case 0:
6796 renew_lease(server, data->timestamp);
6797 break;
6798 case -NFS4ERR_STALE_CLIENTID:
6799 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006800 nfs4_schedule_lease_recovery(server->nfs_client);
6801 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006802 case -NFS4ERR_LEASE_MOVED:
6803 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006804 if (nfs4_async_handle_error(task, server,
6805 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006806 rpc_restart_call_prepare(task);
6807 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006808}
6809
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006810static void nfs4_release_lockowner_release(void *calldata)
6811{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006812 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006813 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006814 kfree(calldata);
6815}
6816
Trond Myklebust17280172012-03-11 13:11:00 -04006817static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006818 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6819 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006820 .rpc_release = nfs4_release_lockowner_release,
6821};
6822
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006823static void
6824nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006825{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006826 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006827 struct rpc_message msg = {
6828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6829 };
6830
6831 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006832 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006833
Trond Myklebustcf470c32012-03-07 13:49:12 -05006834 data = kmalloc(sizeof(*data), GFP_NOFS);
6835 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006836 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006837 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006838 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006839 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6840 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6841 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006842
Trond Myklebustcf470c32012-03-07 13:49:12 -05006843 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006844 msg.rpc_resp = &data->res;
6845 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006846 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006847}
6848
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006849#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6850
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006851static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006852 struct dentry *unused, struct inode *inode,
6853 const char *key, const void *buf,
6854 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006855{
Al Viro59301222016-05-27 10:19:30 -04006856 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006857}
6858
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006859static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006860 struct dentry *unused, struct inode *inode,
6861 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006862{
Al Virob2968212016-04-10 20:48:24 -04006863 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006864}
6865
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006866static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006867{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006868 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006869}
6870
David Quigleyc9bccef2013-05-22 12:50:45 -04006871#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006872
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006873static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006874 struct dentry *unused, struct inode *inode,
6875 const char *key, const void *buf,
6876 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006877{
6878 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006879 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006880
6881 return -EOPNOTSUPP;
6882}
6883
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006884static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006885 struct dentry *unused, struct inode *inode,
6886 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006887{
6888 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006889 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006890 return -EOPNOTSUPP;
6891}
6892
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006893static ssize_t
6894nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006895{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006896 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006897
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006898 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6899 len = security_inode_listsecurity(inode, list, list_len);
6900 if (list_len && len > list_len)
6901 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006902 }
6903 return len;
6904}
6905
6906static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6907 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006908 .get = nfs4_xattr_get_nfs4_label,
6909 .set = nfs4_xattr_set_nfs4_label,
6910};
David Quigleyc9bccef2013-05-22 12:50:45 -04006911
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006912#else
6913
6914static ssize_t
6915nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6916{
6917 return 0;
6918}
6919
6920#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006921
Andy Adamson533eb462011-06-13 18:25:56 -04006922/*
6923 * nfs_fhget will use either the mounted_on_fileid or the fileid
6924 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006925static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6926{
Andy Adamson533eb462011-06-13 18:25:56 -04006927 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6928 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6929 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006930 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006931 return;
6932
6933 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006934 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006935 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6936 fattr->nlink = 2;
6937}
6938
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006939static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6940 const struct qstr *name,
6941 struct nfs4_fs_locations *fs_locations,
6942 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006943{
6944 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006945 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006946 struct nfs4_fs_locations_arg args = {
6947 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006948 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006949 .page = page,
6950 .bitmask = bitmask,
6951 };
Benny Halevy22958462009-04-01 09:22:02 -04006952 struct nfs4_fs_locations_res res = {
6953 .fs_locations = fs_locations,
6954 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006955 struct rpc_message msg = {
6956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6957 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006958 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006959 };
6960 int status;
6961
Harvey Harrison3110ff82008-05-02 13:42:44 -07006962 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006963
Chuck Leverc05cefc2017-11-05 15:45:22 -05006964 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6965 bitmask[1] = nfs4_fattr_bitmap[1];
6966
Andy Adamson533eb462011-06-13 18:25:56 -04006967 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6968 * is not supported */
6969 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05006970 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006971 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05006972 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006973
Trond Myklebustc228fd32007-01-13 02:28:11 -05006974 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006975 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006976 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006977 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006978 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006979 return status;
6980}
6981
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006982int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6983 const struct qstr *name,
6984 struct nfs4_fs_locations *fs_locations,
6985 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006986{
6987 struct nfs4_exception exception = { };
6988 int err;
6989 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006990 err = _nfs4_proc_fs_locations(client, dir, name,
6991 fs_locations, page);
6992 trace_nfs4_get_fs_locations(dir, name, err);
6993 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006994 &exception);
6995 } while (exception.retry);
6996 return err;
6997}
6998
Chuck Leverb03d7352013-10-17 14:12:50 -04006999/*
7000 * This operation also signals the server that this client is
7001 * performing migration recovery. The server can stop returning
7002 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7003 * appended to this compound to identify the client ID which is
7004 * performing recovery.
7005 */
7006static int _nfs40_proc_get_locations(struct inode *inode,
7007 struct nfs4_fs_locations *locations,
7008 struct page *page, struct rpc_cred *cred)
7009{
7010 struct nfs_server *server = NFS_SERVER(inode);
7011 struct rpc_clnt *clnt = server->client;
7012 u32 bitmask[2] = {
7013 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7014 };
7015 struct nfs4_fs_locations_arg args = {
7016 .clientid = server->nfs_client->cl_clientid,
7017 .fh = NFS_FH(inode),
7018 .page = page,
7019 .bitmask = bitmask,
7020 .migration = 1, /* skip LOOKUP */
7021 .renew = 1, /* append RENEW */
7022 };
7023 struct nfs4_fs_locations_res res = {
7024 .fs_locations = locations,
7025 .migration = 1,
7026 .renew = 1,
7027 };
7028 struct rpc_message msg = {
7029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7030 .rpc_argp = &args,
7031 .rpc_resp = &res,
7032 .rpc_cred = cred,
7033 };
7034 unsigned long now = jiffies;
7035 int status;
7036
7037 nfs_fattr_init(&locations->fattr);
7038 locations->server = server;
7039 locations->nlocations = 0;
7040
7041 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7042 nfs4_set_sequence_privileged(&args.seq_args);
7043 status = nfs4_call_sync_sequence(clnt, server, &msg,
7044 &args.seq_args, &res.seq_res);
7045 if (status)
7046 return status;
7047
7048 renew_lease(server, now);
7049 return 0;
7050}
7051
7052#ifdef CONFIG_NFS_V4_1
7053
7054/*
7055 * This operation also signals the server that this client is
7056 * performing migration recovery. The server can stop asserting
7057 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7058 * performing this operation is identified in the SEQUENCE
7059 * operation in this compound.
7060 *
7061 * When the client supports GETATTR(fs_locations_info), it can
7062 * be plumbed in here.
7063 */
7064static int _nfs41_proc_get_locations(struct inode *inode,
7065 struct nfs4_fs_locations *locations,
7066 struct page *page, struct rpc_cred *cred)
7067{
7068 struct nfs_server *server = NFS_SERVER(inode);
7069 struct rpc_clnt *clnt = server->client;
7070 u32 bitmask[2] = {
7071 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7072 };
7073 struct nfs4_fs_locations_arg args = {
7074 .fh = NFS_FH(inode),
7075 .page = page,
7076 .bitmask = bitmask,
7077 .migration = 1, /* skip LOOKUP */
7078 };
7079 struct nfs4_fs_locations_res res = {
7080 .fs_locations = locations,
7081 .migration = 1,
7082 };
7083 struct rpc_message msg = {
7084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7085 .rpc_argp = &args,
7086 .rpc_resp = &res,
7087 .rpc_cred = cred,
7088 };
7089 int status;
7090
7091 nfs_fattr_init(&locations->fattr);
7092 locations->server = server;
7093 locations->nlocations = 0;
7094
7095 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7096 nfs4_set_sequence_privileged(&args.seq_args);
7097 status = nfs4_call_sync_sequence(clnt, server, &msg,
7098 &args.seq_args, &res.seq_res);
7099 if (status == NFS4_OK &&
7100 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7101 status = -NFS4ERR_LEASE_MOVED;
7102 return status;
7103}
7104
7105#endif /* CONFIG_NFS_V4_1 */
7106
7107/**
7108 * nfs4_proc_get_locations - discover locations for a migrated FSID
7109 * @inode: inode on FSID that is migrating
7110 * @locations: result of query
7111 * @page: buffer
7112 * @cred: credential to use for this operation
7113 *
7114 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7115 * operation failed, or a negative errno if a local error occurred.
7116 *
7117 * On success, "locations" is filled in, but if the server has
7118 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7119 * asserted.
7120 *
7121 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7122 * from this client that require migration recovery.
7123 */
7124int nfs4_proc_get_locations(struct inode *inode,
7125 struct nfs4_fs_locations *locations,
7126 struct page *page, struct rpc_cred *cred)
7127{
7128 struct nfs_server *server = NFS_SERVER(inode);
7129 struct nfs_client *clp = server->nfs_client;
7130 const struct nfs4_mig_recovery_ops *ops =
7131 clp->cl_mvops->mig_recovery_ops;
7132 struct nfs4_exception exception = { };
7133 int status;
7134
7135 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7136 (unsigned long long)server->fsid.major,
7137 (unsigned long long)server->fsid.minor,
7138 clp->cl_hostname);
7139 nfs_display_fhandle(NFS_FH(inode), __func__);
7140
7141 do {
7142 status = ops->get_locations(inode, locations, page, cred);
7143 if (status != -NFS4ERR_DELAY)
7144 break;
7145 nfs4_handle_exception(server, status, &exception);
7146 } while (exception.retry);
7147 return status;
7148}
7149
Chuck Lever44c99932013-10-17 14:13:30 -04007150/*
7151 * This operation also signals the server that this client is
7152 * performing "lease moved" recovery. The server can stop
7153 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7154 * is appended to this compound to identify the client ID which is
7155 * performing recovery.
7156 */
7157static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7158{
7159 struct nfs_server *server = NFS_SERVER(inode);
7160 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7161 struct rpc_clnt *clnt = server->client;
7162 struct nfs4_fsid_present_arg args = {
7163 .fh = NFS_FH(inode),
7164 .clientid = clp->cl_clientid,
7165 .renew = 1, /* append RENEW */
7166 };
7167 struct nfs4_fsid_present_res res = {
7168 .renew = 1,
7169 };
7170 struct rpc_message msg = {
7171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7172 .rpc_argp = &args,
7173 .rpc_resp = &res,
7174 .rpc_cred = cred,
7175 };
7176 unsigned long now = jiffies;
7177 int status;
7178
7179 res.fh = nfs_alloc_fhandle();
7180 if (res.fh == NULL)
7181 return -ENOMEM;
7182
7183 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7184 nfs4_set_sequence_privileged(&args.seq_args);
7185 status = nfs4_call_sync_sequence(clnt, server, &msg,
7186 &args.seq_args, &res.seq_res);
7187 nfs_free_fhandle(res.fh);
7188 if (status)
7189 return status;
7190
7191 do_renew_lease(clp, now);
7192 return 0;
7193}
7194
7195#ifdef CONFIG_NFS_V4_1
7196
7197/*
7198 * This operation also signals the server that this client is
7199 * performing "lease moved" recovery. The server can stop asserting
7200 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7201 * this operation is identified in the SEQUENCE operation in this
7202 * compound.
7203 */
7204static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7205{
7206 struct nfs_server *server = NFS_SERVER(inode);
7207 struct rpc_clnt *clnt = server->client;
7208 struct nfs4_fsid_present_arg args = {
7209 .fh = NFS_FH(inode),
7210 };
7211 struct nfs4_fsid_present_res res = {
7212 };
7213 struct rpc_message msg = {
7214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7215 .rpc_argp = &args,
7216 .rpc_resp = &res,
7217 .rpc_cred = cred,
7218 };
7219 int status;
7220
7221 res.fh = nfs_alloc_fhandle();
7222 if (res.fh == NULL)
7223 return -ENOMEM;
7224
7225 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7226 nfs4_set_sequence_privileged(&args.seq_args);
7227 status = nfs4_call_sync_sequence(clnt, server, &msg,
7228 &args.seq_args, &res.seq_res);
7229 nfs_free_fhandle(res.fh);
7230 if (status == NFS4_OK &&
7231 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7232 status = -NFS4ERR_LEASE_MOVED;
7233 return status;
7234}
7235
7236#endif /* CONFIG_NFS_V4_1 */
7237
7238/**
7239 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7240 * @inode: inode on FSID to check
7241 * @cred: credential to use for this operation
7242 *
7243 * Server indicates whether the FSID is present, moved, or not
7244 * recognized. This operation is necessary to clear a LEASE_MOVED
7245 * condition for this client ID.
7246 *
7247 * Returns NFS4_OK if the FSID is present on this server,
7248 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7249 * NFS4ERR code if some error occurred on the server, or a
7250 * negative errno if a local failure occurred.
7251 */
7252int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7253{
7254 struct nfs_server *server = NFS_SERVER(inode);
7255 struct nfs_client *clp = server->nfs_client;
7256 const struct nfs4_mig_recovery_ops *ops =
7257 clp->cl_mvops->mig_recovery_ops;
7258 struct nfs4_exception exception = { };
7259 int status;
7260
7261 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7262 (unsigned long long)server->fsid.major,
7263 (unsigned long long)server->fsid.minor,
7264 clp->cl_hostname);
7265 nfs_display_fhandle(NFS_FH(inode), __func__);
7266
7267 do {
7268 status = ops->fsid_present(inode, cred);
7269 if (status != -NFS4ERR_DELAY)
7270 break;
7271 nfs4_handle_exception(server, status, &exception);
7272 } while (exception.retry);
7273 return status;
7274}
7275
Andy Adamson5ec16a82013-08-08 10:57:55 -04007276/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007277 * If 'use_integrity' is true and the state managment nfs_client
7278 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7279 * and the machine credential as per RFC3530bis and RFC5661 Security
7280 * Considerations sections. Otherwise, just use the user cred with the
7281 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007282 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007283static 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 +00007284{
7285 int status;
7286 struct nfs4_secinfo_arg args = {
7287 .dir_fh = NFS_FH(dir),
7288 .name = name,
7289 };
7290 struct nfs4_secinfo_res res = {
7291 .flavors = flavors,
7292 };
7293 struct rpc_message msg = {
7294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7295 .rpc_argp = &args,
7296 .rpc_resp = &res,
7297 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007298 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007299 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007300
7301 if (use_integrity) {
7302 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007303 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7304 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007305 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007306
7307 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007308
7309 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7310 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7311
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007312 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7313 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007314 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007315
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007316 if (cred)
7317 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007318
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007319 return status;
7320}
7321
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007322int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7323 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007324{
7325 struct nfs4_exception exception = { };
7326 int err;
7327 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007328 err = -NFS4ERR_WRONGSEC;
7329
7330 /* try to use integrity protection with machine cred */
7331 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7332 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7333
7334 /*
7335 * if unable to use integrity protection, or SECINFO with
7336 * integrity protection returns NFS4ERR_WRONGSEC (which is
7337 * disallowed by spec, but exists in deployed servers) use
7338 * the current filesystem's rpc_client and the user cred.
7339 */
7340 if (err == -NFS4ERR_WRONGSEC)
7341 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7342
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007343 trace_nfs4_secinfo(dir, name, err);
7344 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007345 &exception);
7346 } while (exception.retry);
7347 return err;
7348}
7349
Andy Adamson557134a2009-04-01 09:21:53 -04007350#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007351/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007352 * Check the exchange flags returned by the server for invalid flags, having
7353 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7354 * DS flags set.
7355 */
7356static int nfs4_check_cl_exchange_flags(u32 flags)
7357{
7358 if (flags & ~EXCHGID4_FLAG_MASK_R)
7359 goto out_inval;
7360 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7361 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7362 goto out_inval;
7363 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7364 goto out_inval;
7365 return NFS_OK;
7366out_inval:
7367 return -NFS4ERR_INVAL;
7368}
7369
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007370static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007371nfs41_same_server_scope(struct nfs41_server_scope *a,
7372 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007373{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007374 if (a->server_scope_sz != b->server_scope_sz)
7375 return false;
7376 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007377}
7378
Andy Adamson02a95de2016-02-05 16:08:37 -05007379static void
7380nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7381{
7382}
7383
7384static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7385 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7386};
7387
Andy Adamson357f54d2010-12-14 10:11:57 -05007388/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007389 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007390 *
7391 * The 4.1 client currently uses the same TCP connection for the
7392 * fore and backchannel.
7393 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007394static
7395int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7396 struct rpc_xprt *xprt,
7397 struct nfs_client *clp,
7398 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007399{
7400 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007401 struct nfs41_bind_conn_to_session_args args = {
7402 .client = clp,
7403 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7404 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007405 struct nfs41_bind_conn_to_session_res res;
7406 struct rpc_message msg = {
7407 .rpc_proc =
7408 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007409 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007410 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007411 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007412 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007413 struct rpc_task_setup task_setup_data = {
7414 .rpc_client = clnt,
7415 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007416 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007417 .rpc_message = &msg,
7418 .flags = RPC_TASK_TIMEOUT,
7419 };
7420 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007421
Trond Myklebust71a097c2015-02-18 09:27:18 -08007422 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7423 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7424 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007425
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007426 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7427 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7428 args.dir = NFS4_CDFC4_FORE;
7429
7430 task = rpc_run_task(&task_setup_data);
7431 if (!IS_ERR(task)) {
7432 status = task->tk_status;
7433 rpc_put_task(task);
7434 } else
7435 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007436 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007437 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007438 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007439 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7440 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007441 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007442 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007443 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007444 dprintk("NFS: %s: Unexpected direction from server\n",
7445 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007446 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007447 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007448 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007449 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7450 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007451 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007452 }
7453 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007454
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007455 return status;
7456}
7457
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007458struct rpc_bind_conn_calldata {
7459 struct nfs_client *clp;
7460 struct rpc_cred *cred;
7461};
7462
7463static int
7464nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7465 struct rpc_xprt *xprt,
7466 void *calldata)
7467{
7468 struct rpc_bind_conn_calldata *p = calldata;
7469
7470 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7471}
7472
7473int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7474{
7475 struct rpc_bind_conn_calldata data = {
7476 .clp = clp,
7477 .cred = cred,
7478 };
7479 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7480 nfs4_proc_bind_conn_to_session_callback, &data);
7481}
7482
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007483/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007484 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7485 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007486 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007487static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7488 .how = SP4_MACH_CRED,
7489 .enforce.u.words = {
7490 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7491 1 << (OP_EXCHANGE_ID - 32) |
7492 1 << (OP_CREATE_SESSION - 32) |
7493 1 << (OP_DESTROY_SESSION - 32) |
7494 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007495 },
7496 .allow.u.words = {
7497 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007498 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007499 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007500 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007501 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007502 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007503 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007504 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007505 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007506 1 << (OP_FREE_STATEID - 32) |
7507 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007508 }
7509};
7510
7511/*
7512 * Select the state protection mode for client `clp' given the server results
7513 * from exchange_id in `sp'.
7514 *
7515 * Returns 0 on success, negative errno otherwise.
7516 */
7517static int nfs4_sp4_select_mode(struct nfs_client *clp,
7518 struct nfs41_state_protection *sp)
7519{
7520 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7521 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7522 1 << (OP_EXCHANGE_ID - 32) |
7523 1 << (OP_CREATE_SESSION - 32) |
7524 1 << (OP_DESTROY_SESSION - 32) |
7525 1 << (OP_DESTROY_CLIENTID - 32)
7526 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007527 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007528 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007529 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007530
7531 if (sp->how == SP4_MACH_CRED) {
7532 /* Print state protect result */
7533 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7534 for (i = 0; i <= LAST_NFS4_OP; i++) {
7535 if (test_bit(i, sp->enforce.u.longs))
7536 dfprintk(MOUNT, " enforce op %d\n", i);
7537 if (test_bit(i, sp->allow.u.longs))
7538 dfprintk(MOUNT, " allow op %d\n", i);
7539 }
7540
7541 /* make sure nothing is on enforce list that isn't supported */
7542 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7543 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7544 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007545 ret = -EINVAL;
7546 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007547 }
7548 }
7549
7550 /*
7551 * Minimal mode - state operations are allowed to use machine
7552 * credential. Note this already happens by default, so the
7553 * client doesn't have to do anything more than the negotiation.
7554 *
7555 * NOTE: we don't care if EXCHANGE_ID is in the list -
7556 * we're already using the machine cred for exchange_id
7557 * and will never use a different cred.
7558 */
7559 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7560 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7561 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7562 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7563 dfprintk(MOUNT, "sp4_mach_cred:\n");
7564 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007565 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007566 } else {
7567 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007568 ret = -EINVAL;
7569 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007570 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007571
7572 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007573 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7574 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007575 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7576 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007577 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007578 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007579
Andrew Elble99ade3c2015-12-02 09:39:51 -05007580 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7581 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007582 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007583 }
7584
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007585 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7586 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7587 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007588 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007589 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007590
7591 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7592 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7593 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007594 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007595 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007596
7597 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7598 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007599 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007600 }
7601
7602 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7603 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007604 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007605 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007606 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007607out:
7608 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007609 return 0;
7610}
7611
Andy Adamson8d89bd72016-09-09 09:22:18 -04007612struct nfs41_exchange_id_data {
7613 struct nfs41_exchange_id_res res;
7614 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007615};
7616
Andy Adamson8d89bd72016-09-09 09:22:18 -04007617static void nfs4_exchange_id_release(void *data)
7618{
7619 struct nfs41_exchange_id_data *cdata =
7620 (struct nfs41_exchange_id_data *)data;
7621
Olga Kornievskaia63513232017-03-13 10:36:19 -04007622 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007623 kfree(cdata->res.impl_id);
7624 kfree(cdata->res.server_scope);
7625 kfree(cdata->res.server_owner);
7626 kfree(cdata);
7627}
7628
7629static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007630 .rpc_release = nfs4_exchange_id_release,
7631};
7632
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007633/*
7634 * _nfs4_proc_exchange_id()
7635 *
7636 * Wrapper for EXCHANGE_ID operation.
7637 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007638static struct rpc_task *
7639nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007640 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007641{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007642 struct rpc_message msg = {
7643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007644 .rpc_cred = cred,
7645 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007646 struct rpc_task_setup task_setup_data = {
7647 .rpc_client = clp->cl_rpcclient,
7648 .callback_ops = &nfs4_exchange_id_call_ops,
7649 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007650 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007651 };
7652 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007653 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007654
Elena Reshetova212bf412017-10-20 12:53:38 +03007655 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007656 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007657
Trond Myklebust9c760d12017-07-31 18:38:50 -04007658 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007659 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007660 if (!calldata)
7661 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007662
Trond Myklebustfd405592017-08-01 16:02:47 -04007663 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007664
7665 status = nfs4_init_uniform_client_string(clp);
7666 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007667 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007668
Andy Adamson8d89bd72016-09-09 09:22:18 -04007669 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7670 GFP_NOFS);
7671 status = -ENOMEM;
7672 if (unlikely(calldata->res.server_owner == NULL))
7673 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007674
Andy Adamson8d89bd72016-09-09 09:22:18 -04007675 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007676 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007677 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007678 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007679
Andy Adamson8d89bd72016-09-09 09:22:18 -04007680 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7681 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007682 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007683
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007684 switch (sp4_how) {
7685 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007686 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007687 break;
7688
7689 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007690 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007691 break;
7692
7693 default:
7694 /* unsupported! */
7695 WARN_ON_ONCE(1);
7696 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007697 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007698 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007699 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007700 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007701 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007702 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7703 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007704 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007705 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007706 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7707 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007708#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007709 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007710#endif
7711 msg.rpc_argp = &calldata->args;
7712 msg.rpc_resp = &calldata->res;
7713 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007714
Trond Myklebust9c760d12017-07-31 18:38:50 -04007715 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007716
7717out_impl_id:
7718 kfree(calldata->res.impl_id);
7719out_server_scope:
7720 kfree(calldata->res.server_scope);
7721out_server_owner:
7722 kfree(calldata->res.server_owner);
7723out_calldata:
7724 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007725out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007726 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007727 return ERR_PTR(status);
7728}
7729
7730/*
7731 * _nfs4_proc_exchange_id()
7732 *
7733 * Wrapper for EXCHANGE_ID operation.
7734 */
7735static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7736 u32 sp4_how)
7737{
7738 struct rpc_task *task;
7739 struct nfs41_exchange_id_args *argp;
7740 struct nfs41_exchange_id_res *resp;
7741 int status;
7742
7743 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7744 if (IS_ERR(task))
7745 return PTR_ERR(task);
7746
7747 argp = task->tk_msg.rpc_argp;
7748 resp = task->tk_msg.rpc_resp;
7749 status = task->tk_status;
7750 if (status != 0)
7751 goto out;
7752
7753 status = nfs4_check_cl_exchange_flags(resp->flags);
7754 if (status != 0)
7755 goto out;
7756
7757 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7758 if (status != 0)
7759 goto out;
7760
7761 clp->cl_clientid = resp->clientid;
7762 clp->cl_exchange_flags = resp->flags;
7763 clp->cl_seqid = resp->seqid;
7764 /* Client ID is not confirmed */
7765 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7766 clear_bit(NFS4_SESSION_ESTABLISHED,
7767 &clp->cl_session->session_state);
7768
7769 if (clp->cl_serverscope != NULL &&
7770 !nfs41_same_server_scope(clp->cl_serverscope,
7771 resp->server_scope)) {
7772 dprintk("%s: server_scope mismatch detected\n",
7773 __func__);
7774 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7775 }
7776
7777 swap(clp->cl_serverowner, resp->server_owner);
7778 swap(clp->cl_serverscope, resp->server_scope);
7779 swap(clp->cl_implid, resp->impl_id);
7780
7781 /* Save the EXCHANGE_ID verifier session trunk tests */
7782 memcpy(clp->cl_confirm.data, argp->verifier.data,
7783 sizeof(clp->cl_confirm.data));
7784out:
7785 trace_nfs4_exchange_id(clp, status);
7786 rpc_put_task(task);
7787 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007788}
7789
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007790/*
7791 * nfs4_proc_exchange_id()
7792 *
7793 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7794 *
7795 * Since the clientid has expired, all compounds using sessions
7796 * associated with the stale clientid will be returning
7797 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7798 * be in some phase of session reset.
7799 *
7800 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7801 */
7802int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7803{
7804 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7805 int status;
7806
7807 /* try SP4_MACH_CRED if krb5i/p */
7808 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7809 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007810 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007811 if (!status)
7812 return 0;
7813 }
7814
7815 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007816 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007817}
7818
Andy Adamson04fa2c62016-09-09 09:22:29 -04007819/**
7820 * nfs4_test_session_trunk
7821 *
7822 * This is an add_xprt_test() test function called from
7823 * rpc_clnt_setup_test_and_add_xprt.
7824 *
7825 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7826 * and is dereferrenced in nfs4_exchange_id_release
7827 *
7828 * Upon success, add the new transport to the rpc_clnt
7829 *
7830 * @clnt: struct rpc_clnt to get new transport
7831 * @xprt: the rpc_xprt to test
7832 * @data: call data for _nfs4_proc_exchange_id.
7833 */
7834int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7835 void *data)
7836{
7837 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007838 struct rpc_task *task;
7839 int status;
7840
Andy Adamson04fa2c62016-09-09 09:22:29 -04007841 u32 sp4_how;
7842
7843 dprintk("--> %s try %s\n", __func__,
7844 xprt->address_strings[RPC_DISPLAY_ADDR]);
7845
7846 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7847
7848 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007849 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7850 if (IS_ERR(task))
7851 return PTR_ERR(task);
7852
7853 status = task->tk_status;
7854 if (status == 0)
7855 status = nfs4_detect_session_trunking(adata->clp,
7856 task->tk_msg.rpc_resp, xprt);
7857
7858 rpc_put_task(task);
7859 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007860}
7861EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7862
Trond Myklebust66245532012-05-25 17:18:09 -04007863static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7864 struct rpc_cred *cred)
7865{
7866 struct rpc_message msg = {
7867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7868 .rpc_argp = clp,
7869 .rpc_cred = cred,
7870 };
7871 int status;
7872
7873 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007874 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007875 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007876 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007877 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7878 return status;
7879}
7880
7881static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7882 struct rpc_cred *cred)
7883{
7884 unsigned int loop;
7885 int ret;
7886
7887 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7888 ret = _nfs4_proc_destroy_clientid(clp, cred);
7889 switch (ret) {
7890 case -NFS4ERR_DELAY:
7891 case -NFS4ERR_CLIENTID_BUSY:
7892 ssleep(1);
7893 break;
7894 default:
7895 return ret;
7896 }
7897 }
7898 return 0;
7899}
7900
7901int nfs4_destroy_clientid(struct nfs_client *clp)
7902{
7903 struct rpc_cred *cred;
7904 int ret = 0;
7905
7906 if (clp->cl_mvops->minor_version < 1)
7907 goto out;
7908 if (clp->cl_exchange_flags == 0)
7909 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007910 if (clp->cl_preserve_clid)
7911 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007912 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007913 ret = nfs4_proc_destroy_clientid(clp, cred);
7914 if (cred)
7915 put_rpccred(cred);
7916 switch (ret) {
7917 case 0:
7918 case -NFS4ERR_STALE_CLIENTID:
7919 clp->cl_exchange_flags = 0;
7920 }
7921out:
7922 return ret;
7923}
7924
Andy Adamson2050f0c2009-04-01 09:22:30 -04007925struct nfs4_get_lease_time_data {
7926 struct nfs4_get_lease_time_args *args;
7927 struct nfs4_get_lease_time_res *res;
7928 struct nfs_client *clp;
7929};
7930
7931static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7932 void *calldata)
7933{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007934 struct nfs4_get_lease_time_data *data =
7935 (struct nfs4_get_lease_time_data *)calldata;
7936
7937 dprintk("--> %s\n", __func__);
7938 /* just setup sequence, do not trigger session recovery
7939 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007940 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007941 &data->args->la_seq_args,
7942 &data->res->lr_seq_res,
7943 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007944 dprintk("<-- %s\n", __func__);
7945}
7946
7947/*
7948 * Called from nfs4_state_manager thread for session setup, so don't recover
7949 * from sequence operation or clientid errors.
7950 */
7951static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7952{
7953 struct nfs4_get_lease_time_data *data =
7954 (struct nfs4_get_lease_time_data *)calldata;
7955
7956 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007957 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7958 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007959 switch (task->tk_status) {
7960 case -NFS4ERR_DELAY:
7961 case -NFS4ERR_GRACE:
7962 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7963 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7964 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007965 /* fall through */
7966 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007967 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007968 return;
7969 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007970 dprintk("<-- %s\n", __func__);
7971}
7972
Trond Myklebust17280172012-03-11 13:11:00 -04007973static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007974 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7975 .rpc_call_done = nfs4_get_lease_time_done,
7976};
7977
7978int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7979{
7980 struct rpc_task *task;
7981 struct nfs4_get_lease_time_args args;
7982 struct nfs4_get_lease_time_res res = {
7983 .lr_fsinfo = fsinfo,
7984 };
7985 struct nfs4_get_lease_time_data data = {
7986 .args = &args,
7987 .res = &res,
7988 .clp = clp,
7989 };
7990 struct rpc_message msg = {
7991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7992 .rpc_argp = &args,
7993 .rpc_resp = &res,
7994 };
7995 struct rpc_task_setup task_setup = {
7996 .rpc_client = clp->cl_rpcclient,
7997 .rpc_message = &msg,
7998 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007999 .callback_data = &data,
8000 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008001 };
8002 int status;
8003
Chuck Levera9c92d62013-08-09 12:48:18 -04008004 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008005 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008006 task = rpc_run_task(&task_setup);
8007
8008 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008009 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008010
Anna Schumakerf6148712017-04-07 14:15:23 -04008011 status = task->tk_status;
8012 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008013 return status;
8014}
8015
Andy Adamsonfc931582009-04-01 09:22:31 -04008016/*
8017 * Initialize the values to be used by the client in CREATE_SESSION
8018 * If nfs4_init_session set the fore channel request and response sizes,
8019 * use them.
8020 *
8021 * Set the back channel max_resp_sz_cached to zero to force the client to
8022 * always set csa_cachethis to FALSE because the current implementation
8023 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8024 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008025static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8026 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008027{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008028 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008029 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008030
Andy Adamson18aad3d2013-06-26 12:21:49 -04008031 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8032 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8033
Andy Adamsonfc931582009-04-01 09:22:31 -04008034 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008035 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8036 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008037 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008038 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008039
8040 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008041 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008042 __func__,
8043 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008044 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008045
8046 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008047 args->bc_attrs.max_rqst_sz = max_bc_payload;
8048 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008049 args->bc_attrs.max_resp_sz_cached = 0;
8050 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008051 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008052
8053 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8054 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8055 __func__,
8056 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8057 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8058 args->bc_attrs.max_reqs);
8059}
8060
Trond Myklebust79969dd2015-02-18 11:30:18 -08008061static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8062 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008063{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008064 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008065 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008066
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008067 if (rcvd->max_resp_sz > sent->max_resp_sz)
8068 return -EINVAL;
8069 /*
8070 * Our requested max_ops is the minimum we need; we're not
8071 * prepared to break up compounds into smaller pieces than that.
8072 * So, no point even trying to continue if the server won't
8073 * cooperate:
8074 */
8075 if (rcvd->max_ops < sent->max_ops)
8076 return -EINVAL;
8077 if (rcvd->max_reqs == 0)
8078 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008079 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8080 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008081 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008082}
8083
Trond Myklebust79969dd2015-02-18 11:30:18 -08008084static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8085 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008086{
8087 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008088 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008089
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008090 if (!(res->flags & SESSION4_BACK_CHAN))
8091 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008092 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8093 return -EINVAL;
8094 if (rcvd->max_resp_sz < sent->max_resp_sz)
8095 return -EINVAL;
8096 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8097 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008098 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008099 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008100 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008101 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008102out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008103 return 0;
8104}
Andy Adamson8d353012009-04-01 09:22:32 -04008105
Andy Adamson8d353012009-04-01 09:22:32 -04008106static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008107 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008108{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008109 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008110
Trond Myklebust79969dd2015-02-18 11:30:18 -08008111 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008112 if (ret)
8113 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008114 return nfs4_verify_back_channel_attrs(args, res);
8115}
8116
8117static void nfs4_update_session(struct nfs4_session *session,
8118 struct nfs41_create_session_res *res)
8119{
8120 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008121 /* Mark client id and session as being confirmed */
8122 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8123 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008124 session->flags = res->flags;
8125 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008126 if (res->flags & SESSION4_BACK_CHAN)
8127 memcpy(&session->bc_attrs, &res->bc_attrs,
8128 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008129}
8130
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008131static int _nfs4_proc_create_session(struct nfs_client *clp,
8132 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008133{
8134 struct nfs4_session *session = clp->cl_session;
8135 struct nfs41_create_session_args args = {
8136 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008137 .clientid = clp->cl_clientid,
8138 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008139 .cb_program = NFS4_CALLBACK,
8140 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008141 struct nfs41_create_session_res res;
8142
Andy Adamsonfc931582009-04-01 09:22:31 -04008143 struct rpc_message msg = {
8144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8145 .rpc_argp = &args,
8146 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008147 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008148 };
8149 int status;
8150
Chuck Lever6b26cc82016-05-02 14:40:40 -04008151 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008152 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008153
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008154 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008155 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008156
Trond Myklebustb519d402016-09-11 14:50:01 -04008157 switch (status) {
8158 case -NFS4ERR_STALE_CLIENTID:
8159 case -NFS4ERR_DELAY:
8160 case -ETIMEDOUT:
8161 case -EACCES:
8162 case -EAGAIN:
8163 goto out;
8164 };
8165
8166 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008167 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008168 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008169 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008170 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008171 if (status)
8172 goto out;
8173 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008174 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008175out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008176 return status;
8177}
8178
8179/*
8180 * Issues a CREATE_SESSION operation to the server.
8181 * It is the responsibility of the caller to verify the session is
8182 * expired before calling this routine.
8183 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008184int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008185{
8186 int status;
8187 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008188 struct nfs4_session *session = clp->cl_session;
8189
8190 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8191
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008192 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008193 if (status)
8194 goto out;
8195
Andy Adamsonaacd5532011-11-09 13:58:21 -05008196 /* Init or reset the session slot tables */
8197 status = nfs4_setup_session_slot_tables(session);
8198 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008199 if (status)
8200 goto out;
8201
8202 ptr = (unsigned *)&session->sess_id.data[0];
8203 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8204 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008205out:
8206 dprintk("<-- %s\n", __func__);
8207 return status;
8208}
8209
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008210/*
8211 * Issue the over-the-wire RPC DESTROY_SESSION.
8212 * The caller must serialize access to this routine.
8213 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008214int nfs4_proc_destroy_session(struct nfs4_session *session,
8215 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008216{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008217 struct rpc_message msg = {
8218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8219 .rpc_argp = session,
8220 .rpc_cred = cred,
8221 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008222 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008223
8224 dprintk("--> nfs4_proc_destroy_session\n");
8225
8226 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008227 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8228 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008229
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008230 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008231 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008232
8233 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008234 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008235 "Session has been destroyed regardless...\n", status);
8236
8237 dprintk("<-- nfs4_proc_destroy_session\n");
8238 return status;
8239}
8240
Trond Myklebust7b38c362012-05-23 13:23:31 -04008241/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008242 * Renew the cl_session lease.
8243 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008244struct nfs4_sequence_data {
8245 struct nfs_client *clp;
8246 struct nfs4_sequence_args args;
8247 struct nfs4_sequence_res res;
8248};
8249
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008250static void nfs41_sequence_release(void *data)
8251{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008252 struct nfs4_sequence_data *calldata = data;
8253 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008254
Elena Reshetova212bf412017-10-20 12:53:38 +03008255 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008256 nfs4_schedule_state_renewal(clp);
8257 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008258 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008259}
8260
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008261static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8262{
8263 switch(task->tk_status) {
8264 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008265 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8266 return -EAGAIN;
8267 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008268 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008269 }
8270 return 0;
8271}
8272
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008273static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008274{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008275 struct nfs4_sequence_data *calldata = data;
8276 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008277
Trond Myklebust14516c32010-07-31 14:29:06 -04008278 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8279 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008280
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008281 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008282 if (task->tk_status < 0) {
8283 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008284 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008285 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008286
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008287 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8288 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008289 return;
8290 }
8291 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008292 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008293out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008294 dprintk("<-- %s\n", __func__);
8295}
8296
8297static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8298{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008299 struct nfs4_sequence_data *calldata = data;
8300 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008301 struct nfs4_sequence_args *args;
8302 struct nfs4_sequence_res *res;
8303
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008304 args = task->tk_msg.rpc_argp;
8305 res = task->tk_msg.rpc_resp;
8306
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008307 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008308}
8309
8310static const struct rpc_call_ops nfs41_sequence_ops = {
8311 .rpc_call_done = nfs41_sequence_call_done,
8312 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008313 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008314};
8315
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008316static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8317 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008318 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008319 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008320{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008321 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008322 struct rpc_message msg = {
8323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8324 .rpc_cred = cred,
8325 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008326 struct rpc_task_setup task_setup_data = {
8327 .rpc_client = clp->cl_rpcclient,
8328 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008329 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008330 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008331 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008332 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008333
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008334 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008335 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008336 goto out_err;
8337
8338 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008339 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008340 if (calldata == NULL)
8341 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008342 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008343 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008344 if (is_privileged)
8345 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008346 msg.rpc_argp = &calldata->args;
8347 msg.rpc_resp = &calldata->res;
8348 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008349 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008350
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008351 ret = rpc_run_task(&task_setup_data);
8352 if (IS_ERR(ret))
8353 goto out_err;
8354 return ret;
8355out_put_clp:
8356 nfs_put_client(clp);
8357out_err:
8358 nfs41_release_slot(slot);
8359 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008360}
8361
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008362static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008363{
8364 struct rpc_task *task;
8365 int ret = 0;
8366
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008367 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008368 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008369 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008370 if (IS_ERR(task))
8371 ret = PTR_ERR(task);
8372 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008373 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008374 dprintk("<-- %s status=%d\n", __func__, ret);
8375 return ret;
8376}
8377
8378static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8379{
8380 struct rpc_task *task;
8381 int ret;
8382
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008383 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008384 if (IS_ERR(task)) {
8385 ret = PTR_ERR(task);
8386 goto out;
8387 }
8388 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008389 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008390 ret = task->tk_status;
8391 rpc_put_task(task);
8392out:
8393 dprintk("<-- %s status=%d\n", __func__, ret);
8394 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008395}
8396
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008397struct nfs4_reclaim_complete_data {
8398 struct nfs_client *clp;
8399 struct nfs41_reclaim_complete_args arg;
8400 struct nfs41_reclaim_complete_res res;
8401};
8402
8403static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8404{
8405 struct nfs4_reclaim_complete_data *calldata = data;
8406
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008407 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008408 &calldata->arg.seq_args,
8409 &calldata->res.seq_res,
8410 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008411}
8412
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008413static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8414{
8415 switch(task->tk_status) {
8416 case 0:
8417 case -NFS4ERR_COMPLETE_ALREADY:
8418 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8419 break;
8420 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008421 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008422 /* fall through */
8423 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008424 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008425 case -NFS4ERR_BADSESSION:
8426 case -NFS4ERR_DEADSESSION:
8427 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8428 nfs4_schedule_session_recovery(clp->cl_session,
8429 task->tk_status);
8430 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008431 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008432 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008433 }
8434 return 0;
8435}
8436
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008437static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8438{
8439 struct nfs4_reclaim_complete_data *calldata = data;
8440 struct nfs_client *clp = calldata->clp;
8441 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8442
8443 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008444 if (!nfs41_sequence_done(task, res))
8445 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008446
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008447 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008448 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8449 rpc_restart_call_prepare(task);
8450 return;
8451 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008452 dprintk("<-- %s\n", __func__);
8453}
8454
8455static void nfs4_free_reclaim_complete_data(void *data)
8456{
8457 struct nfs4_reclaim_complete_data *calldata = data;
8458
8459 kfree(calldata);
8460}
8461
8462static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8463 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8464 .rpc_call_done = nfs4_reclaim_complete_done,
8465 .rpc_release = nfs4_free_reclaim_complete_data,
8466};
8467
8468/*
8469 * Issue a global reclaim complete.
8470 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008471static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8472 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008473{
8474 struct nfs4_reclaim_complete_data *calldata;
8475 struct rpc_task *task;
8476 struct rpc_message msg = {
8477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008478 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008479 };
8480 struct rpc_task_setup task_setup_data = {
8481 .rpc_client = clp->cl_rpcclient,
8482 .rpc_message = &msg,
8483 .callback_ops = &nfs4_reclaim_complete_call_ops,
8484 .flags = RPC_TASK_ASYNC,
8485 };
8486 int status = -ENOMEM;
8487
8488 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008489 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008490 if (calldata == NULL)
8491 goto out;
8492 calldata->clp = clp;
8493 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008494
Chuck Levera9c92d62013-08-09 12:48:18 -04008495 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008496 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008497 msg.rpc_argp = &calldata->arg;
8498 msg.rpc_resp = &calldata->res;
8499 task_setup_data.callback_data = calldata;
8500 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008501 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008502 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008503 goto out;
8504 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008505 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008506 if (status == 0)
8507 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008508 rpc_put_task(task);
8509out:
8510 dprintk("<-- %s status=%d\n", __func__, status);
8511 return status;
8512}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008513
8514static void
8515nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8516{
8517 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008518 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008519
8520 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008521 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008522 &lgp->res.seq_res, task);
8523 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008524}
8525
8526static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8527{
8528 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008529
8530 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008531 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008532 dprintk("<-- %s\n", __func__);
8533}
8534
8535static int
8536nfs4_layoutget_handle_exception(struct rpc_task *task,
8537 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8538{
Trond Myklebustee314c22012-10-01 17:25:48 -07008539 struct inode *inode = lgp->args.inode;
8540 struct nfs_server *server = NFS_SERVER(inode);
8541 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008542 int nfs4err = task->tk_status;
8543 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008544 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008545
Boaz Harroshed7e5422014-01-22 20:34:54 +02008546 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008547
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008548 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008549 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008550 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008551
8552 /*
8553 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8554 * on the file. set tk_status to -ENODATA to tell upper layer to
8555 * retry go inband.
8556 */
8557 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008558 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008559 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008560 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008561 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8562 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8563 */
8564 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008565 status = -EOVERFLOW;
8566 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008567 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008568 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008569 * (or clients) writing to the same RAID stripe except when
8570 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008571 *
8572 * Treat it like we would RECALLCONFLICT -- we retry for a little
8573 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008574 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008575 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008576 if (lgp->args.minlength == 0) {
8577 status = -EOVERFLOW;
8578 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008579 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008580 status = -EBUSY;
8581 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008582 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008583 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008584 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008585 case -NFS4ERR_DELEG_REVOKED:
8586 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008587 case -NFS4ERR_EXPIRED:
8588 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008589 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008590 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008591 lo = NFS_I(inode)->layout;
8592 /* If the open stateid was bad, then recover it. */
8593 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008594 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008595 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008596 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008597 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008598 break;
8599 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008600
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008601 /*
8602 * Mark the bad layout state as invalid, then retry
8603 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008604 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008605 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008606 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008607 pnfs_free_lseg_list(&head);
8608 status = -EAGAIN;
8609 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008610 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008611
Trond Myklebust8ac09252017-01-23 22:44:12 -05008612 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008613 err = nfs4_handle_exception(server, nfs4err, exception);
8614 if (!status) {
8615 if (exception->retry)
8616 status = -EAGAIN;
8617 else
8618 status = err;
8619 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008620out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008621 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008622 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008623}
8624
Idan Kedar85541162012-08-02 11:47:10 +03008625static size_t max_response_pages(struct nfs_server *server)
8626{
8627 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8628 return nfs_page_array_len(0, max_resp_sz);
8629}
8630
8631static void nfs4_free_pages(struct page **pages, size_t size)
8632{
8633 int i;
8634
8635 if (!pages)
8636 return;
8637
8638 for (i = 0; i < size; i++) {
8639 if (!pages[i])
8640 break;
8641 __free_page(pages[i]);
8642 }
8643 kfree(pages);
8644}
8645
8646static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8647{
8648 struct page **pages;
8649 int i;
8650
8651 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8652 if (!pages) {
8653 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8654 return NULL;
8655 }
8656
8657 for (i = 0; i < size; i++) {
8658 pages[i] = alloc_page(gfp_flags);
8659 if (!pages[i]) {
8660 dprintk("%s: failed to allocate page\n", __func__);
8661 nfs4_free_pages(pages, size);
8662 return NULL;
8663 }
8664 }
8665
8666 return pages;
8667}
8668
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008669static void nfs4_layoutget_release(void *calldata)
8670{
8671 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008672 struct inode *inode = lgp->args.inode;
8673 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008674 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008675
8676 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008677 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008678 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008679 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008680 put_nfs_open_context(lgp->args.ctx);
8681 kfree(calldata);
8682 dprintk("<-- %s\n", __func__);
8683}
8684
8685static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8686 .rpc_call_prepare = nfs4_layoutget_prepare,
8687 .rpc_call_done = nfs4_layoutget_done,
8688 .rpc_release = nfs4_layoutget_release,
8689};
8690
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008691struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008692nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008693{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008694 struct inode *inode = lgp->args.inode;
8695 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008696 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008697 struct rpc_task *task;
8698 struct rpc_message msg = {
8699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8700 .rpc_argp = &lgp->args,
8701 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008702 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008703 };
8704 struct rpc_task_setup task_setup_data = {
8705 .rpc_client = server->client,
8706 .rpc_message = &msg,
8707 .callback_ops = &nfs4_layoutget_call_ops,
8708 .callback_data = lgp,
8709 .flags = RPC_TASK_ASYNC,
8710 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008711 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008712 struct nfs4_exception exception = {
8713 .inode = inode,
8714 .timeout = *timeout,
8715 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008716 int status = 0;
8717
8718 dprintk("--> %s\n", __func__);
8719
Peng Tao4bd5a982014-11-17 11:05:17 +08008720 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8721 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8722
Idan Kedar85541162012-08-02 11:47:10 +03008723 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8724 if (!lgp->args.layout.pages) {
8725 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008726 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008727 }
8728 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8729
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008730 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008731 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008732 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008733
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008734 task = rpc_run_task(&task_setup_data);
8735 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008736 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008737 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008738 if (status == 0) {
8739 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8740 *timeout = exception.timeout;
8741 }
8742
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008743 trace_nfs4_layoutget(lgp->args.ctx,
8744 &lgp->args.range,
8745 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008746 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008747 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008748
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008749 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8750 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008751 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008752 rpc_put_task(task);
8753 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008754 if (status)
8755 return ERR_PTR(status);
8756 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008757}
8758
Benny Halevycbe82602011-05-22 19:52:37 +03008759static void
8760nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8761{
8762 struct nfs4_layoutreturn *lrp = calldata;
8763
8764 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008765 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008766 &lrp->args.seq_args,
8767 &lrp->res.seq_res,
8768 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008769}
8770
8771static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8772{
8773 struct nfs4_layoutreturn *lrp = calldata;
8774 struct nfs_server *server;
8775
8776 dprintk("--> %s\n", __func__);
8777
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008778 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008779 return;
8780
8781 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008782 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008783 case -NFS4ERR_OLD_STATEID:
8784 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8785 lrp->args.inode))
8786 goto out_restart;
8787 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008788 default:
8789 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008790 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008791 case 0:
8792 break;
8793 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008794 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008795 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008796 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008797 }
Benny Halevycbe82602011-05-22 19:52:37 +03008798 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008799 return;
8800out_restart:
8801 task->tk_status = 0;
8802 nfs4_sequence_free_slot(&lrp->res.seq_res);
8803 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008804}
8805
8806static void nfs4_layoutreturn_release(void *calldata)
8807{
8808 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008809 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008810
8811 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008812 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008813 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008814 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008815 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8816 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008817 pnfs_put_layout_hdr(lrp->args.layout);
8818 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008819 kfree(calldata);
8820 dprintk("<-- %s\n", __func__);
8821}
8822
8823static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8824 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8825 .rpc_call_done = nfs4_layoutreturn_done,
8826 .rpc_release = nfs4_layoutreturn_release,
8827};
8828
Peng Tao6c166052014-11-17 09:30:40 +08008829int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008830{
8831 struct rpc_task *task;
8832 struct rpc_message msg = {
8833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8834 .rpc_argp = &lrp->args,
8835 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008836 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008837 };
8838 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008839 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008840 .rpc_message = &msg,
8841 .callback_ops = &nfs4_layoutreturn_call_ops,
8842 .callback_data = lrp,
8843 };
Peng Tao6c166052014-11-17 09:30:40 +08008844 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008845
Andrew Elble99ade3c2015-12-02 09:39:51 -05008846 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8847 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8848 &task_setup_data.rpc_client, &msg);
8849
Benny Halevycbe82602011-05-22 19:52:37 +03008850 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008851 if (!sync) {
8852 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8853 if (!lrp->inode) {
8854 nfs4_layoutreturn_release(lrp);
8855 return -EAGAIN;
8856 }
8857 task_setup_data.flags |= RPC_TASK_ASYNC;
8858 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008859 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008860 task = rpc_run_task(&task_setup_data);
8861 if (IS_ERR(task))
8862 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008863 if (sync)
8864 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008865 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008866 dprintk("<-- %s status=%d\n", __func__, status);
8867 rpc_put_task(task);
8868 return status;
8869}
8870
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008871static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008872_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8873 struct pnfs_device *pdev,
8874 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008875{
8876 struct nfs4_getdeviceinfo_args args = {
8877 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008878 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8879 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008880 };
8881 struct nfs4_getdeviceinfo_res res = {
8882 .pdev = pdev,
8883 };
8884 struct rpc_message msg = {
8885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8886 .rpc_argp = &args,
8887 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008888 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008889 };
8890 int status;
8891
8892 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008893 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008894 if (res.notification & ~args.notify_types)
8895 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008896 if (res.notification != args.notify_types)
8897 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008898
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008899 dprintk("<-- %s status=%d\n", __func__, status);
8900
8901 return status;
8902}
8903
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008904int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8905 struct pnfs_device *pdev,
8906 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008907{
8908 struct nfs4_exception exception = { };
8909 int err;
8910
8911 do {
8912 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008913 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008914 &exception);
8915 } while (exception.retry);
8916 return err;
8917}
8918EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8919
Andy Adamson863a3c62011-03-23 13:27:54 +00008920static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8921{
8922 struct nfs4_layoutcommit_data *data = calldata;
8923 struct nfs_server *server = NFS_SERVER(data->args.inode);
8924
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008925 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008926 &data->args.seq_args,
8927 &data->res.seq_res,
8928 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008929}
8930
8931static void
8932nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8933{
8934 struct nfs4_layoutcommit_data *data = calldata;
8935 struct nfs_server *server = NFS_SERVER(data->args.inode);
8936
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008937 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008938 return;
8939
8940 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008941 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8942 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8943 case -NFS4ERR_BADLAYOUT: /* no layout */
8944 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008945 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008946 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008947 break;
8948 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008949 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008950 rpc_restart_call_prepare(task);
8951 return;
8952 }
8953 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008954}
8955
8956static void nfs4_layoutcommit_release(void *calldata)
8957{
8958 struct nfs4_layoutcommit_data *data = calldata;
8959
Andy Adamsondb29c082011-07-30 20:52:38 -04008960 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008961 nfs_post_op_update_inode_force_wcc(data->args.inode,
8962 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008963 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008964 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008965 kfree(data);
8966}
8967
8968static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8969 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8970 .rpc_call_done = nfs4_layoutcommit_done,
8971 .rpc_release = nfs4_layoutcommit_release,
8972};
8973
8974int
Andy Adamsonef311532011-03-12 02:58:10 -05008975nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008976{
8977 struct rpc_message msg = {
8978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8979 .rpc_argp = &data->args,
8980 .rpc_resp = &data->res,
8981 .rpc_cred = data->cred,
8982 };
8983 struct rpc_task_setup task_setup_data = {
8984 .task = &data->task,
8985 .rpc_client = NFS_CLIENT(data->args.inode),
8986 .rpc_message = &msg,
8987 .callback_ops = &nfs4_layoutcommit_ops,
8988 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008989 };
8990 struct rpc_task *task;
8991 int status = 0;
8992
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008993 dprintk("NFS: initiating layoutcommit call. sync %d "
8994 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008995 data->args.lastbytewritten,
8996 data->args.inode->i_ino);
8997
Trond Myklebust472e2592015-02-05 16:50:30 -05008998 if (!sync) {
8999 data->inode = nfs_igrab_and_active(data->args.inode);
9000 if (data->inode == NULL) {
9001 nfs4_layoutcommit_release(data);
9002 return -EAGAIN;
9003 }
9004 task_setup_data.flags = RPC_TASK_ASYNC;
9005 }
Chuck Levera9c92d62013-08-09 12:48:18 -04009006 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00009007 task = rpc_run_task(&task_setup_data);
9008 if (IS_ERR(task))
9009 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009010 if (sync)
9011 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009012 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009013 dprintk("%s: status %d\n", __func__, status);
9014 rpc_put_task(task);
9015 return status;
9016}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009017
Andy Adamson97431202013-08-08 10:57:56 -04009018/**
9019 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9020 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9021 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009022static int
9023_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009024 struct nfs_fsinfo *info,
9025 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009026{
9027 struct nfs41_secinfo_no_name_args args = {
9028 .style = SECINFO_STYLE_CURRENT_FH,
9029 };
9030 struct nfs4_secinfo_res res = {
9031 .flavors = flavors,
9032 };
9033 struct rpc_message msg = {
9034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9035 .rpc_argp = &args,
9036 .rpc_resp = &res,
9037 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009038 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009039 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009040 int status;
9041
9042 if (use_integrity) {
9043 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009044 cred = nfs4_get_clid_cred(server->nfs_client);
9045 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009046 }
9047
9048 dprintk("--> %s\n", __func__);
9049 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9050 &res.seq_res, 0);
9051 dprintk("<-- %s status=%d\n", __func__, status);
9052
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009053 if (cred)
9054 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009055
9056 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009057}
9058
9059static int
9060nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9061 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9062{
9063 struct nfs4_exception exception = { };
9064 int err;
9065 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009066 /* first try using integrity protection */
9067 err = -NFS4ERR_WRONGSEC;
9068
9069 /* try to use integrity protection with machine cred */
9070 if (_nfs4_is_integrity_protected(server->nfs_client))
9071 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9072 flavors, true);
9073
9074 /*
9075 * if unable to use integrity protection, or SECINFO with
9076 * integrity protection returns NFS4ERR_WRONGSEC (which is
9077 * disallowed by spec, but exists in deployed servers) use
9078 * the current filesystem's rpc_client and the user cred.
9079 */
9080 if (err == -NFS4ERR_WRONGSEC)
9081 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9082 flavors, false);
9083
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009084 switch (err) {
9085 case 0:
9086 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009087 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009088 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009089 default:
9090 err = nfs4_handle_exception(server, err, &exception);
9091 }
9092 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009093out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009094 return err;
9095}
9096
9097static int
9098nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9099 struct nfs_fsinfo *info)
9100{
9101 int err;
9102 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009103 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009104 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009105 struct nfs4_secinfo4 *secinfo;
9106 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009107
9108 page = alloc_page(GFP_KERNEL);
9109 if (!page) {
9110 err = -ENOMEM;
9111 goto out;
9112 }
9113
9114 flavors = page_address(page);
9115 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9116
9117 /*
9118 * Fall back on "guess and check" method if
9119 * the server doesn't support SECINFO_NO_NAME
9120 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009121 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009122 err = nfs4_find_root_sec(server, fhandle, info);
9123 goto out_freepage;
9124 }
9125 if (err)
9126 goto out_freepage;
9127
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009128 for (i = 0; i < flavors->num_flavors; i++) {
9129 secinfo = &flavors->flavors[i];
9130
9131 switch (secinfo->flavor) {
9132 case RPC_AUTH_NULL:
9133 case RPC_AUTH_UNIX:
9134 case RPC_AUTH_GSS:
9135 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9136 &secinfo->flavor_info);
9137 break;
9138 default:
9139 flavor = RPC_AUTH_MAXFLAVOR;
9140 break;
9141 }
9142
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009143 if (!nfs_auth_info_match(&server->auth_info, flavor))
9144 flavor = RPC_AUTH_MAXFLAVOR;
9145
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009146 if (flavor != RPC_AUTH_MAXFLAVOR) {
9147 err = nfs4_lookup_root_sec(server, fhandle,
9148 info, flavor);
9149 if (!err)
9150 break;
9151 }
9152 }
9153
9154 if (flavor == RPC_AUTH_MAXFLAVOR)
9155 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009156
9157out_freepage:
9158 put_page(page);
9159 if (err == -EACCES)
9160 return -EPERM;
9161out:
9162 return err;
9163}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009164
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009165static int _nfs41_test_stateid(struct nfs_server *server,
9166 nfs4_stateid *stateid,
9167 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009168{
9169 int status;
9170 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009171 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009172 };
9173 struct nfs41_test_stateid_res res;
9174 struct rpc_message msg = {
9175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9176 .rpc_argp = &args,
9177 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009178 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009179 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009180 struct rpc_clnt *rpc_client = server->client;
9181
9182 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9183 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009184
Chuck Lever38527b12012-07-11 16:30:23 -04009185 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009186 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009187 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009188 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009189 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009190 if (status != NFS_OK) {
9191 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009192 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009193 }
9194 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009195 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009196}
9197
Trond Myklebust43912bb2016-09-22 13:38:56 -04009198static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9199 int err, struct nfs4_exception *exception)
9200{
9201 exception->retry = 0;
9202 switch(err) {
9203 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009204 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009205 nfs4_handle_exception(server, err, exception);
9206 break;
9207 case -NFS4ERR_BADSESSION:
9208 case -NFS4ERR_BADSLOT:
9209 case -NFS4ERR_BAD_HIGH_SLOT:
9210 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9211 case -NFS4ERR_DEADSESSION:
9212 nfs4_do_handle_exception(server, err, exception);
9213 }
9214}
9215
Chuck Lever38527b12012-07-11 16:30:23 -04009216/**
9217 * nfs41_test_stateid - perform a TEST_STATEID operation
9218 *
9219 * @server: server / transport on which to perform the operation
9220 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009221 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009222 *
9223 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9224 * Otherwise a negative NFS4ERR value is returned if the operation
9225 * failed or the state ID is not currently valid.
9226 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009227static int nfs41_test_stateid(struct nfs_server *server,
9228 nfs4_stateid *stateid,
9229 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009230{
9231 struct nfs4_exception exception = { };
9232 int err;
9233 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009234 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009235 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009236 } while (exception.retry);
9237 return err;
9238}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009239
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009240struct nfs_free_stateid_data {
9241 struct nfs_server *server;
9242 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009243 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009244};
9245
9246static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9247{
9248 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009249 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009250 &data->args.seq_args,
9251 &data->res.seq_res,
9252 task);
9253}
9254
9255static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9256{
9257 struct nfs_free_stateid_data *data = calldata;
9258
9259 nfs41_sequence_done(task, &data->res.seq_res);
9260
9261 switch (task->tk_status) {
9262 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009263 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009264 rpc_restart_call_prepare(task);
9265 }
9266}
9267
9268static void nfs41_free_stateid_release(void *calldata)
9269{
9270 kfree(calldata);
9271}
9272
Trond Myklebust17f26b12013-08-21 15:48:42 -04009273static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009274 .rpc_call_prepare = nfs41_free_stateid_prepare,
9275 .rpc_call_done = nfs41_free_stateid_done,
9276 .rpc_release = nfs41_free_stateid_release,
9277};
9278
9279static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009280 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009281 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009282 bool privileged)
9283{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009284 struct rpc_message msg = {
9285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009286 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009287 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009288 struct rpc_task_setup task_setup = {
9289 .rpc_client = server->client,
9290 .rpc_message = &msg,
9291 .callback_ops = &nfs41_free_stateid_ops,
9292 .flags = RPC_TASK_ASYNC,
9293 };
9294 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009295
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009296 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9297 &task_setup.rpc_client, &msg);
9298
Chuck Lever38527b12012-07-11 16:30:23 -04009299 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009300 data = kmalloc(sizeof(*data), GFP_NOFS);
9301 if (!data)
9302 return ERR_PTR(-ENOMEM);
9303 data->server = server;
9304 nfs4_stateid_copy(&data->args.stateid, stateid);
9305
9306 task_setup.callback_data = data;
9307
9308 msg.rpc_argp = &data->args;
9309 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009310 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009311 if (privileged)
9312 nfs4_set_sequence_privileged(&data->args.seq_args);
9313
9314 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009315}
9316
Chuck Lever38527b12012-07-11 16:30:23 -04009317/**
9318 * nfs41_free_stateid - perform a FREE_STATEID operation
9319 *
9320 * @server: server / transport on which to perform the operation
9321 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009322 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009323 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009324 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009325 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009326 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009327static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009328 const nfs4_stateid *stateid,
9329 struct rpc_cred *cred,
9330 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009331{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009332 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009333
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009334 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009335 if (IS_ERR(task))
9336 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009337 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009338 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009339}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009340
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009341static void
9342nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009343{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009344 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009345
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009346 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009347 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009348}
9349
Trond Myklebust36281ca2012-03-04 18:13:56 -05009350static bool nfs41_match_stateid(const nfs4_stateid *s1,
9351 const nfs4_stateid *s2)
9352{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009353 if (s1->type != s2->type)
9354 return false;
9355
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009356 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009357 return false;
9358
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009359 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009360 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009361
Anna Schumaker045c5512017-01-11 16:59:48 -05009362 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009363}
9364
Andy Adamson557134a2009-04-01 09:21:53 -04009365#endif /* CONFIG_NFS_V4_1 */
9366
Trond Myklebust36281ca2012-03-04 18:13:56 -05009367static bool nfs4_match_stateid(const nfs4_stateid *s1,
9368 const nfs4_stateid *s2)
9369{
Trond Myklebustf597c532012-03-04 18:13:56 -05009370 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009371}
9372
9373
Trond Myklebust17280172012-03-11 13:11:00 -04009374static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009375 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009376 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 .recover_open = nfs4_open_reclaim,
9378 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009379 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009380 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381};
9382
Andy Adamson591d71c2009-04-01 09:22:47 -04009383#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009384static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009385 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9386 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9387 .recover_open = nfs4_open_reclaim,
9388 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009389 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009390 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009391 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009392};
9393#endif /* CONFIG_NFS_V4_1 */
9394
Trond Myklebust17280172012-03-11 13:11:00 -04009395static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009396 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009397 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009398 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009400 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401};
9402
Andy Adamson591d71c2009-04-01 09:22:47 -04009403#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009404static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009405 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9406 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009407 .recover_open = nfs41_open_expired,
9408 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009409 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009410};
9411#endif /* CONFIG_NFS_V4_1 */
9412
Trond Myklebust17280172012-03-11 13:11:00 -04009413static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009414 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009415 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009416 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009417};
9418
9419#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009420static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009421 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009422 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009423 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009424};
9425#endif
9426
Chuck Leverec011fe2013-10-17 14:12:39 -04009427static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009428 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009429 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009430};
9431
9432#if defined(CONFIG_NFS_V4_1)
9433static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009434 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009435 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009436};
9437#endif /* CONFIG_NFS_V4_1 */
9438
Trond Myklebust97dc1352010-06-16 09:52:26 -04009439static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9440 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009441 .init_caps = NFS_CAP_READDIRPLUS
9442 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009443 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009444 .init_client = nfs40_init_client,
9445 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009446 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009447 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009448 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009449 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009450 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009451 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009452 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9453 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9454 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009455 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009456};
9457
9458#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009459static struct nfs_seqid *
9460nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9461{
9462 return NULL;
9463}
9464
Trond Myklebust97dc1352010-06-16 09:52:26 -04009465static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9466 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009467 .init_caps = NFS_CAP_READDIRPLUS
9468 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009469 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009470 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009471 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009472 .init_client = nfs41_init_client,
9473 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009474 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009475 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009476 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009477 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009478 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009479 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009480 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009481 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9482 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9483 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009484 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009485};
9486#endif
9487
Steve Dickson42c2c422013-05-22 12:50:38 -04009488#if defined(CONFIG_NFS_V4_2)
9489static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9490 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009491 .init_caps = NFS_CAP_READDIRPLUS
9492 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009493 | NFS_CAP_POSIX_LOCK
9494 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009495 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009496 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009497 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009498 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009499 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009500 | NFS_CAP_LAYOUTSTATS
9501 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009502 .init_client = nfs41_init_client,
9503 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009504 .match_stateid = nfs41_match_stateid,
9505 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009506 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009507 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009508 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009509 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009510 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009511 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9512 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9513 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009514 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009515};
9516#endif
9517
Trond Myklebust97dc1352010-06-16 09:52:26 -04009518const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9519 [0] = &nfs_v4_0_minor_ops,
9520#if defined(CONFIG_NFS_V4_1)
9521 [1] = &nfs_v4_1_minor_ops,
9522#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009523#if defined(CONFIG_NFS_V4_2)
9524 [2] = &nfs_v4_2_minor_ops,
9525#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009526};
9527
Trond Myklebust13997822016-07-24 17:10:52 -04009528static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009529{
9530 ssize_t error, error2;
9531
9532 error = generic_listxattr(dentry, list, size);
9533 if (error < 0)
9534 return error;
9535 if (list) {
9536 list += error;
9537 size -= error;
9538 }
9539
9540 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9541 if (error2 < 0)
9542 return error2;
9543 return error + error2;
9544}
9545
Trond Myklebust17f26b12013-08-21 15:48:42 -04009546static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009547 .create = nfs_create,
9548 .lookup = nfs_lookup,
9549 .atomic_open = nfs_atomic_open,
9550 .link = nfs_link,
9551 .unlink = nfs_unlink,
9552 .symlink = nfs_symlink,
9553 .mkdir = nfs_mkdir,
9554 .rmdir = nfs_rmdir,
9555 .mknod = nfs_mknod,
9556 .rename = nfs_rename,
9557 .permission = nfs_permission,
9558 .getattr = nfs_getattr,
9559 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009560 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009561};
9562
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009563static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009564 .permission = nfs_permission,
9565 .getattr = nfs_getattr,
9566 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009567 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009568};
9569
David Howells509de812006-08-22 20:06:11 -04009570const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 .version = 4, /* protocol version */
9572 .dentry_ops = &nfs4_dentry_operations,
9573 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009574 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009575 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009577 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009578 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579 .getattr = nfs4_proc_getattr,
9580 .setattr = nfs4_proc_setattr,
9581 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009582 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 .access = nfs4_proc_access,
9584 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585 .create = nfs4_proc_create,
9586 .remove = nfs4_proc_remove,
9587 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009588 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009589 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009590 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009591 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009592 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593 .link = nfs4_proc_link,
9594 .symlink = nfs4_proc_symlink,
9595 .mkdir = nfs4_proc_mkdir,
9596 .rmdir = nfs4_proc_remove,
9597 .readdir = nfs4_proc_readdir,
9598 .mknod = nfs4_proc_mknod,
9599 .statfs = nfs4_proc_statfs,
9600 .fsinfo = nfs4_proc_fsinfo,
9601 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009602 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009603 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009604 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009605 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009606 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009608 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009610 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009611 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009613 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009614 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009615 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009616 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009617 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009618 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009619 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009620 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009621 .create_server = nfs4_create_server,
9622 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623};
9624
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009625static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009626 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009627 .list = nfs4_xattr_list_nfs4_acl,
9628 .get = nfs4_xattr_get_nfs4_acl,
9629 .set = nfs4_xattr_set_nfs4_acl,
9630};
9631
9632const struct xattr_handler *nfs4_xattr_handlers[] = {
9633 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009634#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9635 &nfs4_xattr_nfs4_label_handler,
9636#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009637 NULL
9638};
9639
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640/*
9641 * Local variables:
9642 * c-basic-offset: 8
9643 * End:
9644 */