blob: b2d628c9425cad955ff46eed0904fc85bcf8b16f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static 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 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -040099static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
100 struct rpc_cred *cred,
101 struct nfs4_slot *slot,
102 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400103static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
104 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400105static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
106 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400107#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400108
109#ifdef CONFIG_NFS_V4_SECURITY_LABEL
110static inline struct nfs4_label *
111nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
112 struct iattr *sattr, struct nfs4_label *label)
113{
114 int err;
115
116 if (label == NULL)
117 return NULL;
118
119 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
120 return NULL;
121
David Quigleyaa9c2662013-05-22 12:50:44 -0400122 err = security_dentry_init_security(dentry, sattr->ia_mode,
123 &dentry->d_name, (void **)&label->label, &label->len);
124 if (err == 0)
125 return label;
126
127 return NULL;
128}
129static inline void
130nfs4_label_release_security(struct nfs4_label *label)
131{
132 if (label)
133 security_release_secctx(label->label, label->len);
134}
135static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
136{
137 if (label)
138 return server->attr_bitmask;
139
140 return server->attr_bitmask_nl;
141}
142#else
143static inline struct nfs4_label *
144nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
145 struct iattr *sattr, struct nfs4_label *l)
146{ return NULL; }
147static inline void
148nfs4_label_release_security(struct nfs4_label *label)
149{ return; }
150static inline u32 *
151nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
152{ return server->attr_bitmask; }
153#endif
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500156static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Trond Myklebust52567b02009-10-23 14:46:42 -0400158 if (err >= -1000)
159 return err;
160 switch (err) {
161 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500162 case -NFS4ERR_LAYOUTTRYLATER:
163 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000165 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400166 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800168 case -NFS4ERR_BADOWNER:
169 case -NFS4ERR_BADNAME:
170 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400171 case -NFS4ERR_SHARE_DENIED:
172 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400173 case -NFS4ERR_MINOR_VERS_MISMATCH:
174 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400175 case -NFS4ERR_FILE_OPEN:
176 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700179 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
186 * This is our standard bitmap for GETATTR requests.
187 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400188const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID,
194 FATTR4_WORD1_MODE
195 | FATTR4_WORD1_NUMLINKS
196 | FATTR4_WORD1_OWNER
197 | FATTR4_WORD1_OWNER_GROUP
198 | FATTR4_WORD1_RAWDEV
199 | FATTR4_WORD1_SPACE_USED
200 | FATTR4_WORD1_TIME_ACCESS
201 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400202 | FATTR4_WORD1_TIME_MODIFY
203 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400204#ifdef CONFIG_NFS_V4_SECURITY_LABEL
205 FATTR4_WORD2_SECURITY_LABEL
206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Trond Myklebust1549210f2012-06-05 09:16:47 -0400209static const u32 nfs4_pnfs_open_bitmap[3] = {
210 FATTR4_WORD0_TYPE
211 | FATTR4_WORD0_CHANGE
212 | FATTR4_WORD0_SIZE
213 | FATTR4_WORD0_FSID
214 | FATTR4_WORD0_FILEID,
215 FATTR4_WORD1_MODE
216 | FATTR4_WORD1_NUMLINKS
217 | FATTR4_WORD1_OWNER
218 | FATTR4_WORD1_OWNER_GROUP
219 | FATTR4_WORD1_RAWDEV
220 | FATTR4_WORD1_SPACE_USED
221 | FATTR4_WORD1_TIME_ACCESS
222 | FATTR4_WORD1_TIME_METADATA
223 | FATTR4_WORD1_TIME_MODIFY,
224 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500225#ifdef CONFIG_NFS_V4_SECURITY_LABEL
226 | FATTR4_WORD2_SECURITY_LABEL
227#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400228};
229
Andy Adamsone23008e2012-10-02 21:07:32 -0400230static const u32 nfs4_open_noattr_bitmap[3] = {
231 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400232 | FATTR4_WORD0_FILEID,
233};
234
David Quigleya09df2c2013-05-22 12:50:41 -0400235const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 FATTR4_WORD0_FILES_AVAIL
237 | FATTR4_WORD0_FILES_FREE
238 | FATTR4_WORD0_FILES_TOTAL,
239 FATTR4_WORD1_SPACE_AVAIL
240 | FATTR4_WORD1_SPACE_FREE
241 | FATTR4_WORD1_SPACE_TOTAL
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_MAXLINK
246 | FATTR4_WORD0_MAXNAME,
247 0
248};
249
Fred Isamandae100c2011-07-30 20:52:37 -0400250const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 | FATTR4_WORD0_MAXREAD
252 | FATTR4_WORD0_MAXWRITE
253 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700254 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400255 | FATTR4_WORD1_FS_LAYOUT_TYPES,
256 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800257 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
David Quigleya09df2c2013-05-22 12:50:41 -0400260const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400261 FATTR4_WORD0_TYPE
262 | FATTR4_WORD0_CHANGE
263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
267 FATTR4_WORD1_MODE
268 | FATTR4_WORD1_NUMLINKS
269 | FATTR4_WORD1_OWNER
270 | FATTR4_WORD1_OWNER_GROUP
271 | FATTR4_WORD1_RAWDEV
272 | FATTR4_WORD1_SPACE_USED
273 | FATTR4_WORD1_TIME_ACCESS
274 | FATTR4_WORD1_TIME_METADATA
275 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400276 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400277};
278
Al Virobc4785c2006-10-19 23:28:51 -0700279static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct nfs4_readdir_arg *readdir)
281{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400282 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700283 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000286 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
288 return;
289 }
290
291 readdir->cookie = 0;
292 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
293 if (cookie == 2)
294 return;
295
296 /*
297 * NFSv4 servers do not return entries for '.' and '..'
298 * Therefore, we fake these entries here. We let '.'
299 * have cookie 0 and '..' have cookie 1. Note that
300 * when talking to the server, we always send cookie 0
301 * instead of 1 or 2.
302 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800303 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (cookie == 0) {
306 *p++ = xdr_one; /* next */
307 *p++ = xdr_zero; /* cookie, first word */
308 *p++ = xdr_one; /* cookie, second word */
309 *p++ = xdr_one; /* entry len */
310 memcpy(p, ".\0\0\0", 4); /* entry */
311 p++;
312 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400313 *p++ = htonl(attrs); /* bitmap */
314 *p++ = htonl(12); /* attribute buffer length */
315 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000316 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 *p++ = xdr_one; /* next */
320 *p++ = xdr_zero; /* cookie, first word */
321 *p++ = xdr_two; /* cookie, second word */
322 *p++ = xdr_two; /* entry len */
323 memcpy(p, "..\0\0", 4); /* entry */
324 p++;
325 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400326 *p++ = htonl(attrs); /* bitmap */
327 *p++ = htonl(12); /* attribute buffer length */
328 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000329 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 readdir->pgbase = (char *)p - (char *)start;
332 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800333 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Trond Myklebust26d36302016-09-22 13:39:05 -0400336static void nfs4_test_and_free_stateid(struct nfs_server *server,
337 nfs4_stateid *stateid,
338 struct rpc_cred *cred)
339{
340 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
341
342 ops->test_and_free_expired(server, stateid, cred);
343}
344
345static void __nfs4_free_revoked_stateid(struct nfs_server *server,
346 nfs4_stateid *stateid,
347 struct rpc_cred *cred)
348{
349 stateid->type = NFS4_REVOKED_STATEID_TYPE;
350 nfs4_test_and_free_stateid(server, stateid, cred);
351}
352
353static void nfs4_free_revoked_stateid(struct nfs_server *server,
354 const nfs4_stateid *stateid,
355 struct rpc_cred *cred)
356{
357 nfs4_stateid tmp;
358
359 nfs4_stateid_copy(&tmp, stateid);
360 __nfs4_free_revoked_stateid(server, &tmp, cred);
361}
362
NeilBrown8478eaa2014-09-18 16:09:27 +1000363static long nfs4_update_delay(long *timeout)
364{
365 long ret;
366 if (!timeout)
367 return NFS4_POLL_RETRY_MAX;
368 if (*timeout <= 0)
369 *timeout = NFS4_POLL_RETRY_MIN;
370 if (*timeout > NFS4_POLL_RETRY_MAX)
371 *timeout = NFS4_POLL_RETRY_MAX;
372 ret = *timeout;
373 *timeout <<= 1;
374 return ret;
375}
376
Trond Myklebust65de8722008-12-23 15:21:44 -0500377static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
378{
379 int res = 0;
380
381 might_sleep();
382
NeilBrown8478eaa2014-09-18 16:09:27 +1000383 freezable_schedule_timeout_killable_unsafe(
384 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500385 if (fatal_signal_pending(current))
386 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500387 return res;
388}
389
390/* This is the error handling routine for processes that are allowed
391 * to sleep.
392 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400393static int nfs4_do_handle_exception(struct nfs_server *server,
394 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500395{
396 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500397 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400398 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500399 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 int ret = errorcode;
401
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400402 exception->delay = 0;
403 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400405
406 if (stateid == NULL && state != NULL)
407 stateid = &state->stateid;
408
Trond Myklebust65de8722008-12-23 15:21:44 -0500409 switch(errorcode) {
410 case 0:
411 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400412 case -NFS4ERR_DELEG_REVOKED:
413 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400414 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400415 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400416 if (inode != NULL && stateid != NULL) {
417 nfs_inode_find_state_and_recover(inode,
418 stateid);
419 goto wait_on_recovery;
420 }
421 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400422 if (inode) {
423 int err;
424
425 err = nfs_async_inode_return_delegation(inode,
426 stateid);
427 if (err == 0)
428 goto wait_on_recovery;
429 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
430 exception->retry = 1;
431 break;
432 }
433 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500434 if (state == NULL)
435 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400436 ret = nfs4_schedule_stateid_recovery(server, state);
437 if (ret < 0)
438 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500439 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500440 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500442 nfs4_schedule_lease_recovery(clp);
443 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400444 case -NFS4ERR_MOVED:
445 ret = nfs4_schedule_migration_recovery(server);
446 if (ret < 0)
447 break;
448 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400449 case -NFS4ERR_LEASE_MOVED:
450 nfs4_schedule_lease_moved_recovery(clp);
451 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500452#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400453 case -NFS4ERR_BADSESSION:
454 case -NFS4ERR_BADSLOT:
455 case -NFS4ERR_BAD_HIGH_SLOT:
456 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
457 case -NFS4ERR_DEADSESSION:
458 case -NFS4ERR_SEQ_FALSE_RETRY:
459 case -NFS4ERR_SEQ_MISORDERED:
460 dprintk("%s ERROR: %d Reset session\n", __func__,
461 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400462 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400463 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500464#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500465 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500466 if (exception->timeout > HZ) {
467 /* We have retried a decent amount, time to
468 * fail
469 */
470 ret = -EBUSY;
471 break;
472 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400474 nfs_inc_server_stats(server, NFSIOS_DELAY);
475 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400476 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400477 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400478 exception->delay = 1;
479 return 0;
480
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400481 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500482 case -NFS4ERR_OLD_STATEID:
483 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800484 break;
485 case -NFS4ERR_BADOWNER:
486 /* The following works around a Linux server bug! */
487 case -NFS4ERR_BADNAME:
488 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
489 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
490 exception->retry = 1;
491 printk(KERN_WARNING "NFS: v4 server %s "
492 "does not accept raw "
493 "uid/gids. "
494 "Reenabling the idmapper.\n",
495 server->nfs_client->cl_hostname);
496 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500497 }
498 /* We failed to handle the error */
499 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500500wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400501 exception->recovering = 1;
502 return 0;
503}
504
505/* This is the error handling routine for processes that are allowed
506 * to sleep.
507 */
508int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
509{
510 struct nfs_client *clp = server->nfs_client;
511 int ret;
512
513 ret = nfs4_do_handle_exception(server, errorcode, exception);
514 if (exception->delay) {
515 ret = nfs4_delay(server->client, &exception->timeout);
516 goto out_retry;
517 }
518 if (exception->recovering) {
519 ret = nfs4_wait_clnt_recover(clp);
520 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
521 return -EIO;
522 goto out_retry;
523 }
524 return ret;
525out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500526 if (ret == 0)
527 exception->retry = 1;
528 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500529}
530
Trond Myklebust037fc982015-09-20 15:51:00 -0400531static int
532nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
533 int errorcode, struct nfs4_exception *exception)
534{
535 struct nfs_client *clp = server->nfs_client;
536 int ret;
537
538 ret = nfs4_do_handle_exception(server, errorcode, exception);
539 if (exception->delay) {
540 rpc_delay(task, nfs4_update_delay(&exception->timeout));
541 goto out_retry;
542 }
543 if (exception->recovering) {
544 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
545 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
546 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
547 goto out_retry;
548 }
549 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
550 ret = -EIO;
551 return ret;
552out_retry:
553 if (ret == 0)
554 exception->retry = 1;
555 return ret;
556}
557
558static int
559nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
560 struct nfs4_state *state, long *timeout)
561{
562 struct nfs4_exception exception = {
563 .state = state,
564 };
565
566 if (task->tk_status >= 0)
567 return 0;
568 if (timeout)
569 exception.timeout = *timeout;
570 task->tk_status = nfs4_async_handle_exception(task, server,
571 task->tk_status,
572 &exception);
573 if (exception.delay && timeout)
574 *timeout = exception.timeout;
575 if (exception.retry)
576 return -EAGAIN;
577 return 0;
578}
579
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400580/*
581 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
582 * or 'false' otherwise.
583 */
584static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
585{
586 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500587 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400588}
Trond Myklebust65de8722008-12-23 15:21:44 -0500589
Trond Myklebust452e9352010-07-31 14:29:06 -0400590static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
596}
597
Trond Myklebust452e9352010-07-31 14:29:06 -0400598static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
599{
Trond Myklebustbe824162015-07-05 14:50:46 -0400600 struct nfs_client *clp = server->nfs_client;
601
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400604}
605
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400606struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
610};
611
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800612void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400614{
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
618
619 res->sr_slot = NULL;
620}
621
622static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
623{
624 args->sa_privileged = 1;
625}
626
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400627static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400628{
629 struct nfs4_slot *slot = res->sr_slot;
630 struct nfs4_slot_table *tbl;
631
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 tbl = slot->table;
633 spin_lock(&tbl->slot_tbl_lock);
634 if (!nfs41_wake_and_assign_slot(tbl, slot))
635 nfs4_free_slot(tbl, slot);
636 spin_unlock(&tbl->slot_tbl_lock);
637
638 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400639}
640
641static int nfs40_sequence_done(struct rpc_task *task,
642 struct nfs4_sequence_res *res)
643{
644 if (res->sr_slot != NULL)
645 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400646 return 1;
647}
648
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400649#if defined(CONFIG_NFS_V4_1)
650
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400651static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400652{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500653 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400654 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500655 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400656
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400657 if (!slot)
658 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500659 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500660 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500661
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400662 /* Bump the slot sequence number */
663 if (slot->seq_done)
664 slot->seq_nr++;
665 slot->seq_done = 0;
666
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500667 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500668 /* Be nice to the server: try to ensure that the last transmitted
669 * value for highest_user_slotid <= target_highest_slotid
670 */
671 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
672 send_new_highest_used_slotid = true;
673
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500674 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500675 send_new_highest_used_slotid = false;
676 goto out_unlock;
677 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500678 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500679
680 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
681 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500682out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500683 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500684 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400685 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400686 if (waitqueue_active(&tbl->slot_waitq))
687 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400688}
689
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400690static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
691{
692 nfs41_release_slot(res->sr_slot);
693 res->sr_slot = NULL;
694}
695
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400696static int nfs41_sequence_process(struct rpc_task *task,
697 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400698{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500699 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500702 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500703 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400704
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500705 if (slot == NULL)
706 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400707 /* don't increment the sequence number if the task wasn't sent */
708 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400709 goto out;
710
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500711 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500712
Trond Myklebustac20d162012-12-15 15:36:07 -0500713 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400714 if (res->sr_status != -NFS4ERR_DELAY)
715 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500716 interrupted = true;
717 }
718
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400719 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500720 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400721 switch (res->sr_status) {
722 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400723 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400724 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500725 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500726 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500727 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400728 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
729 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500730 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400731 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500732 case 1:
733 /*
734 * sr_status remains 1 if an RPC level error occurred.
735 * The server may or may not have processed the sequence
736 * operation..
737 * Mark the slot as having hosted an interrupted RPC call.
738 */
739 slot->interrupted = 1;
740 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400741 case -NFS4ERR_DELAY:
742 /* The server detected a resend of the RPC call and
743 * returned NFS4ERR_DELAY as per Section 2.10.6.2
744 * of RFC5661.
745 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500746 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400747 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500748 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500749 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400750 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500751 case -NFS4ERR_BADSLOT:
752 /*
753 * The slot id we used was probably retired. Try again
754 * using a different slot id.
755 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400756 if (slot->seq_nr < slot->table->target_highest_slotid)
757 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500758 goto retry_nowait;
759 case -NFS4ERR_SEQ_MISORDERED:
760 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500761 * Was the last operation on this sequence interrupted?
762 * If so, retry after bumping the sequence number.
763 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400764 if (interrupted)
765 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500766 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500767 * Could this slot have been previously retired?
768 * If so, then the server may be expecting seq_nr = 1!
769 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500770 if (slot->seq_nr != 1) {
771 slot->seq_nr = 1;
772 goto retry_nowait;
773 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400774 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500775 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400776 if (interrupted)
777 goto retry_new_seq;
778 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400779 default:
780 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400781 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400782 }
783out:
784 /* The session may be reset by one of the error handlers. */
785 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500786out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500787 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400788session_recover:
789 nfs4_schedule_session_recovery(session, res->sr_status);
790 goto retry_nowait;
791retry_new_seq:
792 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500793retry_nowait:
794 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400795 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500796 task->tk_status = 0;
797 ret = 0;
798 }
799 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400800out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400801 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 goto out;
803 rpc_delay(task, NFS4_POLL_RETRY_MAX);
804 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400806
807int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
808{
809 if (!nfs41_sequence_process(task, res))
810 return 0;
811 if (res->sr_slot != NULL)
812 nfs41_sequence_free_slot(res);
813 return 1;
814
815}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500816EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400817
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400818static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
819{
820 if (res->sr_slot == NULL)
821 return 1;
822 if (res->sr_slot->table->session != NULL)
823 return nfs41_sequence_process(task, res);
824 return nfs40_sequence_done(task, res);
825}
826
827static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
828{
829 if (res->sr_slot != NULL) {
830 if (res->sr_slot->table->session != NULL)
831 nfs41_sequence_free_slot(res);
832 else
833 nfs40_sequence_free_slot(res);
834 }
835}
836
Peng Tao2c4b1312014-06-11 05:24:16 +0800837int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400838{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500839 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400841 if (!res->sr_slot->table->session)
842 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400843 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844}
Peng Tao2c4b1312014-06-11 05:24:16 +0800845EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400846
Andy Adamsonce5039c2009-04-01 09:22:13 -0400847static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
848{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400849 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400850
Trond Myklebust035168ab2010-06-16 09:52:26 -0400851 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
852
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500853 nfs4_setup_sequence(data->seq_server->nfs_client,
854 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855}
856
Andy Adamson69ab40c2009-04-01 09:22:19 -0400857static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
858{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400859 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400860
Trond Myklebust14516c32010-07-31 14:29:06 -0400861 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400862}
863
Trond Myklebust17280172012-03-11 13:11:00 -0400864static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400865 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400866 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400867};
868
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400869static void
870nfs4_sequence_process_interrupted(struct nfs_client *client,
871 struct nfs4_slot *slot, struct rpc_cred *cred)
872{
873 struct rpc_task *task;
874
875 task = _nfs41_proc_sequence(client, cred, slot, true);
876 if (!IS_ERR(task))
877 rpc_put_task_async(task);
878}
879
Chuck Lever3bd23842013-08-09 12:49:19 -0400880#else /* !CONFIG_NFS_V4_1 */
881
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400882static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
883{
884 return nfs40_sequence_done(task, res);
885}
886
887static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
888{
889 if (res->sr_slot != NULL)
890 nfs40_sequence_free_slot(res);
891}
892
Peng Tao2c4b1312014-06-11 05:24:16 +0800893int nfs4_sequence_done(struct rpc_task *task,
894 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400895{
Chuck Lever3bd23842013-08-09 12:49:19 -0400896 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400897}
Peng Tao2c4b1312014-06-11 05:24:16 +0800898EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400899
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400900static void
901nfs4_sequence_process_interrupted(struct nfs_client *client,
902 struct nfs4_slot *slot, struct rpc_cred *cred)
903{
904 WARN_ON_ONCE(1);
905 slot->interrupted = 0;
906}
907
Chuck Lever3bd23842013-08-09 12:49:19 -0400908#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400909
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400910static
911void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
912 struct nfs4_sequence_res *res,
913 struct nfs4_slot *slot)
914{
915 if (!slot)
916 return;
917 slot->privileged = args->sa_privileged ? 1 : 0;
918 args->sa_slot = slot;
919
920 res->sr_slot = slot;
921 res->sr_timestamp = jiffies;
922 res->sr_status_flags = 0;
923 res->sr_status = 1;
924
925}
926
927int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500928 struct nfs4_sequence_args *args,
929 struct nfs4_sequence_res *res,
930 struct rpc_task *task)
931{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500932 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500933 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500934 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500935
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500936 /* slot already allocated? */
937 if (res->sr_slot != NULL)
938 goto out_start;
939
Anna Schumaker76ee0352017-01-10 16:49:31 -0500940 if (session) {
941 tbl = &session->fc_slot_table;
942 task->tk_timeout = 0;
943 }
944
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400945 for (;;) {
946 spin_lock(&tbl->slot_tbl_lock);
947 /* The state manager will wait until the slot table is empty */
948 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
949 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500950
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400951 slot = nfs4_alloc_slot(tbl);
952 if (IS_ERR(slot)) {
953 /* Try again in 1/4 second */
954 if (slot == ERR_PTR(-ENOMEM))
955 task->tk_timeout = HZ >> 2;
956 goto out_sleep;
957 }
958 spin_unlock(&tbl->slot_tbl_lock);
959
960 if (likely(!slot->interrupted))
961 break;
962 nfs4_sequence_process_interrupted(client,
963 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500964 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500965
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400966 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500967
Anna Schumakerad05cc02017-01-11 13:37:06 -0500968 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500969out_start:
970 rpc_call_start(task);
971 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500972
973out_sleep:
974 if (args->sa_privileged)
975 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
976 NULL, RPC_PRIORITY_PRIVILEGED);
977 else
978 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
979 spin_unlock(&tbl->slot_tbl_lock);
980 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500981}
982EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
983
Chuck Lever9915ea72013-08-09 12:48:27 -0400984static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
985{
986 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500987 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400988 data->seq_args, data->seq_res, task);
989}
990
991static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
992{
993 struct nfs4_call_sync_data *data = calldata;
994 nfs4_sequence_done(task, data->seq_res);
995}
996
997static const struct rpc_call_ops nfs40_call_sync_ops = {
998 .rpc_call_prepare = nfs40_call_sync_prepare,
999 .rpc_call_done = nfs40_call_sync_done,
1000};
1001
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001002static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001003 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001005 struct nfs4_sequence_args *args,
1006 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001007{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 int ret;
1009 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001010 struct nfs_client *clp = server->nfs_client;
1011 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001012 .seq_server = server,
1013 .seq_args = args,
1014 .seq_res = res,
1015 };
1016 struct rpc_task_setup task_setup = {
1017 .rpc_client = clnt,
1018 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001019 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001020 .callback_data = &data
1021 };
1022
1023 task = rpc_run_task(&task_setup);
1024 if (IS_ERR(task))
1025 ret = PTR_ERR(task);
1026 else {
1027 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001028 rpc_put_task(task);
1029 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 return ret;
1031}
1032
Bryan Schumaker7c513052011-03-24 17:12:24 +00001033int nfs4_call_sync(struct rpc_clnt *clnt,
1034 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001035 struct rpc_message *msg,
1036 struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res,
1038 int cache_reply)
1039{
Chuck Levera9c92d62013-08-09 12:48:18 -04001040 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001041 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001044static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1045 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct nfs_inode *nfsi = NFS_I(dir);
1048
1049 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001050 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001051 if (cinfo->atomic && cinfo->before == dir->i_version) {
1052 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1053 nfsi->attrtimeo_timestamp = jiffies;
1054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001056 if (cinfo->before != dir->i_version)
1057 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1058 NFS_INO_INVALID_ACL;
1059 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001060 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001061 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001062 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001063 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock(&dir->i_lock);
1065}
1066
1067struct nfs4_opendata {
1068 struct kref kref;
1069 struct nfs_openargs o_arg;
1070 struct nfs_openres o_res;
1071 struct nfs_open_confirmargs c_arg;
1072 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001073 struct nfs4_string owner_name;
1074 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001075 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001077 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001079 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct nfs4_state_owner *owner;
1081 struct nfs4_state *state;
1082 struct iattr attrs;
1083 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001084 bool rpc_done;
1085 bool file_created;
1086 bool is_recover;
1087 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001088 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001090
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001091struct nfs4_open_createattrs {
1092 struct nfs4_label *label;
1093 struct iattr *sattr;
1094 const __u32 verf[2];
1095};
1096
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001097static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1098 int err, struct nfs4_exception *exception)
1099{
1100 if (err != -EINVAL)
1101 return false;
1102 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1103 return false;
1104 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1105 exception->retry = 1;
1106 return true;
1107}
1108
Trond Myklebust6ae37332015-01-30 14:21:14 -05001109static u32
1110nfs4_map_atomic_open_share(struct nfs_server *server,
1111 fmode_t fmode, int openflags)
1112{
1113 u32 res = 0;
1114
1115 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1116 case FMODE_READ:
1117 res = NFS4_SHARE_ACCESS_READ;
1118 break;
1119 case FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_WRITE;
1121 break;
1122 case FMODE_READ|FMODE_WRITE:
1123 res = NFS4_SHARE_ACCESS_BOTH;
1124 }
1125 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1126 goto out;
1127 /* Want no delegation if we're using O_DIRECT */
1128 if (openflags & O_DIRECT)
1129 res |= NFS4_SHARE_WANT_NO_DELEG;
1130out:
1131 return res;
1132}
1133
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001134static enum open_claim_type4
1135nfs4_map_atomic_open_claim(struct nfs_server *server,
1136 enum open_claim_type4 claim)
1137{
1138 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1139 return claim;
1140 switch (claim) {
1141 default:
1142 return claim;
1143 case NFS4_OPEN_CLAIM_FH:
1144 return NFS4_OPEN_CLAIM_NULL;
1145 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1146 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1147 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1148 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1149 }
1150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153{
1154 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001155 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 p->o_res.seqid = p->o_arg.seqid;
1157 p->c_res.seqid = p->c_arg.seqid;
1158 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001159 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001160 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001161 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162}
1163
Al Viro82a2c1b2011-06-22 18:30:55 -04001164static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001166 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001167 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001168 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169{
Al Viro82a2c1b2011-06-22 18:30:55 -04001170 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001171 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001173 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001174 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 struct nfs4_opendata *p;
1176
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001177 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 if (p == NULL)
1179 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001180
1181 p->f_label = nfs4_label_alloc(server, gfp_mask);
1182 if (IS_ERR(p->f_label))
1183 goto err_free_p;
1184
Kinglong Meea49c2692015-07-27 15:31:38 +08001185 p->a_label = nfs4_label_alloc(server, gfp_mask);
1186 if (IS_ERR(p->a_label))
1187 goto err_free_f;
1188
Trond Myklebust63f5f792015-01-23 19:19:25 -05001189 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1190 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001191 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001192 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001193 nfs_sb_active(dentry->d_sb);
1194 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 p->dir = parent;
1196 p->owner = sp;
1197 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001198 p->o_arg.open_flags = flags;
1199 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001200 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001201 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001202 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1203 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001204 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1205 * will return permission denied for all bits until close */
1206 if (!(flags & O_EXCL)) {
1207 /* ask server to check for all possible rights as results
1208 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001209 switch (p->o_arg.claim) {
1210 default:
1211 break;
1212 case NFS4_OPEN_CLAIM_NULL:
1213 case NFS4_OPEN_CLAIM_FH:
1214 p->o_arg.access = NFS4_ACCESS_READ |
1215 NFS4_ACCESS_MODIFY |
1216 NFS4_ACCESS_EXTEND |
1217 NFS4_ACCESS_EXECUTE;
1218 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 }
David Howells7539bba2006-08-22 20:06:09 -04001220 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001221 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1222 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001224 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001225 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001226 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001228 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001229 case NFS4_OPEN_CLAIM_NULL:
1230 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1231 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1232 p->o_arg.fh = NFS_FH(dir);
1233 break;
1234 case NFS4_OPEN_CLAIM_PREVIOUS:
1235 case NFS4_OPEN_CLAIM_FH:
1236 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1237 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001238 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001239 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001240 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001241 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001242 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001243
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001244 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001245 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247 p->c_arg.fh = &p->o_res.fh;
1248 p->c_arg.stateid = &p->o_res.stateid;
1249 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001252 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001253
1254err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001255 nfs4_label_free(p->a_label);
1256err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001257 nfs4_label_free(p->f_label);
1258err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 kfree(p);
1260err:
1261 dput(parent);
1262 return NULL;
1263}
1264
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001265static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267 struct nfs4_opendata *p = container_of(kref,
1268 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001269 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270
1271 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001272 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 if (p->state != NULL)
1274 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001276
Kinglong Meea49c2692015-07-27 15:31:38 +08001277 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001278 nfs4_label_free(p->f_label);
1279
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 dput(p->dentry);
1282 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001283 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001284 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001285 kfree(p);
1286}
1287
1288static void nfs4_opendata_put(struct nfs4_opendata *p)
1289{
1290 if (p != NULL)
1291 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292}
1293
Trond Myklebust24311f82015-09-20 10:50:17 -04001294static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1295 fmode_t fmode)
1296{
1297 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1298 case FMODE_READ|FMODE_WRITE:
1299 return state->n_rdwr != 0;
1300 case FMODE_WRITE:
1301 return state->n_wronly != 0;
1302 case FMODE_READ:
1303 return state->n_rdonly != 0;
1304 }
1305 WARN_ON_ONCE(1);
1306 return false;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310{
1311 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312
Trond Myklebust536e43d2012-01-17 22:04:26 -05001313 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 goto out;
1315 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001317 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1318 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001319 break;
1320 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001321 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1322 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001323 break;
1324 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001325 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1326 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001327 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001328out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001329 return ret;
1330}
1331
Trond Myklebust2a606182015-08-19 22:30:00 -05001332static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1333 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001334{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001335 if (delegation == NULL)
1336 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001339 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1340 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001341 switch (claim) {
1342 case NFS4_OPEN_CLAIM_NULL:
1343 case NFS4_OPEN_CLAIM_FH:
1344 break;
1345 case NFS4_OPEN_CLAIM_PREVIOUS:
1346 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1347 break;
1348 default:
1349 return 0;
1350 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001351 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001352 return 1;
1353}
1354
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001356{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001358 case FMODE_WRITE:
1359 state->n_wronly++;
1360 break;
1361 case FMODE_READ:
1362 state->n_rdonly++;
1363 break;
1364 case FMODE_READ|FMODE_WRITE:
1365 state->n_rdwr++;
1366 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001368}
1369
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001370#ifdef CONFIG_NFS_V4_1
1371static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1372{
1373 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1374 return true;
1375 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1376 return true;
1377 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1378 return true;
1379 return false;
1380}
1381#endif /* CONFIG_NFS_V4_1 */
1382
Trond Myklebustc9399f22017-11-06 15:28:01 -05001383static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1384{
1385 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1386 wake_up_all(&state->waitq);
1387}
1388
1389static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1390 const nfs4_stateid *stateid)
1391{
1392 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1393 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1394
1395 if (stateid_seqid == state_seqid + 1U ||
1396 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1397 nfs_state_log_update_open_stateid(state);
1398 else
1399 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1400}
1401
Trond Myklebust4f14c192014-02-12 19:15:06 -05001402static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001403{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001404 struct nfs_client *clp = state->owner->so_server->nfs_client;
1405 bool need_recover = false;
1406
1407 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1410 need_recover = true;
1411 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1412 need_recover = true;
1413 if (need_recover)
1414 nfs4_state_mark_reclaim_nograce(clp, state);
1415}
1416
Trond Myklebustc9399f22017-11-06 15:28:01 -05001417/*
1418 * Check for whether or not the caller may update the open stateid
1419 * to the value passed in by stateid.
1420 *
1421 * Note: This function relies heavily on the server implementing
1422 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1423 * correctly.
1424 * i.e. The stateid seqids have to be initialised to 1, and
1425 * are then incremented on every state transition.
1426 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001427static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001428 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001429{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001430 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1431 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1432 if (stateid->seqid == cpu_to_be32(1))
1433 nfs_state_log_update_open_stateid(state);
1434 else
1435 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001436 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001438
1439 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1440 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001441 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001442 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001443 return false;
1444}
1445
Trond Myklebustf95549c2015-01-23 18:06:09 -05001446static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1447{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001448 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1449 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001450 if (state->n_wronly)
1451 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1452 if (state->n_rdonly)
1453 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1454 if (state->n_rdwr)
1455 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001456 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001457}
1458
Trond Myklebust226056c2014-02-11 10:41:07 -05001459static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1460 nfs4_stateid *stateid, fmode_t fmode)
1461{
1462 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1463 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1464 case FMODE_WRITE:
1465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 break;
1467 case FMODE_READ:
1468 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1469 break;
1470 case 0:
1471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1472 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1473 clear_bit(NFS_OPEN_STATE, &state->flags);
1474 }
1475 if (stateid == NULL)
1476 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001477 /* Handle OPEN+OPEN_DOWNGRADE races */
1478 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1479 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001480 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001481 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001482 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001484 nfs4_stateid_copy(&state->stateid, stateid);
1485 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001486 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001487out:
1488 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001489}
1490
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001491static void nfs_clear_open_stateid(struct nfs4_state *state,
1492 nfs4_stateid *arg_stateid,
1493 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001494{
1495 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001496 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1497 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1498 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001500 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1501 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001502}
1503
Trond Myklebust1393d962016-09-22 13:39:13 -04001504static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001505 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001506{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001507 DEFINE_WAIT(wait);
1508 int status = 0;
1509 for (;;) {
1510
1511 if (!nfs_need_update_open_stateid(state, stateid))
1512 return;
1513 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001514 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001515 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001516 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001517 /* Rely on seqids for serialisation with NFSv4.0 */
1518 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1519 break;
1520
1521 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1522 /*
1523 * Ensure we process the state changes in the same order
1524 * in which the server processed them by delaying the
1525 * update of the stateid until we are in sequence.
1526 */
1527 write_sequnlock(&state->seqlock);
1528 spin_unlock(&state->owner->so_lock);
1529 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001530 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001531 if (!signal_pending(current)) {
1532 if (schedule_timeout(5*HZ) == 0)
1533 status = -EAGAIN;
1534 else
1535 status = 0;
1536 } else
1537 status = -EINTR;
1538 finish_wait(&state->waitq, &wait);
1539 rcu_read_lock();
1540 spin_lock(&state->owner->so_lock);
1541 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001542 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543
1544 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1545 nfs4_stateid_copy(freeme, &state->open_stateid);
1546 nfs_test_and_clear_all_open_stateid(state);
1547 }
1548
Trond Myklebuste999e802014-02-10 18:20:47 -05001549 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1550 nfs4_stateid_copy(&state->stateid, stateid);
1551 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001552 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001553 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001554}
1555
Trond Myklebustc9399f22017-11-06 15:28:01 -05001556static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001557 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001558 fmode_t fmode,
1559 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001561 /*
1562 * Protect the call to nfs4_state_set_mode_locked and
1563 * serialise the stateid update
1564 */
1565 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001566 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1567 switch (fmode) {
1568 case FMODE_READ:
1569 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1570 break;
1571 case FMODE_WRITE:
1572 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1573 break;
1574 case FMODE_READ|FMODE_WRITE:
1575 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001576 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001577 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001578 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001579}
1580
1581static void nfs_state_set_delegation(struct nfs4_state *state,
1582 const nfs4_stateid *deleg_stateid,
1583 fmode_t fmode)
1584{
1585 /*
1586 * Protect the call to nfs4_state_set_mode_locked and
1587 * serialise the stateid update
1588 */
1589 write_seqlock(&state->seqlock);
1590 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1591 set_bit(NFS_DELEGATED_STATE, &state->flags);
1592 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Trond Myklebust1393d962016-09-22 13:39:13 -04001595static int update_open_stateid(struct nfs4_state *state,
1596 const nfs4_stateid *open_stateid,
1597 const nfs4_stateid *delegation,
1598 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001599{
Trond Myklebust1393d962016-09-22 13:39:13 -04001600 struct nfs_server *server = NFS_SERVER(state->inode);
1601 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001602 struct nfs_inode *nfsi = NFS_I(state->inode);
1603 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001604 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001605 int ret = 0;
1606
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001607 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001608
1609 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001610 spin_lock(&state->owner->so_lock);
1611 if (open_stateid != NULL) {
1612 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1613 ret = 1;
1614 }
1615
Trond Myklebust34310432008-12-23 15:21:38 -05001616 deleg_cur = rcu_dereference(nfsi->delegation);
1617 if (deleg_cur == NULL)
1618 goto no_delegation;
1619
1620 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001621 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001622 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001624 goto no_delegation_unlock;
1625
1626 if (delegation == NULL)
1627 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001628 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001629 goto no_delegation_unlock;
1630
Trond Myklebustb7391f42008-12-23 15:21:52 -05001631 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001632 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001633 ret = 1;
1634no_delegation_unlock:
1635 spin_unlock(&deleg_cur->lock);
1636no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001637 if (ret)
1638 update_open_stateflags(state, fmode);
1639 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001640 rcu_read_unlock();
1641
Trond Myklebust4f14c192014-02-12 19:15:06 -05001642 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001643 nfs4_schedule_state_manager(clp);
1644 if (freeme.type != 0)
1645 nfs4_test_and_free_stateid(server, &freeme,
1646 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001647
1648 return ret;
1649}
1650
Trond Myklebust39071e62015-01-24 15:07:56 -05001651static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1652 const nfs4_stateid *stateid)
1653{
1654 struct nfs4_state *state = lsp->ls_state;
1655 bool ret = false;
1656
1657 spin_lock(&state->state_lock);
1658 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1659 goto out_noupdate;
1660 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1661 goto out_noupdate;
1662 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1663 ret = true;
1664out_noupdate:
1665 spin_unlock(&state->state_lock);
1666 return ret;
1667}
Trond Myklebust34310432008-12-23 15:21:38 -05001668
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001669static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001670{
1671 struct nfs_delegation *delegation;
1672
1673 rcu_read_lock();
1674 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001675 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 rcu_read_unlock();
1677 return;
1678 }
1679 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001680 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681}
1682
Trond Myklebust6ee41262007-07-08 14:11:36 -04001683static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001684{
1685 struct nfs4_state *state = opendata->state;
1686 struct nfs_inode *nfsi = NFS_I(state->inode);
1687 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001688 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001690 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691 nfs4_stateid stateid;
1692 int ret = -EAGAIN;
1693
Trond Myklebustaac00a82007-07-05 19:02:21 -04001694 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001696 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001700 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001701 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001702 rcu_read_lock();
1703 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001704 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001705 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001706 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001707 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001709 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001711 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001712 if (!opendata->is_recover) {
1713 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1714 if (ret != 0)
1715 goto out;
1716 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001717 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001718
1719 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001720 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001721 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001722 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723out:
1724 return ERR_PTR(ret);
1725out_return_state:
1726 atomic_inc(&state->count);
1727 return state;
1728}
1729
Andy Adamsone23008e2012-10-02 21:07:32 -04001730static void
1731nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1732{
1733 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1734 struct nfs_delegation *delegation;
1735 int delegation_flags = 0;
1736
1737 rcu_read_lock();
1738 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1739 if (delegation)
1740 delegation_flags = delegation->flags;
1741 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001742 switch (data->o_arg.claim) {
1743 default:
1744 break;
1745 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1746 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001747 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1748 "returning a delegation for "
1749 "OPEN(CLAIM_DELEGATE_CUR)\n",
1750 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001751 return;
1752 }
1753 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001754 nfs_inode_set_delegation(state->inode,
1755 data->owner->so_cred,
1756 &data->o_res);
1757 else
1758 nfs_inode_reclaim_delegation(state->inode,
1759 data->owner->so_cred,
1760 &data->o_res);
1761}
1762
1763/*
1764 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1765 * and update the nfs4_state.
1766 */
1767static struct nfs4_state *
1768_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1769{
1770 struct inode *inode = data->state->inode;
1771 struct nfs4_state *state = data->state;
1772 int ret;
1773
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001774 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001775 if (data->rpc_status)
1776 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001777 /* cached opens have already been processed */
1778 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 }
1780
Andy Adamsone23008e2012-10-02 21:07:32 -04001781 ret = nfs_refresh_inode(inode, &data->f_attr);
1782 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001783 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001784
1785 if (data->o_res.delegation_type != 0)
1786 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001787update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001788 update_open_stateid(state, &data->o_res.stateid, NULL,
1789 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001790 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001791
1792 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001793}
1794
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001795static struct inode *
1796nfs4_opendata_get_inode(struct nfs4_opendata *data)
1797{
1798 struct inode *inode;
1799
1800 switch (data->o_arg.claim) {
1801 case NFS4_OPEN_CLAIM_NULL:
1802 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1803 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1804 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1805 return ERR_PTR(-EAGAIN);
1806 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1807 &data->f_attr, data->f_label);
1808 break;
1809 default:
1810 inode = d_inode(data->dentry);
1811 ihold(inode);
1812 nfs_refresh_inode(inode, &data->f_attr);
1813 }
1814 return inode;
1815}
1816
Andy Adamsone23008e2012-10-02 21:07:32 -04001817static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001818nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1819{
1820 struct nfs4_state *state;
1821 struct inode *inode;
1822
1823 inode = nfs4_opendata_get_inode(data);
1824 if (IS_ERR(inode))
1825 return ERR_CAST(inode);
1826 if (data->state != NULL && data->state->inode == inode) {
1827 state = data->state;
1828 atomic_inc(&state->count);
1829 } else
1830 state = nfs4_get_open_state(inode, data->owner);
1831 iput(inode);
1832 if (state == NULL)
1833 state = ERR_PTR(-ENOMEM);
1834 return state;
1835}
1836
Andy Adamsone23008e2012-10-02 21:07:32 -04001837static struct nfs4_state *
1838_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001840 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001843 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001844 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 goto out;
1846 }
1847
Trond Myklebust75e8c482017-08-08 10:38:07 -04001848 state = nfs4_opendata_find_nfs4_state(data);
1849 if (IS_ERR(state))
1850 goto out;
1851
Andy Adamsone23008e2012-10-02 21:07:32 -04001852 if (data->o_res.delegation_type != 0)
1853 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001854 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001855 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001857 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001858 return state;
1859}
1860
Andy Adamsone23008e2012-10-02 21:07:32 -04001861static struct nfs4_state *
1862nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1863{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001864 struct nfs4_state *ret;
1865
Andy Adamsone23008e2012-10-02 21:07:32 -04001866 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001867 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1868 else
1869 ret = _nfs4_opendata_to_nfs4_state(data);
1870 nfs4_sequence_free_slot(&data->o_res.seq_res);
1871 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001872}
1873
Trond Myklebust864472e2006-01-03 09:55:15 +01001874static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1875{
1876 struct nfs_inode *nfsi = NFS_I(state->inode);
1877 struct nfs_open_context *ctx;
1878
1879 spin_lock(&state->inode->i_lock);
1880 list_for_each_entry(ctx, &nfsi->open_files, list) {
1881 if (ctx->state != state)
1882 continue;
1883 get_nfs_open_context(ctx);
1884 spin_unlock(&state->inode->i_lock);
1885 return ctx;
1886 }
1887 spin_unlock(&state->inode->i_lock);
1888 return ERR_PTR(-ENOENT);
1889}
1890
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001891static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1892 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001893{
1894 struct nfs4_opendata *opendata;
1895
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001896 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001897 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001898 if (opendata == NULL)
1899 return ERR_PTR(-ENOMEM);
1900 opendata->state = state;
1901 atomic_inc(&state->count);
1902 return opendata;
1903}
1904
Trond Myklebust24311f82015-09-20 10:50:17 -04001905static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1906 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001907{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001908 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001909 int ret;
1910
Trond Myklebust24311f82015-09-20 10:50:17 -04001911 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001912 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001913 opendata->o_arg.open_flags = 0;
1914 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001915 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1916 NFS_SB(opendata->dentry->d_sb),
1917 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001918 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1919 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1920 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001921 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 if (ret != 0)
1923 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001924 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001925 if (IS_ERR(newstate))
1926 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001927 if (newstate != opendata->state)
1928 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001929 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001930 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001931}
1932
1933static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1934{
Trond Myklebust864472e2006-01-03 09:55:15 +01001935 int ret;
1936
Trond Myklebust4f14c192014-02-12 19:15:06 -05001937 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1938 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1939 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1940 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001941 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001942 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001943 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001944 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001945 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1946 if (ret != 0)
1947 return ret;
1948 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1949 if (ret != 0)
1950 return ret;
1951 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1952 if (ret != 0)
1953 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001954 /*
1955 * We may have performed cached opens for all three recoveries.
1956 * Check if we need to update the current stateid.
1957 */
1958 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001959 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001960 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001961 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001962 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001963 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001964 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001965 return 0;
1966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * OPEN_RECLAIM:
1970 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001972static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001974 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001975 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001976 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int status;
1978
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001979 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1980 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001981 if (IS_ERR(opendata))
1982 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001983 rcu_read_lock();
1984 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001985 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001986 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001987 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001988 opendata->o_arg.u.delegation_type = delegation_type;
1989 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001990 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return status;
1992}
1993
Trond Myklebust539cd032007-06-05 11:46:42 -04001994static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 struct nfs_server *server = NFS_SERVER(state->inode);
1997 struct nfs4_exception exception = { };
1998 int err;
1999 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002000 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002001 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002002 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2003 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002004 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002005 break;
2006 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 } while (exception.retry);
2008 return err;
2009}
2010
Trond Myklebust864472e2006-01-03 09:55:15 +01002011static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2012{
2013 struct nfs_open_context *ctx;
2014 int ret;
2015
2016 ctx = nfs4_state_find_open_context(state);
2017 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002018 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002019 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002020 put_nfs_open_context(ctx);
2021 return ret;
2022}
2023
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002024static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002026 switch (err) {
2027 default:
2028 printk(KERN_ERR "NFS: %s: unhandled error "
2029 "%d.\n", __func__, err);
2030 case 0:
2031 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002032 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002033 case -ESTALE:
2034 break;
2035 case -NFS4ERR_BADSESSION:
2036 case -NFS4ERR_BADSLOT:
2037 case -NFS4ERR_BAD_HIGH_SLOT:
2038 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2039 case -NFS4ERR_DEADSESSION:
2040 set_bit(NFS_DELEGATED_STATE, &state->flags);
2041 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2042 return -EAGAIN;
2043 case -NFS4ERR_STALE_CLIENTID:
2044 case -NFS4ERR_STALE_STATEID:
2045 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002046 /* Don't recall a delegation if it was lost */
2047 nfs4_schedule_lease_recovery(server->nfs_client);
2048 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002049 case -NFS4ERR_MOVED:
2050 nfs4_schedule_migration_recovery(server);
2051 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002052 case -NFS4ERR_LEASE_MOVED:
2053 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2054 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002055 case -NFS4ERR_DELEG_REVOKED:
2056 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002057 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002059 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002060 nfs_inode_find_state_and_recover(state->inode,
2061 stateid);
2062 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002063 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002064 case -NFS4ERR_DELAY:
2065 case -NFS4ERR_GRACE:
2066 set_bit(NFS_DELEGATED_STATE, &state->flags);
2067 ssleep(1);
2068 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002069 case -ENOMEM:
2070 case -NFS4ERR_DENIED:
2071 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2072 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return err;
2075}
2076
Trond Myklebust24311f82015-09-20 10:50:17 -04002077int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2078 struct nfs4_state *state, const nfs4_stateid *stateid,
2079 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002080{
2081 struct nfs_server *server = NFS_SERVER(state->inode);
2082 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002083 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002084
2085 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2086 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2087 if (IS_ERR(opendata))
2088 return PTR_ERR(opendata);
2089 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002090 write_seqlock(&state->seqlock);
2091 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2092 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002093 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2094 switch (type & (FMODE_READ|FMODE_WRITE)) {
2095 case FMODE_READ|FMODE_WRITE:
2096 case FMODE_WRITE:
2097 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2098 if (err)
2099 break;
2100 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2101 if (err)
2102 break;
2103 case FMODE_READ:
2104 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2105 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002106 nfs4_opendata_put(opendata);
2107 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2108}
2109
Chuck Leverbe05c862013-08-09 12:49:47 -04002110static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2111{
2112 struct nfs4_opendata *data = calldata;
2113
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002114 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2115 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002116}
2117
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002118static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2119{
2120 struct nfs4_opendata *data = calldata;
2121
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002122 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002123
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002124 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002125 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002126 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002127 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002128 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002129 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002130 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002131}
2132
2133static void nfs4_open_confirm_release(void *calldata)
2134{
2135 struct nfs4_opendata *data = calldata;
2136 struct nfs4_state *state = NULL;
2137
2138 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002139 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002140 goto out_free;
2141 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002142 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002143 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002144 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002145 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002146 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002148 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002149}
2150
2151static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002152 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002153 .rpc_call_done = nfs4_open_confirm_done,
2154 .rpc_release = nfs4_open_confirm_release,
2155};
2156
2157/*
2158 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2159 */
2160static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2161{
David Howells2b0143b2015-03-17 22:25:59 +00002162 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002163 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002164 struct rpc_message msg = {
2165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2166 .rpc_argp = &data->c_arg,
2167 .rpc_resp = &data->c_res,
2168 .rpc_cred = data->owner->so_cred,
2169 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002170 struct rpc_task_setup task_setup_data = {
2171 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002172 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 .callback_ops = &nfs4_open_confirm_ops,
2174 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002175 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .flags = RPC_TASK_ASYNC,
2177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 int status;
2179
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002180 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002181 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002182 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002183 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002184 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002185 if (data->is_recover)
2186 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002187 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002188 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002189 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002190 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002191 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002192 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002193 smp_wmb();
2194 } else
2195 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002196 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return status;
2198}
2199
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 struct nfs4_opendata *data = calldata;
2203 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002204 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002205 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002206
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002208 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002209 /*
2210 * Check if we still need to send an OPEN call, or if we can use
2211 * a delegation instead.
2212 */
2213 if (data->state != NULL) {
2214 struct nfs_delegation *delegation;
2215
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002216 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002217 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002218 rcu_read_lock();
2219 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002220 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002221 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002222 rcu_read_unlock();
2223 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002224 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002225 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002226 switch (claim) {
2227 default:
2228 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002229 case NFS4_OPEN_CLAIM_PREVIOUS:
2230 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2231 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002232 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002233 case NFS4_OPEN_CLAIM_FH:
2234 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002235 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002236 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002237 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002238 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002239 &data->o_res.seq_res,
2240 task) != 0)
2241 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002242
2243 /* Set the create mode (note dependency on the session type) */
2244 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2245 if (data->o_arg.open_flags & O_EXCL) {
2246 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2247 if (nfs4_has_persistent_session(clp))
2248 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2249 else if (clp->cl_mvops->minor_version > 0)
2250 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2251 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002252 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002253unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002254 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002255 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002256out_no_action:
2257 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002258out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002259 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002260}
2261
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002262static void nfs4_open_done(struct rpc_task *task, void *calldata)
2263{
2264 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002267
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002268 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002269 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002270
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002271 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002272 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2273 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002274 case S_IFREG:
2275 break;
2276 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002277 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002278 break;
2279 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002280 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002281 break;
2282 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002284 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002285 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002286 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002287 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2288 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002289 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002290 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002292
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002293static void nfs4_open_release(void *calldata)
2294{
2295 struct nfs4_opendata *data = calldata;
2296 struct nfs4_state *state = NULL;
2297
2298 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002299 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002300 goto out_free;
2301 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002302 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303 goto out_free;
2304 /* In case we need an open_confirm, no cleanup! */
2305 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2306 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002308 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002309 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002311 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002312}
2313
2314static const struct rpc_call_ops nfs4_open_ops = {
2315 .rpc_call_prepare = nfs4_open_prepare,
2316 .rpc_call_done = nfs4_open_done,
2317 .rpc_release = nfs4_open_release,
2318};
2319
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002320static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002321{
David Howells2b0143b2015-03-17 22:25:59 +00002322 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002323 struct nfs_server *server = NFS_SERVER(dir);
2324 struct nfs_openargs *o_arg = &data->o_arg;
2325 struct nfs_openres *o_res = &data->o_res;
2326 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002327 struct rpc_message msg = {
2328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2329 .rpc_argp = o_arg,
2330 .rpc_resp = o_res,
2331 .rpc_cred = data->owner->so_cred,
2332 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002333 struct rpc_task_setup task_setup_data = {
2334 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002335 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002336 .callback_ops = &nfs4_open_ops,
2337 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002338 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002339 .flags = RPC_TASK_ASYNC,
2340 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002341 int status;
2342
Chuck Levera9c92d62013-08-09 12:48:18 -04002343 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002344 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002345 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002346 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002347 data->cancelled = false;
2348 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002349 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002350 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002351 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002352 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002353 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002354 if (IS_ERR(task))
2355 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002356 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002357 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002358 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002359 smp_wmb();
2360 } else
2361 status = data->rpc_status;
2362 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002363
2364 return status;
2365}
2366
2367static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2368{
David Howells2b0143b2015-03-17 22:25:59 +00002369 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002370 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002371 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002372
2373 status = nfs4_run_open_task(data, 1);
2374 if (status != 0 || !data->rpc_done)
2375 return status;
2376
Trond Myklebust6926afd2012-01-07 13:22:46 -05002377 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2378
Anna Schumakerd7e98252017-01-11 16:13:29 -05002379 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002380 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002381
2382 return status;
2383}
2384
Trond Myklebustf3792d62014-07-10 08:54:32 -04002385/*
2386 * Additional permission checks in order to distinguish between an
2387 * open for read, and an open for execute. This works around the
2388 * fact that NFSv4 OPEN treats read and execute permissions as being
2389 * the same.
2390 * Note that in the non-execute case, we want to turn off permission
2391 * checking if we just created a new file (POSIX open() semantics).
2392 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002393static int nfs4_opendata_access(struct rpc_cred *cred,
2394 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002395 struct nfs4_state *state, fmode_t fmode,
2396 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002397{
2398 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002399 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002400
2401 /* access call failed or for some reason the server doesn't
2402 * support any access modes -- defer access call until later */
2403 if (opendata->o_res.access_supported == 0)
2404 return 0;
2405
2406 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002407 /*
2408 * Use openflags to check for exec, because fmode won't
2409 * always have FMODE_EXEC set when file open for exec.
2410 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002411 if (openflags & __FMODE_EXEC) {
2412 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002413 if (S_ISDIR(state->inode->i_mode))
2414 mask = NFS4_ACCESS_LOOKUP;
2415 else
2416 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002417 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002418 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002419
2420 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002421 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2422 nfs_access_add_cache(state->inode, &cache);
2423
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002424 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2425 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002426 return 0;
2427
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002428 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002429}
2430
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002431/*
2432 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2433 */
2434static int _nfs4_proc_open(struct nfs4_opendata *data)
2435{
David Howells2b0143b2015-03-17 22:25:59 +00002436 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002437 struct nfs_server *server = NFS_SERVER(dir);
2438 struct nfs_openargs *o_arg = &data->o_arg;
2439 struct nfs_openres *o_res = &data->o_res;
2440 int status;
2441
2442 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002443 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002444 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002445 if (status != 0) {
2446 if (status == -NFS4ERR_BADNAME &&
2447 !(o_arg->open_flags & O_CREAT))
2448 return -ENOENT;
2449 return status;
2450 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002451
Trond Myklebust6926afd2012-01-07 13:22:46 -05002452 nfs_fattr_map_and_free_names(server, &data->f_attr);
2453
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002454 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002455 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002456 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002457 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002458 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002459 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002460 update_changeattr(dir, &o_res->cinfo,
2461 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002462 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002463 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2464 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002466 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002468 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002470 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2471 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002472 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002473 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477/*
2478 * OPEN_EXPIRED:
2479 * reclaim state on the server after a network partition.
2480 * Assumes caller holds the appropriate lock
2481 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002482static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002484 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002485 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002487 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002488 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002489 if (IS_ERR(opendata))
2490 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002491 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002492 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002493 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002494 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002498static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002499{
Trond Myklebust539cd032007-06-05 11:46:42 -04002500 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002501 struct nfs4_exception exception = { };
2502 int err;
2503
2504 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002505 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002506 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002507 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2508 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002509 switch (err) {
2510 default:
2511 goto out;
2512 case -NFS4ERR_GRACE:
2513 case -NFS4ERR_DELAY:
2514 nfs4_handle_exception(server, err, &exception);
2515 err = 0;
2516 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002517 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002518out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002519 return err;
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002525 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Trond Myklebust864472e2006-01-03 09:55:15 +01002527 ctx = nfs4_state_find_open_context(state);
2528 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002529 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002530 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002531 put_nfs_open_context(ctx);
2532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
Trond Myklebust41020b62016-09-22 13:38:58 -04002535static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2536 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002537{
Trond Myklebust41020b62016-09-22 13:38:58 -04002538 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002539 write_seqlock(&state->seqlock);
2540 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2541 write_sequnlock(&state->seqlock);
2542 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2543}
2544
2545static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2546{
2547 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002548 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002549}
2550
2551static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2552{
2553 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2554 nfs40_clear_delegation_stateid(state);
2555 return nfs4_open_expired(sp, state);
2556}
2557
Trond Myklebust45870d62016-09-22 13:38:59 -04002558static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2559 nfs4_stateid *stateid,
2560 struct rpc_cred *cred)
2561{
2562 return -NFS4ERR_BAD_STATEID;
2563}
2564
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002565#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002566static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2567 nfs4_stateid *stateid,
2568 struct rpc_cred *cred)
2569{
2570 int status;
2571
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002572 switch (stateid->type) {
2573 default:
2574 break;
2575 case NFS4_INVALID_STATEID_TYPE:
2576 case NFS4_SPECIAL_STATEID_TYPE:
2577 return -NFS4ERR_BAD_STATEID;
2578 case NFS4_REVOKED_STATEID_TYPE:
2579 goto out_free;
2580 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002581
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002582 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002583 switch (status) {
2584 case -NFS4ERR_EXPIRED:
2585 case -NFS4ERR_ADMIN_REVOKED:
2586 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002587 break;
2588 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002589 return status;
2590 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002591out_free:
2592 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002593 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002594 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002595}
2596
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002597static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002598{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002599 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002600 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002601 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002602 struct rpc_cred *cred;
2603 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002604
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002605 /* Get the delegation credential for use by test/free_stateid */
2606 rcu_read_lock();
2607 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002608 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002609 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002610 return;
2611 }
2612
2613 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002614 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2615 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2616 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002617 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002618 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002619 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002621
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002622 cred = get_rpccred(delegation->cred);
2623 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002624 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002626 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002627 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002628
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002629 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002630}
2631
2632/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002633 * nfs41_check_expired_locks - possibly free a lock stateid
2634 *
2635 * @state: NFSv4 state for an inode
2636 *
2637 * Returns NFS_OK if recovery for this stateid is now finished.
2638 * Otherwise a negative NFS4ERR value is returned.
2639 */
2640static int nfs41_check_expired_locks(struct nfs4_state *state)
2641{
2642 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002643 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002644 struct nfs_server *server = NFS_SERVER(state->inode);
2645
2646 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2647 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002648
2649 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002650 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2651 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2652 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2653
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002654 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002655 spin_unlock(&state->state_lock);
2656
2657 nfs4_put_lock_state(prev);
2658 prev = lsp;
2659
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002660 status = nfs41_test_and_free_expired_stateid(server,
2661 &lsp->ls_stateid,
2662 cred);
2663 trace_nfs4_test_lock_stateid(state, lsp, status);
2664 if (status == -NFS4ERR_EXPIRED ||
2665 status == -NFS4ERR_BAD_STATEID) {
2666 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002667 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002668 if (!recover_lost_locks)
2669 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2670 } else if (status != NFS_OK) {
2671 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002672 nfs4_put_lock_state(prev);
2673 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002674 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002675 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002676 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002677 }
2678 spin_unlock(&state->state_lock);
2679 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002680out:
2681 return ret;
2682}
2683
2684/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002685 * nfs41_check_open_stateid - possibly free an open stateid
2686 *
2687 * @state: NFSv4 state for an inode
2688 *
2689 * Returns NFS_OK if recovery for this stateid is now finished.
2690 * Otherwise a negative NFS4ERR value is returned.
2691 */
2692static int nfs41_check_open_stateid(struct nfs4_state *state)
2693{
2694 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002695 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002696 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002697 int status;
2698
Trond Myklebustb134fc42016-09-22 13:39:16 -04002699 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002700 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2701 if (nfs4_have_delegation(state->inode, state->state))
2702 return NFS_OK;
2703 return -NFS4ERR_OPENMODE;
2704 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002705 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002706 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002707 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002708 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002709 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002710 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2711 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2712 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002713 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002714 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002715 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002716 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002717 if (nfs_open_stateid_recover_openmode(state))
2718 return -NFS4ERR_OPENMODE;
2719 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002720}
2721
2722static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2723{
Chuck Levereb64cf92012-07-11 16:30:05 -04002724 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002725
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002726 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002727 status = nfs41_check_expired_locks(state);
2728 if (status != NFS_OK)
2729 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002730 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002731 if (status != NFS_OK)
2732 status = nfs4_open_expired(sp, state);
2733 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002734}
2735#endif
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002738 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2739 * fields corresponding to attributes that were used to store the verifier.
2740 * Make sure we clobber those fields in the later setattr call
2741 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002742static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2743 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002744{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002745 const u32 *attrset = opendata->o_res.attrset;
2746
2747 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002748 !(sattr->ia_valid & ATTR_ATIME_SET))
2749 sattr->ia_valid |= ATTR_ATIME;
2750
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002751 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002752 !(sattr->ia_valid & ATTR_MTIME_SET))
2753 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002754
2755 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002756 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002757 (attrset[1] & FATTR4_WORD1_MODE ||
2758 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002759 sattr->ia_valid &= ~ATTR_MODE;
2760
2761 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2762 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002763}
2764
Trond Myklebustc21443c2013-02-07 14:26:21 -05002765static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2766 fmode_t fmode,
2767 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002768 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002769{
2770 struct nfs4_state_owner *sp = opendata->owner;
2771 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002772 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002773 struct nfs4_state *state;
2774 unsigned int seq;
2775 int ret;
2776
2777 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2778
2779 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002780 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002781 goto out;
2782
2783 state = nfs4_opendata_to_nfs4_state(opendata);
2784 ret = PTR_ERR(state);
2785 if (IS_ERR(state))
2786 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002787 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002788 if (server->caps & NFS_CAP_POSIX_LOCK)
2789 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002790 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2791 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002792
Trond Myklebust275bb302013-05-29 13:11:28 -04002793 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002794 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002795 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002796 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002797 alias = d_exact_alias(dentry, state->inode);
2798 if (!alias)
2799 alias = d_splice_alias(igrab(state->inode), dentry);
2800 /* d_splice_alias() can't fail here - it's a non-directory */
2801 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002802 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002803 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002804 }
2805 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002806 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002807 }
2808
Trond Myklebustc21443c2013-02-07 14:26:21 -05002809 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2810 if (ret != 0)
2811 goto out;
2812
David Howells2b0143b2015-03-17 22:25:59 +00002813 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002814 nfs_inode_attach_open_context(ctx);
2815 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2816 nfs4_schedule_stateid_recovery(server, state);
2817 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002818out:
2819 return ret;
2820}
2821
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002822/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002823 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 */
Andy Adamson82be4172012-05-23 05:02:35 -04002825static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002826 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002827 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002828 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002829 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 struct nfs4_state_owner *sp;
2832 struct nfs4_state *state = NULL;
2833 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002834 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002835 struct dentry *dentry = ctx->dentry;
2836 struct rpc_cred *cred = ctx->cred;
2837 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2838 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002839 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002840 struct iattr *sattr = c->sattr;
2841 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002842 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002843 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002847 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2848 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2850 goto out_err;
2851 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002852 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002853 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002854 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002855 if (d_really_is_positive(dentry))
2856 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002857 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002858 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002859 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002860 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2861 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002862 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002863 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
David Quigley14c43f72013-05-22 12:50:43 -04002865 if (label) {
2866 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2867 if (IS_ERR(olabel)) {
2868 status = PTR_ERR(olabel);
2869 goto err_opendata_put;
2870 }
2871 }
2872
Trond Myklebuste911b812014-03-26 13:24:37 -07002873 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2874 if (!opendata->f_attr.mdsthreshold) {
2875 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2876 if (!opendata->f_attr.mdsthreshold)
2877 goto err_free_label;
2878 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002879 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002880 }
David Howells2b0143b2015-03-17 22:25:59 +00002881 if (d_really_is_positive(dentry))
2882 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002883
Trond Myklebust3efb9722013-05-29 13:17:04 -04002884 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002885 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002886 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002887 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002888
NeilBrownefcbc042015-07-30 13:00:56 +10002889 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002890 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002891 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002892 /*
2893 * send create attributes which was not set by open
2894 * with an extra setattr.
2895 */
2896 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2897 nfs_fattr_init(opendata->o_res.f_attr);
2898 status = nfs4_do_setattr(state->inode, cred,
2899 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002900 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002901 if (status == 0) {
2902 nfs_setattr_update_inode(state->inode, sattr,
2903 opendata->o_res.f_attr);
2904 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2905 }
David Quigley1775fd32013-05-22 12:50:42 -04002906 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002907 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002908 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002909 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002910
Trond Myklebuste911b812014-03-26 13:24:37 -07002911 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002912 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002913 opendata->f_attr.mdsthreshold = NULL;
2914 }
Andy Adamson82be4172012-05-23 05:02:35 -04002915
David Quigley14c43f72013-05-22 12:50:43 -04002916 nfs4_label_free(olabel);
2917
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002918 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002921err_free_label:
2922 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002923err_opendata_put:
2924 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002925err_put_state_owner:
2926 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return status;
2929}
2930
2931
Andy Adamson82be4172012-05-23 05:02:35 -04002932static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002933 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002934 int flags,
2935 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002936 struct nfs4_label *label,
2937 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002939 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 struct nfs4_exception exception = { };
2941 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002942 struct nfs4_open_createattrs c = {
2943 .label = label,
2944 .sattr = sattr,
2945 .verf = {
2946 [0] = (__u32)jiffies,
2947 [1] = (__u32)current->pid,
2948 },
2949 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 int status;
2951
2952 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002953 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002954 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002955 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (status == 0)
2957 break;
2958 /* NOTE: BAD_SEQID means the server and client disagree about the
2959 * book-keeping w.r.t. state-changing operations
2960 * (OPEN/CLOSE/LOCK/LOCKU...)
2961 * It is actually a sign of a bug on the client or on the server.
2962 *
2963 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002964 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 * have unhashed the old state_owner for us, and that we can
2966 * therefore safely retry using a new one. We should still warn
2967 * the user though...
2968 */
2969 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002970 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002971 " returned a bad sequence-id error!\n",
2972 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 exception.retry = 1;
2974 continue;
2975 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002976 /*
2977 * BAD_STATEID on OPEN means that the server cancelled our
2978 * state before it received the OPEN_CONFIRM.
2979 * Recover by retrying the request as per the discussion
2980 * on Page 181 of RFC3530.
2981 */
2982 if (status == -NFS4ERR_BAD_STATEID) {
2983 exception.retry = 1;
2984 continue;
2985 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002986 if (status == -EAGAIN) {
2987 /* We must have found a delegation */
2988 exception.retry = 1;
2989 continue;
2990 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002991 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2992 continue;
2993 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 status, &exception));
2995 } while (exception.retry);
2996 return res;
2997}
2998
Trond Myklebust8487c472016-06-26 08:44:35 -04002999static int _nfs4_do_setattr(struct inode *inode,
3000 struct nfs_setattrargs *arg,
3001 struct nfs_setattrres *res,
3002 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003003 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003004{
3005 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003006 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3008 .rpc_argp = arg,
3009 .rpc_resp = res,
3010 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003011 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003012 struct rpc_cred *delegation_cred = NULL;
3013 unsigned long timestamp = jiffies;
3014 fmode_t fmode;
3015 bool truncate;
3016 int status;
3017
3018 nfs_fattr_init(res->fattr);
3019
3020 /* Servers should only apply open mode checks for file size changes */
3021 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3022 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3023
3024 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3025 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003026 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003027 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003028 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003029 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003030 l_ctx = nfs_get_lock_context(ctx);
3031 if (IS_ERR(l_ctx))
3032 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003033 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3034 &arg->stateid, &delegation_cred);
3035 nfs_put_lock_context(l_ctx);
3036 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003037 return -EBADF;
3038 } else
3039 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3040 if (delegation_cred)
3041 msg.rpc_cred = delegation_cred;
3042
3043 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3044
3045 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003046 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003047 renew_lease(server, timestamp);
3048 trace_nfs4_setattr(inode, &arg->stateid, status);
3049 return status;
3050}
3051
3052static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3053 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003054 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003055 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003057 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003058 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003059 struct nfs_setattrargs arg = {
3060 .fh = NFS_FH(inode),
3061 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 .server = server,
3063 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003064 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003065 };
3066 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003068 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003070 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003071 struct nfs4_exception exception = {
3072 .state = state,
3073 .inode = inode,
3074 .stateid = &arg.stateid,
3075 };
3076 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
David Quigleyaa9c2662013-05-22 12:50:44 -04003078 arg.bitmask = nfs4_bitmask(server, ilabel);
3079 if (ilabel)
3080 arg.bitmask = nfs4_bitmask(server, olabel);
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003083 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003084 switch (err) {
3085 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003086 if (!(sattr->ia_valid & ATTR_SIZE)) {
3087 pr_warn_once("NFSv4: server %s is incorrectly "
3088 "applying open mode checks to "
3089 "a SETATTR that is not "
3090 "changing file size.\n",
3091 server->nfs_client->cl_hostname);
3092 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003093 if (state && !(state->state & FMODE_WRITE)) {
3094 err = -EBADF;
3095 if (sattr->ia_valid & ATTR_OPEN)
3096 err = -EACCES;
3097 goto out;
3098 }
3099 }
3100 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return err;
3104}
3105
Peng Tao500d7012015-09-22 11:35:22 +08003106static bool
3107nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3108{
3109 if (inode == NULL || !nfs_have_layout(inode))
3110 return false;
3111
3112 return pnfs_wait_on_layoutreturn(inode, task);
3113}
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115struct nfs4_closedata {
3116 struct inode *inode;
3117 struct nfs4_state *state;
3118 struct nfs_closeargs arg;
3119 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003120 struct {
3121 struct nfs4_layoutreturn_args arg;
3122 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003123 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003124 u32 roc_barrier;
3125 bool roc;
3126 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003127 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003128 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129};
3130
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003132{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003133 struct nfs4_closedata *calldata = data;
3134 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003135 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003136
Trond Myklebustcf805162016-11-15 14:56:07 -05003137 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003138 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3139 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003140 nfs4_put_open_state(calldata->state);
3141 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003142 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003143 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003144 kfree(calldata);
3145}
3146
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003149 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003152 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Chuck Levera3ca5652012-03-01 17:00:40 -05003154 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003155 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3156 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003157 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003158
3159 /* Handle Layoutreturn errors */
3160 if (calldata->arg.lr_args && task->tk_status != 0) {
3161 switch (calldata->res.lr_ret) {
3162 default:
3163 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3164 break;
3165 case 0:
3166 calldata->arg.lr_args = NULL;
3167 calldata->res.lr_res = NULL;
3168 break;
3169 case -NFS4ERR_ADMIN_REVOKED:
3170 case -NFS4ERR_DELEG_REVOKED:
3171 case -NFS4ERR_EXPIRED:
3172 case -NFS4ERR_BAD_STATEID:
3173 case -NFS4ERR_OLD_STATEID:
3174 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3175 case -NFS4ERR_WRONG_CRED:
3176 calldata->arg.lr_args = NULL;
3177 calldata->res.lr_res = NULL;
3178 calldata->res.lr_ret = 0;
3179 rpc_restart_call_prepare(task);
3180 return;
3181 }
3182 }
3183
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003184 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 * the state_owner. we keep this around to process errors
3186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 switch (task->tk_status) {
3188 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003189 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003190 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003191 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003192 case -NFS4ERR_ACCESS:
3193 if (calldata->arg.bitmask != NULL) {
3194 calldata->arg.bitmask = NULL;
3195 calldata->res.fattr = NULL;
3196 task->tk_status = 0;
3197 rpc_restart_call_prepare(task);
3198 goto out_release;
3199
3200 }
3201 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003202 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003204 case -NFS4ERR_EXPIRED:
3205 nfs4_free_revoked_stateid(server,
3206 &calldata->arg.stateid,
3207 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003208 case -NFS4ERR_OLD_STATEID:
3209 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003210 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003211 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003212 rpc_restart_call_prepare(task);
3213 goto out_release;
3214 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003215 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003218 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003219 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003220 goto out_release;
3221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003223 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3224 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003225out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003226 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003227 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003228 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003231static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003233 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003234 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003235 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003236 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003237 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003238
Chuck Levera3ca5652012-03-01 17:00:40 -05003239 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003240 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003241 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003242
Trond Myklebust88069f72009-12-08 08:33:16 -05003243 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003244 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003245 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3246 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3247 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003248 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003249 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003250 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003251 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003252 if (state->n_rdonly == 0)
3253 call_close |= is_rdonly;
3254 else if (is_rdonly)
3255 calldata->arg.fmode |= FMODE_READ;
3256 if (state->n_wronly == 0)
3257 call_close |= is_wronly;
3258 else if (is_wronly)
3259 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003260 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3261 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003262 } else if (is_rdwr)
3263 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3264
Trond Myklebust5cc78612016-11-14 11:19:56 -05003265 if (!nfs4_valid_open_stateid(state) ||
3266 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003267 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003268 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003269
3270 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003271 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003272 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003273 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003274
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003275 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003276 nfs_release_seqid(calldata->arg.seqid);
3277 goto out_wait;
3278 }
3279
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003280 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003281 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003282
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003283 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003284 /* Close-to-open cache consistency revalidation */
3285 if (!nfs4_have_delegation(inode, FMODE_READ))
3286 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3287 else
3288 calldata->arg.bitmask = NULL;
3289 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003290
Trond Myklebust6ae37332015-01-30 14:21:14 -05003291 calldata->arg.share_access =
3292 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3293 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003294
Trond Myklebustd8d84982016-12-19 12:14:44 -05003295 if (calldata->res.fattr == NULL)
3296 calldata->arg.bitmask = NULL;
3297 else if (calldata->arg.bitmask == NULL)
3298 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003299 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003300 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003301 &calldata->arg.seq_args,
3302 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003303 task) != 0)
3304 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003305 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003306 return;
3307out_no_action:
3308 task->tk_action = NULL;
3309out_wait:
3310 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003313static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003314 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003315 .rpc_call_done = nfs4_close_done,
3316 .rpc_release = nfs4_free_closedata,
3317};
3318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319/*
3320 * It is possible for data to be read/written from a mem-mapped file
3321 * after the sys_close call (which hits the vfs layer as a flush).
3322 * This means that we can't safely call nfsv4 close on a file until
3323 * the inode is cleared. This in turn means that we are not good
3324 * NFSv4 citizens - we do not indicate to the server to update the file's
3325 * share state even when we are done with one of the three share
3326 * stateid's in the inode.
3327 *
3328 * NOTE: Caller must be holding the sp->so_owner semaphore!
3329 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003330int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003332 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003333 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003335 struct nfs4_state_owner *sp = state->owner;
3336 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003337 struct rpc_message msg = {
3338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3339 .rpc_cred = state->owner->so_cred,
3340 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003341 struct rpc_task_setup task_setup_data = {
3342 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003343 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003344 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003345 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003346 .flags = RPC_TASK_ASYNC,
3347 };
Trond Myklebust95121352005-10-18 14:20:12 -07003348 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003350 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3351 &task_setup_data.rpc_client, &msg);
3352
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003353 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003355 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003356 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003357 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003359 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003361 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3362 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003363 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003364 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003365 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003366 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003367 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003368 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003369 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003370 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003371 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003372 calldata->lr.roc = pnfs_roc(state->inode,
3373 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3374 if (calldata->lr.roc) {
3375 calldata->arg.lr_args = &calldata->lr.arg;
3376 calldata->res.lr_res = &calldata->lr.res;
3377 }
Al Viro643168c2011-06-22 18:20:23 -04003378 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003379
Trond Myklebust1174dd12010-12-21 10:52:24 -05003380 msg.rpc_argp = &calldata->arg;
3381 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003382 task_setup_data.callback_data = calldata;
3383 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003384 if (IS_ERR(task))
3385 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003386 status = 0;
3387 if (wait)
3388 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003389 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003390 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003391out_free_calldata:
3392 kfree(calldata);
3393out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003394 nfs4_put_open_state(state);
3395 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003396 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
Trond Myklebust2b484292010-09-17 10:56:51 -04003399static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003400nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3401 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003404 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3405
3406 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Trond Myklebust565277f2007-10-15 18:17:53 -04003408 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003409 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003410
3411 nfs4_label_release_security(label);
3412
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003413 if (IS_ERR(state))
3414 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003415 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Trond Myklebust1185a552009-12-03 15:54:02 -05003418static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003419{
3420 if (ctx->state == NULL)
3421 return;
3422 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003423 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003424 else
Al Viro643168c2011-06-22 18:20:23 -04003425 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Trond Myklebustb944dba2013-11-04 15:20:20 -05003428#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3429#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003430#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003431
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3433{
Kinglong Mee8c612822015-08-26 21:12:58 +08003434 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003435 struct nfs4_server_caps_arg args = {
3436 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003437 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003438 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 struct nfs4_server_caps_res res = {};
3440 struct rpc_message msg = {
3441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003442 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 .rpc_resp = &res,
3444 };
3445 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003446 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Kinglong Mee8c612822015-08-26 21:12:58 +08003448 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3449 FATTR4_WORD0_FH_EXPIRE_TYPE |
3450 FATTR4_WORD0_LINK_SUPPORT |
3451 FATTR4_WORD0_SYMLINK_SUPPORT |
3452 FATTR4_WORD0_ACLSUPPORT;
3453 if (minorversion)
3454 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3455
Bryan Schumaker7c513052011-03-24 17:12:24 +00003456 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003458 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003459 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003460 case 0:
3461 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3462 res.attr_bitmask[2] = 0;
3463 break;
3464 case 1:
3465 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3466 break;
3467 case 2:
3468 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003471 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3472 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3473 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3474 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003475 NFS_CAP_CTIME|NFS_CAP_MTIME|
3476 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003477 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3478 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 server->caps |= NFS_CAP_ACLS;
3480 if (res.has_links != 0)
3481 server->caps |= NFS_CAP_HARDLINKS;
3482 if (res.has_symlinks != 0)
3483 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003484 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3485 server->caps |= NFS_CAP_FILEID;
3486 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3487 server->caps |= NFS_CAP_MODE;
3488 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3489 server->caps |= NFS_CAP_NLINK;
3490 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3491 server->caps |= NFS_CAP_OWNER;
3492 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3493 server->caps |= NFS_CAP_OWNER_GROUP;
3494 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3495 server->caps |= NFS_CAP_ATIME;
3496 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3497 server->caps |= NFS_CAP_CTIME;
3498 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3499 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003500#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3501 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3502 server->caps |= NFS_CAP_SECURITY_LABEL;
3503#endif
3504 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3505 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003506 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003507
Trond Myklebusta65318b2009-03-11 14:10:28 -04003508 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3509 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3510 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003511 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003512
3513 /* Avoid a regression due to buggy server */
3514 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3515 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003516 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3517 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003520 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 return status;
3524}
3525
Trond Myklebust55a97592006-06-09 09:34:19 -04003526int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
3528 struct nfs4_exception exception = { };
3529 int err;
3530 do {
3531 err = nfs4_handle_exception(server,
3532 _nfs4_server_capabilities(server, fhandle),
3533 &exception);
3534 } while (exception.retry);
3535 return err;
3536}
3537
3538static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3539 struct nfs_fsinfo *info)
3540{
David Quigleyaa9c2662013-05-22 12:50:44 -04003541 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003543 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 };
3545 struct nfs4_lookup_res res = {
3546 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003547 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 .fh = fhandle,
3549 };
3550 struct rpc_message msg = {
3551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3552 .rpc_argp = &args,
3553 .rpc_resp = &res,
3554 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003555
David Quigleyaa9c2662013-05-22 12:50:44 -04003556 bitmask[0] = nfs4_fattr_bitmap[0];
3557 bitmask[1] = nfs4_fattr_bitmap[1];
3558 /*
3559 * Process the label in the upcoming getfattr
3560 */
3561 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3562
Trond Myklebust0e574af2005-10-27 22:12:38 -04003563 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003564 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565}
3566
3567static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3568 struct nfs_fsinfo *info)
3569{
3570 struct nfs4_exception exception = { };
3571 int err;
3572 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003573 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003574 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003575 switch (err) {
3576 case 0:
3577 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003578 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003579 default:
3580 err = nfs4_handle_exception(server, err, &exception);
3581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003583out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return err;
3585}
3586
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003587static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3588 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3589{
Trond Myklebustc2190662013-08-26 19:23:04 -04003590 struct rpc_auth_create_args auth_args = {
3591 .pseudoflavor = flavor,
3592 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003593 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003594
Trond Myklebustc2190662013-08-26 19:23:04 -04003595 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003596 if (IS_ERR(auth))
3597 return -EACCES;
3598 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003599}
3600
Chuck Lever9a744ba2013-03-16 15:56:02 -04003601/*
3602 * Retry pseudoroot lookup with various security flavors. We do this when:
3603 *
3604 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3605 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3606 *
3607 * Returns zero on success, or a negative NFS4ERR value, or a
3608 * negative errno value.
3609 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003610static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003611 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003613 /* Per 3530bis 15.33.5 */
3614 static const rpc_authflavor_t flav_array[] = {
3615 RPC_AUTH_GSS_KRB5P,
3616 RPC_AUTH_GSS_KRB5I,
3617 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003618 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003619 RPC_AUTH_NULL,
3620 };
3621 int status = -EPERM;
3622 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003624 if (server->auth_info.flavor_len > 0) {
3625 /* try each flavor specified by user */
3626 for (i = 0; i < server->auth_info.flavor_len; i++) {
3627 status = nfs4_lookup_root_sec(server, fhandle, info,
3628 server->auth_info.flavors[i]);
3629 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3630 continue;
3631 break;
3632 }
3633 } else {
3634 /* no flavors specified by user, try default list */
3635 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3636 status = nfs4_lookup_root_sec(server, fhandle, info,
3637 flav_array[i]);
3638 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3639 continue;
3640 break;
3641 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003642 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003643
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003644 /*
3645 * -EACCESS could mean that the user doesn't have correct permissions
3646 * to access the mount. It could also mean that we tried to mount
3647 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3648 * existing mount programs don't handle -EACCES very well so it should
3649 * be mapped to -EPERM instead.
3650 */
3651 if (status == -EACCES)
3652 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003653 return status;
3654}
3655
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003656/**
3657 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3658 * @server: initialized nfs_server handle
3659 * @fhandle: we fill in the pseudo-fs root file handle
3660 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003661 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003662 *
3663 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003664 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003665int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003666 struct nfs_fsinfo *info,
3667 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003668{
Andre Przywarac7757072015-04-23 17:17:40 +01003669 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003670
Andre Przywarac7757072015-04-23 17:17:40 +01003671 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003672 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003673
3674 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003675 status = server->nfs_client->cl_mvops->find_root_sec(server,
3676 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 if (status == 0)
3679 status = nfs4_server_capabilities(server, fhandle);
3680 if (status == 0)
3681 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003682
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003683 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
Bryan Schumakerbae36242012-05-10 15:07:31 -04003686static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3687 struct nfs_fsinfo *info)
3688{
3689 int error;
3690 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003691 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003692
3693 error = nfs4_server_capabilities(server, mntfh);
3694 if (error < 0) {
3695 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3696 return error;
3697 }
3698
David Quigley14c43f72013-05-22 12:50:43 -04003699 label = nfs4_label_alloc(server, GFP_KERNEL);
3700 if (IS_ERR(label))
3701 return PTR_ERR(label);
3702
David Quigley1775fd32013-05-22 12:50:42 -04003703 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003704 if (error < 0) {
3705 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003706 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003707 }
3708
3709 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3710 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3711 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3712
David Quigley14c43f72013-05-22 12:50:43 -04003713err_free_label:
3714 nfs4_label_free(label);
3715
Bryan Schumakerbae36242012-05-10 15:07:31 -04003716 return error;
3717}
3718
Manoj Naik6b97fd32006-06-09 09:34:29 -04003719/*
3720 * Get locations and (maybe) other attributes of a referral.
3721 * Note that we'll actually follow the referral later when
3722 * we detect fsid mismatch in inode revalidation
3723 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003724static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3725 const struct qstr *name, struct nfs_fattr *fattr,
3726 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003727{
3728 int status = -ENOMEM;
3729 struct page *page = NULL;
3730 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003731
3732 page = alloc_page(GFP_KERNEL);
3733 if (page == NULL)
3734 goto out;
3735 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3736 if (locations == NULL)
3737 goto out;
3738
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003739 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003740 if (status != 0)
3741 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003742
3743 /*
3744 * If the fsid didn't change, this is a migration event, not a
3745 * referral. Cause us to drop into the exception handler, which
3746 * will kick off migration recovery.
3747 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003748 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003749 dprintk("%s: server did not return a different fsid for"
3750 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003751 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003752 goto out;
3753 }
Andy Adamson533eb462011-06-13 18:25:56 -04003754 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3755 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003756
Andy Adamson533eb462011-06-13 18:25:56 -04003757 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003758 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003759 memset(fhandle, 0, sizeof(struct nfs_fh));
3760out:
3761 if (page)
3762 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003763 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003764 return status;
3765}
3766
David Quigley1775fd32013-05-22 12:50:42 -04003767static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3768 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
3770 struct nfs4_getattr_arg args = {
3771 .fh = fhandle,
3772 .bitmask = server->attr_bitmask,
3773 };
3774 struct nfs4_getattr_res res = {
3775 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003776 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 .server = server,
3778 };
3779 struct rpc_message msg = {
3780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3781 .rpc_argp = &args,
3782 .rpc_resp = &res,
3783 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003784
3785 args.bitmask = nfs4_bitmask(server, label);
3786
Trond Myklebust0e574af2005-10-27 22:12:38 -04003787 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003788 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
David Quigley1775fd32013-05-22 12:50:42 -04003791static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3792 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
3794 struct nfs4_exception exception = { };
3795 int err;
3796 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003797 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3798 trace_nfs4_getattr(server, fhandle, fattr, err);
3799 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 &exception);
3801 } while (exception.retry);
3802 return err;
3803}
3804
3805/*
3806 * The file is not closed if it is opened due to the a request to change
3807 * the size of the file. The open call will not be needed once the
3808 * VFS layer lookup-intents are implemented.
3809 *
3810 * Close is called when the inode is destroyed.
3811 * If we haven't opened the file for O_WRONLY, we
3812 * need to in the size_change case to obtain a stateid.
3813 *
3814 * Got race?
3815 * Because OPEN is always done by name in nfsv4, it is
3816 * possible that we opened a different file by the same
3817 * name. We can recognize this race condition, but we
3818 * can't do anything about it besides returning an error.
3819 *
3820 * This will be fixed with VFS changes (lookup-intent).
3821 */
3822static int
3823nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3824 struct iattr *sattr)
3825{
David Howells2b0143b2015-03-17 22:25:59 +00003826 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003827 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003828 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003829 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 int status;
3831
Peng Tao88ac8152014-09-12 11:04:10 +08003832 if (pnfs_ld_layoutret_on_setattr(inode) &&
3833 sattr->ia_valid & ATTR_SIZE &&
3834 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003835 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003836
Trond Myklebust0e574af2005-10-27 22:12:38 -04003837 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Andy Adamson26699402012-05-30 16:12:24 -04003839 /* Deal with open(O_TRUNC) */
3840 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003841 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003842
3843 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003844 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003845 return 0;
3846
Trond Myklebustd5308382005-11-04 15:33:38 -05003847 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003848 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003849
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003850 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003851 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003852 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003853 }
3854
David Quigley14c43f72013-05-22 12:50:43 -04003855 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3856 if (IS_ERR(label))
3857 return PTR_ERR(label);
3858
NeilBrown29b59f92016-10-13 15:26:47 +11003859 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003860 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003861 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003862 nfs_setsecurity(inode, fattr, label);
3863 }
David Quigley14c43f72013-05-22 12:50:43 -04003864 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return status;
3866}
3867
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003868static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3869 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003870 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003871{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003872 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003873 int status;
3874 struct nfs4_lookup_arg args = {
3875 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003876 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003877 .name = name,
3878 };
3879 struct nfs4_lookup_res res = {
3880 .server = server,
3881 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003882 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003883 .fh = fhandle,
3884 };
3885 struct rpc_message msg = {
3886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3887 .rpc_argp = &args,
3888 .rpc_resp = &res,
3889 };
3890
David Quigleyaa9c2662013-05-22 12:50:44 -04003891 args.bitmask = nfs4_bitmask(server, label);
3892
David Howells2b3de442006-08-22 20:06:09 -04003893 nfs_fattr_init(fattr);
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003896 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 dprintk("NFS reply lookup: %d\n", status);
3898 return status;
3899}
3900
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003901static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003902{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003903 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003904 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003905 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3906 fattr->nlink = 2;
3907}
3908
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003909static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003910 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003911 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003912{
3913 struct nfs4_exception exception = { };
3914 struct rpc_clnt *client = *clnt;
3915 int err;
3916 do {
David Quigley1775fd32013-05-22 12:50:42 -04003917 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003918 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003919 switch (err) {
3920 case -NFS4ERR_BADNAME:
3921 err = -ENOENT;
3922 goto out;
3923 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003924 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003925 if (err == -NFS4ERR_MOVED)
3926 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003927 goto out;
3928 case -NFS4ERR_WRONGSEC:
3929 err = -EPERM;
3930 if (client != *clnt)
3931 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003932 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003933 if (IS_ERR(client))
3934 return PTR_ERR(client);
3935
3936 exception.retry = 1;
3937 break;
3938 default:
3939 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3940 }
3941 } while (exception.retry);
3942
3943out:
3944 if (err == 0)
3945 *clnt = client;
3946 else if (client != *clnt)
3947 rpc_shutdown_client(client);
3948
3949 return err;
3950}
3951
Al Virobeffb8f2016-07-20 16:34:42 -04003952static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003953 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3954 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003956 int status;
3957 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003958
David Quigley1775fd32013-05-22 12:50:42 -04003959 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003960 if (client != NFS_CLIENT(dir)) {
3961 rpc_shutdown_client(client);
3962 nfs_fixup_secinfo_attributes(fattr);
3963 }
3964 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003967struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003968nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003969 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3970{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003971 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003972 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003973
David Quigley1775fd32013-05-22 12:50:42 -04003974 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003975 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003976 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003977 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003978}
3979
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003980static int _nfs4_proc_lookupp(struct inode *inode,
3981 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3982 struct nfs4_label *label)
3983{
3984 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3985 struct nfs_server *server = NFS_SERVER(inode);
3986 int status;
3987 struct nfs4_lookupp_arg args = {
3988 .bitmask = server->attr_bitmask,
3989 .fh = NFS_FH(inode),
3990 };
3991 struct nfs4_lookupp_res res = {
3992 .server = server,
3993 .fattr = fattr,
3994 .label = label,
3995 .fh = fhandle,
3996 };
3997 struct rpc_message msg = {
3998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
3999 .rpc_argp = &args,
4000 .rpc_resp = &res,
4001 };
4002
4003 args.bitmask = nfs4_bitmask(server, label);
4004
4005 nfs_fattr_init(fattr);
4006
4007 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4008 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4009 &res.seq_res, 0);
4010 dprintk("NFS reply lookupp: %d\n", status);
4011 return status;
4012}
4013
4014static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4015 struct nfs_fattr *fattr, struct nfs4_label *label)
4016{
4017 struct nfs4_exception exception = { };
4018 int err;
4019 do {
4020 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4021 trace_nfs4_lookupp(inode, err);
4022 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4023 &exception);
4024 } while (exception.retry);
4025 return err;
4026}
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4029{
Trond Myklebust76b32992007-08-10 17:45:11 -04004030 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct nfs4_accessargs args = {
4032 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004033 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004034 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004036 struct nfs4_accessres res = {
4037 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 struct rpc_message msg = {
4040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4041 .rpc_argp = &args,
4042 .rpc_resp = &res,
4043 .rpc_cred = entry->cred,
4044 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004045 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Trond Myklebustc407d412010-04-16 16:22:48 -04004047 res.fattr = nfs_alloc_fattr();
4048 if (res.fattr == NULL)
4049 return -ENOMEM;
4050
Bryan Schumaker7c513052011-03-24 17:12:24 +00004051 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004053 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004054 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004056 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return status;
4058}
4059
4060static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4061{
4062 struct nfs4_exception exception = { };
4063 int err;
4064 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004065 err = _nfs4_proc_access(inode, entry);
4066 trace_nfs4_access(inode, err);
4067 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 &exception);
4069 } while (exception.retry);
4070 return err;
4071}
4072
4073/*
4074 * TODO: For the time being, we don't try to get any attributes
4075 * along with any of the zero-copy operations READ, READDIR,
4076 * READLINK, WRITE.
4077 *
4078 * In the case of the first three, we want to put the GETATTR
4079 * after the read-type operation -- this is because it is hard
4080 * to predict the length of a GETATTR response in v4, and thus
4081 * align the READ data correctly. This means that the GETATTR
4082 * may end up partially falling into the page cache, and we should
4083 * shift it into the 'tail' of the xdr_buf before processing.
4084 * To do this efficiently, we need to know the total length
4085 * of data received, which doesn't seem to be available outside
4086 * of the RPC layer.
4087 *
4088 * In the case of WRITE, we also want to put the GETATTR after
4089 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004090 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 *
4092 * Both of these changes to the XDR layer would in fact be quite
4093 * minor, but I decided to leave them for a subsequent patch.
4094 */
4095static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4096 unsigned int pgbase, unsigned int pglen)
4097{
4098 struct nfs4_readlink args = {
4099 .fh = NFS_FH(inode),
4100 .pgbase = pgbase,
4101 .pglen = pglen,
4102 .pages = &page,
4103 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004104 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 struct rpc_message msg = {
4106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4107 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004108 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 };
4110
Bryan Schumaker7c513052011-03-24 17:12:24 +00004111 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 -07004112}
4113
4114static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4115 unsigned int pgbase, unsigned int pglen)
4116{
4117 struct nfs4_exception exception = { };
4118 int err;
4119 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004120 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4121 trace_nfs4_readlink(inode, err);
4122 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 &exception);
4124 } while (exception.retry);
4125 return err;
4126}
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004129 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131static int
4132nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004133 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004135 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004136 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004137 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 int status = 0;
4140
NeilBrown532d4de2016-10-13 15:26:47 +11004141 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004142 if (IS_ERR(ctx))
4143 return PTR_ERR(ctx);
4144
David Quigleyaa9c2662013-05-22 12:50:44 -04004145 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4146
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004147 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4148 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004149 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 if (IS_ERR(state)) {
4151 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004155 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004156 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 return status;
4158}
4159
Al Virobeffb8f2016-07-20 16:34:42 -04004160static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Trond Myklebust16e42952005-10-27 22:12:44 -04004162 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004163 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004165 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004167 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004168 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004169 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4172 .rpc_argp = &args,
4173 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004175 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004176 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004177
Bryan Schumaker7c513052011-03-24 17:12:24 +00004178 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004179 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004180 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return status;
4182}
4183
Al Virobeffb8f2016-07-20 16:34:42 -04004184static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
4186 struct nfs4_exception exception = { };
4187 int err;
4188 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004189 err = _nfs4_proc_remove(dir, name);
4190 trace_nfs4_remove(dir, name, err);
4191 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 &exception);
4193 } while (exception.retry);
4194 return err;
4195}
4196
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004197static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004199 struct nfs_server *server = NFS_SERVER(dir);
4200 struct nfs_removeargs *args = msg->rpc_argp;
4201 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004203 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004205 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004206
4207 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004210static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4211{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004212 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004213 &data->args.seq_args,
4214 &data->res.seq_res,
4215 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004218static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004220 struct nfs_unlinkdata *data = task->tk_calldata;
4221 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004222
Trond Myklebust14516c32010-07-31 14:29:06 -04004223 if (!nfs4_sequence_done(task, &res->seq_res))
4224 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004225 if (nfs4_async_handle_error(task, res->server, NULL,
4226 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004227 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004228 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004229 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004230 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
Jeff Laytond3d41522010-09-17 17:31:57 -04004233static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4234{
4235 struct nfs_server *server = NFS_SERVER(dir);
4236 struct nfs_renameargs *arg = msg->rpc_argp;
4237 struct nfs_renameres *res = msg->rpc_resp;
4238
4239 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004240 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004241 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004242}
4243
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004244static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4245{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004246 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004247 &data->args.seq_args,
4248 &data->res.seq_res,
4249 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004250}
4251
4252static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4253 struct inode *new_dir)
4254{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004255 struct nfs_renamedata *data = task->tk_calldata;
4256 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004257
4258 if (!nfs4_sequence_done(task, &res->seq_res))
4259 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004260 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004261 return 0;
4262
Trond Myklebustc733c492017-01-11 12:32:26 -05004263 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004264 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004265 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004266 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004267 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004268 return 1;
4269}
4270
Al Virobeffb8f2016-07-20 16:34:42 -04004271static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004273 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 struct nfs4_link_arg arg = {
4275 .fh = NFS_FH(inode),
4276 .dir_fh = NFS_FH(dir),
4277 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004278 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004280 struct nfs4_link_res res = {
4281 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004282 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004283 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 struct rpc_message msg = {
4285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4286 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004287 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004289 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Trond Myklebust136f2622010-04-16 16:22:49 -04004291 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004292 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004293 goto out;
4294
David Quigley14c43f72013-05-22 12:50:43 -04004295 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4296 if (IS_ERR(res.label)) {
4297 status = PTR_ERR(res.label);
4298 goto out;
4299 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004300 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004301
Bryan Schumaker7c513052011-03-24 17:12:24 +00004302 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004303 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004304 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004305 status = nfs_post_op_update_inode(inode, res.fattr);
4306 if (!status)
4307 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004308 }
David Quigley14c43f72013-05-22 12:50:43 -04004309
4310
4311 nfs4_label_free(res.label);
4312
Trond Myklebust136f2622010-04-16 16:22:49 -04004313out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004314 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return status;
4316}
4317
Al Virobeffb8f2016-07-20 16:34:42 -04004318static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
4320 struct nfs4_exception exception = { };
4321 int err;
4322 do {
4323 err = nfs4_handle_exception(NFS_SERVER(inode),
4324 _nfs4_proc_link(inode, dir, name),
4325 &exception);
4326 } while (exception.retry);
4327 return err;
4328}
4329
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004330struct nfs4_createdata {
4331 struct rpc_message msg;
4332 struct nfs4_create_arg arg;
4333 struct nfs4_create_res res;
4334 struct nfs_fh fh;
4335 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004336 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004337};
4338
4339static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004340 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004341{
4342 struct nfs4_createdata *data;
4343
4344 data = kzalloc(sizeof(*data), GFP_KERNEL);
4345 if (data != NULL) {
4346 struct nfs_server *server = NFS_SERVER(dir);
4347
David Quigley14c43f72013-05-22 12:50:43 -04004348 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4349 if (IS_ERR(data->label))
4350 goto out_free;
4351
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004352 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4353 data->msg.rpc_argp = &data->arg;
4354 data->msg.rpc_resp = &data->res;
4355 data->arg.dir_fh = NFS_FH(dir);
4356 data->arg.server = server;
4357 data->arg.name = name;
4358 data->arg.attrs = sattr;
4359 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004360 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004361 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004362 data->res.server = server;
4363 data->res.fh = &data->fh;
4364 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004365 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004366 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004367 }
4368 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004369out_free:
4370 kfree(data);
4371 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004372}
4373
4374static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4375{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004376 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004377 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004378 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004379 update_changeattr(dir, &data->res.dir_cinfo,
4380 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004381 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004382 }
4383 return status;
4384}
4385
4386static void nfs4_free_createdata(struct nfs4_createdata *data)
4387{
David Quigley14c43f72013-05-22 12:50:43 -04004388 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004389 kfree(data);
4390}
4391
Chuck Lever4f390c12006-08-22 20:06:22 -04004392static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004393 struct page *page, unsigned int len, struct iattr *sattr,
4394 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 struct nfs4_createdata *data;
4397 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Chuck Lever94a6d752006-08-22 20:06:23 -04004399 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004400 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004401
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004402 status = -ENOMEM;
4403 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4404 if (data == NULL)
4405 goto out;
4406
4407 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4408 data->arg.u.symlink.pages = &page;
4409 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004410 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412 status = nfs4_do_create(dir, dentry, data);
4413
4414 nfs4_free_createdata(data);
4415out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return status;
4417}
4418
Chuck Lever4f390c12006-08-22 20:06:22 -04004419static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004420 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
4422 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004423 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004425
4426 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004429 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4430 trace_nfs4_symlink(dir, &dentry->d_name, err);
4431 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 &exception);
4433 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004434
4435 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return err;
4437}
4438
4439static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004440 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004442 struct nfs4_createdata *data;
4443 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4446 if (data == NULL)
4447 goto out;
4448
David Quigley1775fd32013-05-22 12:50:42 -04004449 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 status = nfs4_do_create(dir, dentry, data);
4451
4452 nfs4_free_createdata(data);
4453out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return status;
4455}
4456
4457static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4458 struct iattr *sattr)
4459{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004460 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004462 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004464
David Quigleyaa9c2662013-05-22 12:50:44 -04004465 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4466
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004467 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4468 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004470 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4471 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4472 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 &exception);
4474 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004475 nfs4_label_release_security(label);
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 return err;
4478}
4479
4480static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004481 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482{
David Howells2b0143b2015-03-17 22:25:59 +00004483 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 struct nfs4_readdir_arg args = {
4485 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004486 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 .pgbase = 0,
4488 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004489 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004490 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 };
4492 struct nfs4_readdir_res res;
4493 struct rpc_message msg = {
4494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4495 .rpc_argp = &args,
4496 .rpc_resp = &res,
4497 .rpc_cred = cred,
4498 };
4499 int status;
4500
Al Viro6de14722013-09-16 10:53:17 -04004501 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4502 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004503 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004504 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004506 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 -05004507 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004508 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004509 status += args.pgbase;
4510 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004511
4512 nfs_invalidate_atime(dir);
4513
Harvey Harrison3110ff82008-05-02 13:42:44 -07004514 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return status;
4516}
4517
4518static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004519 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
4521 struct nfs4_exception exception = { };
4522 int err;
4523 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004524 err = _nfs4_proc_readdir(dentry, cred, cookie,
4525 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004526 trace_nfs4_readdir(d_inode(dentry), err);
4527 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 &exception);
4529 } while (exception.retry);
4530 return err;
4531}
4532
4533static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004534 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004536 struct nfs4_createdata *data;
4537 int mode = sattr->ia_mode;
4538 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004540 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4541 if (data == NULL)
4542 goto out;
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004545 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004547 data->arg.ftype = NF4BLK;
4548 data->arg.u.device.specdata1 = MAJOR(rdev);
4549 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 }
4551 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 data->arg.ftype = NF4CHR;
4553 data->arg.u.device.specdata1 = MAJOR(rdev);
4554 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004555 } else if (!S_ISSOCK(mode)) {
4556 status = -EINVAL;
4557 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 }
David Quigley1775fd32013-05-22 12:50:42 -04004559
David Quigleyaa9c2662013-05-22 12:50:44 -04004560 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004561 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004562out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004563 nfs4_free_createdata(data);
4564out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 return status;
4566}
4567
4568static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4569 struct iattr *sattr, dev_t rdev)
4570{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004571 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004573 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004575
David Quigleyaa9c2662013-05-22 12:50:44 -04004576 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4577
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004578 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4579 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004581 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4582 trace_nfs4_mknod(dir, &dentry->d_name, err);
4583 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 &exception);
4585 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004586
4587 nfs4_label_release_security(label);
4588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 return err;
4590}
4591
4592static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4593 struct nfs_fsstat *fsstat)
4594{
4595 struct nfs4_statfs_arg args = {
4596 .fh = fhandle,
4597 .bitmask = server->attr_bitmask,
4598 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004599 struct nfs4_statfs_res res = {
4600 .fsstat = fsstat,
4601 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 struct rpc_message msg = {
4603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4604 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004605 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 };
4607
Trond Myklebust0e574af2005-10-27 22:12:38 -04004608 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004609 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
4611
4612static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4613{
4614 struct nfs4_exception exception = { };
4615 int err;
4616 do {
4617 err = nfs4_handle_exception(server,
4618 _nfs4_proc_statfs(server, fhandle, fsstat),
4619 &exception);
4620 } while (exception.retry);
4621 return err;
4622}
4623
4624static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4625 struct nfs_fsinfo *fsinfo)
4626{
4627 struct nfs4_fsinfo_arg args = {
4628 .fh = fhandle,
4629 .bitmask = server->attr_bitmask,
4630 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004631 struct nfs4_fsinfo_res res = {
4632 .fsinfo = fsinfo,
4633 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 struct rpc_message msg = {
4635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4636 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004637 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 };
4639
Bryan Schumaker7c513052011-03-24 17:12:24 +00004640 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
4643static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4644{
4645 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004646 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 int err;
4648
4649 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004650 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004651 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004652 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004653 nfs4_set_lease_period(server->nfs_client,
4654 fsinfo->lease_time * HZ,
4655 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004656 break;
4657 }
4658 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 } while (exception.retry);
4660 return err;
4661}
4662
4663static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4664{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004665 int error;
4666
Trond Myklebust0e574af2005-10-27 22:12:38 -04004667 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004668 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004669 if (error == 0) {
4670 /* block layout checks this! */
4671 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004672 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004673 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004674
4675 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
4678static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4679 struct nfs_pathconf *pathconf)
4680{
4681 struct nfs4_pathconf_arg args = {
4682 .fh = fhandle,
4683 .bitmask = server->attr_bitmask,
4684 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004685 struct nfs4_pathconf_res res = {
4686 .pathconf = pathconf,
4687 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 struct rpc_message msg = {
4689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4690 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004691 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 };
4693
4694 /* None of the pathconf attributes are mandatory to implement */
4695 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4696 memset(pathconf, 0, sizeof(*pathconf));
4697 return 0;
4698 }
4699
Trond Myklebust0e574af2005-10-27 22:12:38 -04004700 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004701 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
4704static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4705 struct nfs_pathconf *pathconf)
4706{
4707 struct nfs4_exception exception = { };
4708 int err;
4709
4710 do {
4711 err = nfs4_handle_exception(server,
4712 _nfs4_proc_pathconf(server, fhandle, pathconf),
4713 &exception);
4714 } while (exception.retry);
4715 return err;
4716}
4717
Trond Myklebust5521abf2013-03-16 20:54:34 -04004718int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004719 const struct nfs_open_context *ctx,
4720 const struct nfs_lock_context *l_ctx,
4721 fmode_t fmode)
4722{
NeilBrown17393472016-10-13 15:26:47 +11004723 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004724}
4725EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4726
Trond Myklebust5521abf2013-03-16 20:54:34 -04004727static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4728 const struct nfs_open_context *ctx,
4729 const struct nfs_lock_context *l_ctx,
4730 fmode_t fmode)
4731{
4732 nfs4_stateid current_stateid;
4733
Trond Myklebuste1253be2014-03-05 08:44:23 -05004734 /* If the current stateid represents a lost lock, then exit */
4735 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4736 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004737 return nfs4_stateid_match(stateid, &current_stateid);
4738}
4739
4740static bool nfs4_error_stateid_expired(int err)
4741{
4742 switch (err) {
4743 case -NFS4ERR_DELEG_REVOKED:
4744 case -NFS4ERR_ADMIN_REVOKED:
4745 case -NFS4ERR_BAD_STATEID:
4746 case -NFS4ERR_STALE_STATEID:
4747 case -NFS4ERR_OLD_STATEID:
4748 case -NFS4ERR_OPENMODE:
4749 case -NFS4ERR_EXPIRED:
4750 return true;
4751 }
4752 return false;
4753}
4754
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004755static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004757 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004759 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004760 if (task->tk_status < 0) {
4761 struct nfs4_exception exception = {
4762 .inode = hdr->inode,
4763 .state = hdr->args.context->state,
4764 .stateid = &hdr->args.stateid,
4765 };
4766 task->tk_status = nfs4_async_handle_exception(task,
4767 server, task->tk_status, &exception);
4768 if (exception.retry) {
4769 rpc_restart_call_prepare(task);
4770 return -EAGAIN;
4771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004775 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
Trond Myklebust5521abf2013-03-16 20:54:34 -04004779static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004780 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004781{
4782
4783 if (!nfs4_error_stateid_expired(task->tk_status) ||
4784 nfs4_stateid_is_current(&args->stateid,
4785 args->context,
4786 args->lock_context,
4787 FMODE_READ))
4788 return false;
4789 rpc_restart_call_prepare(task);
4790 return true;
4791}
4792
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004793static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004794{
4795
4796 dprintk("--> %s\n", __func__);
4797
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004798 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004799 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004800 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004801 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004802 if (task->tk_status > 0)
4803 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004804 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4805 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004806}
4807
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004808static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4809 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004811 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004812 if (!hdr->pgio_done_cb)
4813 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004814 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004815 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004818static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4819 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004820{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004821 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004822 &hdr->args.seq_args,
4823 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004824 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004825 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004826 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4827 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004828 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004829 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004830 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004831 return -EIO;
4832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004835static int nfs4_write_done_cb(struct rpc_task *task,
4836 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004838 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004839
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004840 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004841 if (task->tk_status < 0) {
4842 struct nfs4_exception exception = {
4843 .inode = hdr->inode,
4844 .state = hdr->args.context->state,
4845 .stateid = &hdr->args.stateid,
4846 };
4847 task->tk_status = nfs4_async_handle_exception(task,
4848 NFS_SERVER(inode), task->tk_status,
4849 &exception);
4850 if (exception.retry) {
4851 rpc_restart_call_prepare(task);
4852 return -EAGAIN;
4853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004855 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004856 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004857 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004858 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
Trond Myklebust5521abf2013-03-16 20:54:34 -04004862static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004863 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004864{
4865
4866 if (!nfs4_error_stateid_expired(task->tk_status) ||
4867 nfs4_stateid_is_current(&args->stateid,
4868 args->context,
4869 args->lock_context,
4870 FMODE_WRITE))
4871 return false;
4872 rpc_restart_call_prepare(task);
4873 return true;
4874}
4875
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004876static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004877{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004878 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004879 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004880 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004881 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004882 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4883 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004884}
4885
Trond Myklebust5a37f852012-04-28 14:55:16 -04004886static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004888{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004889 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004890 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004891 return false;
4892 /* Otherwise, request attributes if and only if we don't hold
4893 * a delegation
4894 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004895 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004896}
Fred Isamana69aef12011-03-03 15:13:47 +00004897
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004898static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4899 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004901 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004902
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004903 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4904 hdr->args.bitmask = NULL;
4905 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004906 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004907 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004908
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004909 if (!hdr->pgio_done_cb)
4910 hdr->pgio_done_cb = nfs4_write_done_cb;
4911 hdr->res.server = server;
4912 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004914 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004915 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916}
4917
Fred Isaman0b7c0152012-04-20 14:47:39 -04004918static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4919{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004920 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004921 &data->args.seq_args,
4922 &data->res.seq_res,
4923 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
Fred Isaman0b7c0152012-04-20 14:47:39 -04004926static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004929
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004930 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004931 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4932 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004933 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004934 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938
Fred Isaman0b7c0152012-04-20 14:47:39 -04004939static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004940{
4941 if (!nfs4_sequence_done(task, &data->res.seq_res))
4942 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004943 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004944}
4945
Fred Isaman0b7c0152012-04-20 14:47:39 -04004946static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004948 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004949
Fred Isaman0b7c0152012-04-20 14:47:39 -04004950 if (data->commit_done_cb == NULL)
4951 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004952 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004953 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004954 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955}
4956
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004957struct nfs4_renewdata {
4958 struct nfs_client *client;
4959 unsigned long timestamp;
4960};
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962/*
4963 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4964 * standalone procedure for queueing an asynchronous RENEW.
4965 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004966static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004967{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004968 struct nfs4_renewdata *data = calldata;
4969 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004970
Elena Reshetova212bf412017-10-20 12:53:38 +03004971 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004972 nfs4_schedule_state_renewal(clp);
4973 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004974 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004975}
4976
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004977static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004979 struct nfs4_renewdata *data = calldata;
4980 struct nfs_client *clp = data->client;
4981 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004983 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004984 switch (task->tk_status) {
4985 case 0:
4986 break;
4987 case -NFS4ERR_LEASE_MOVED:
4988 nfs4_schedule_lease_moved_recovery(clp);
4989 break;
4990 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004991 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004992 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4993 return;
4994 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004995 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004996 return;
4997 }
4998 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005000 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001}
5002
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005003static const struct rpc_call_ops nfs4_renew_ops = {
5004 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005005 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005006};
5007
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005008static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009{
5010 struct rpc_message msg = {
5011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5012 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005013 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005015 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005017 if (renew_flags == 0)
5018 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005019 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005020 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005021 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005022 if (data == NULL) {
5023 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005024 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005025 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005026 data->client = clp;
5027 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005028 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005029 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005032static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033{
5034 struct rpc_message msg = {
5035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5036 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005037 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 };
5039 unsigned long now = jiffies;
5040 int status;
5041
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005042 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 if (status < 0)
5044 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005045 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 return 0;
5047}
5048
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005049static inline int nfs4_server_supports_acls(struct nfs_server *server)
5050{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005051 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005052}
5053
Trond Myklebust21f498c2012-08-24 10:59:25 -04005054/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5055 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005056 * the stack.
5057 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005058#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005059
Neil Hormane9e3d722011-03-04 19:26:03 -05005060static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005061 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005062{
5063 struct page *newpage, **spages;
5064 int rc = 0;
5065 size_t len;
5066 spages = pages;
5067
5068 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005069 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005070 newpage = alloc_page(GFP_KERNEL);
5071
5072 if (newpage == NULL)
5073 goto unwind;
5074 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005075 buf += len;
5076 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005077 *pages++ = newpage;
5078 rc++;
5079 } while (buflen != 0);
5080
5081 return rc;
5082
5083unwind:
5084 for(; rc > 0; rc--)
5085 __free_page(spages[rc-1]);
5086 return -ENOMEM;
5087}
5088
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089struct nfs4_cached_acl {
5090 int cached;
5091 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005092 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005093};
5094
5095static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005096{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005097 struct nfs_inode *nfsi = NFS_I(inode);
5098
5099 spin_lock(&inode->i_lock);
5100 kfree(nfsi->nfs4_acl);
5101 nfsi->nfs4_acl = acl;
5102 spin_unlock(&inode->i_lock);
5103}
5104
5105static void nfs4_zap_acl_attr(struct inode *inode)
5106{
5107 nfs4_set_cached_acl(inode, NULL);
5108}
5109
5110static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5111{
5112 struct nfs_inode *nfsi = NFS_I(inode);
5113 struct nfs4_cached_acl *acl;
5114 int ret = -ENOENT;
5115
5116 spin_lock(&inode->i_lock);
5117 acl = nfsi->nfs4_acl;
5118 if (acl == NULL)
5119 goto out;
5120 if (buf == NULL) /* user is just asking for length */
5121 goto out_len;
5122 if (acl->cached == 0)
5123 goto out;
5124 ret = -ERANGE; /* see getxattr(2) man page */
5125 if (acl->len > buflen)
5126 goto out;
5127 memcpy(buf, acl->data, acl->len);
5128out_len:
5129 ret = acl->len;
5130out:
5131 spin_unlock(&inode->i_lock);
5132 return ret;
5133}
5134
Sachin Prabhu5794d212012-04-17 14:36:40 +01005135static 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 +00005136{
5137 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005138 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005139
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005140 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005141 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005142 if (acl == NULL)
5143 goto out;
5144 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005145 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005146 } else {
5147 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5148 if (acl == NULL)
5149 goto out;
5150 acl->cached = 0;
5151 }
5152 acl->len = acl_len;
5153out:
5154 nfs4_set_cached_acl(inode, acl);
5155}
5156
Andy Adamsonbf118a32011-12-07 11:55:27 -05005157/*
5158 * The getxattr API returns the required buffer length when called with a
5159 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5160 * the required buf. On a NULL buf, we send a page of data to the server
5161 * guessing that the ACL request can be serviced by a page. If so, we cache
5162 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5163 * the cache. If not so, we throw away the page, and cache the required
5164 * length. The next getxattr call will then produce another round trip to
5165 * the server, this time with the input buf of the required size.
5166 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005167static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005168{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005169 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005170 struct nfs_getaclargs args = {
5171 .fh = NFS_FH(inode),
5172 .acl_pages = pages,
5173 .acl_len = buflen,
5174 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005175 struct nfs_getaclres res = {
5176 .acl_len = buflen,
5177 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005178 struct rpc_message msg = {
5179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5180 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005181 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005182 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005183 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005184 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005185
Trond Myklebust21f498c2012-08-24 10:59:25 -04005186 if (npages > ARRAY_SIZE(pages))
5187 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005188
Andy Adamsonbf118a32011-12-07 11:55:27 -05005189 for (i = 0; i < npages; i++) {
5190 pages[i] = alloc_page(GFP_KERNEL);
5191 if (!pages[i])
5192 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005193 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005194
5195 /* for decoding across pages */
5196 res.acl_scratch = alloc_page(GFP_KERNEL);
5197 if (!res.acl_scratch)
5198 goto out_free;
5199
Andy Adamsonbf118a32011-12-07 11:55:27 -05005200 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005201
Peng Taode040be2012-01-10 22:42:47 +08005202 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005203 __func__, buf, buflen, npages, args.acl_len);
5204 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5205 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005206 if (ret)
5207 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005208
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005209 /* Handle the case where the passed-in buffer is too short */
5210 if (res.acl_flags & NFS4_ACL_TRUNC) {
5211 /* Did the user only issue a request for the acl length? */
5212 if (buf == NULL)
5213 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005214 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005215 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005216 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005217 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005218 if (buf) {
5219 if (res.acl_len > buflen) {
5220 ret = -ERANGE;
5221 goto out_free;
5222 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005223 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005224 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005225out_ok:
5226 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005227out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005228 for (i = 0; i < npages; i++)
5229 if (pages[i])
5230 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005231 if (res.acl_scratch)
5232 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005233 return ret;
5234}
5235
Trond Myklebust16b42892006-08-24 12:27:15 -04005236static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5237{
5238 struct nfs4_exception exception = { };
5239 ssize_t ret;
5240 do {
5241 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005242 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005243 if (ret >= 0)
5244 break;
5245 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5246 } while (exception.retry);
5247 return ret;
5248}
5249
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005250static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5251{
5252 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005253 int ret;
5254
5255 if (!nfs4_server_supports_acls(server))
5256 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005257 ret = nfs_revalidate_inode(server, inode);
5258 if (ret < 0)
5259 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005260 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5261 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005262 ret = nfs4_read_cached_acl(inode, buf, buflen);
5263 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005264 /* -ENOENT is returned if there is no ACL or if there is an ACL
5265 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005266 return ret;
5267 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005268}
5269
Trond Myklebust16b42892006-08-24 12:27:15 -04005270static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005271{
5272 struct nfs_server *server = NFS_SERVER(inode);
5273 struct page *pages[NFS4ACL_MAXPAGES];
5274 struct nfs_setaclargs arg = {
5275 .fh = NFS_FH(inode),
5276 .acl_pages = pages,
5277 .acl_len = buflen,
5278 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005279 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005280 struct rpc_message msg = {
5281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5282 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005283 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005284 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005285 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005286 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005287
5288 if (!nfs4_server_supports_acls(server))
5289 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005290 if (npages > ARRAY_SIZE(pages))
5291 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005292 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005293 if (i < 0)
5294 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005295 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005296 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005297
5298 /*
5299 * Free each page after tx, so the only ref left is
5300 * held by the network stack
5301 */
5302 for (; i > 0; i--)
5303 put_page(pages[i-1]);
5304
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005305 /*
5306 * Acl update can result in inode attribute update.
5307 * so mark the attribute cache invalid.
5308 */
5309 spin_lock(&inode->i_lock);
5310 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5311 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005312 nfs_access_zap_cache(inode);
5313 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005314 return ret;
5315}
5316
Trond Myklebust16b42892006-08-24 12:27:15 -04005317static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5318{
5319 struct nfs4_exception exception = { };
5320 int err;
5321 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005322 err = __nfs4_proc_set_acl(inode, buf, buflen);
5323 trace_nfs4_set_acl(inode, err);
5324 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005325 &exception);
5326 } while (exception.retry);
5327 return err;
5328}
5329
David Quigleyaa9c2662013-05-22 12:50:44 -04005330#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5331static int _nfs4_get_security_label(struct inode *inode, void *buf,
5332 size_t buflen)
5333{
5334 struct nfs_server *server = NFS_SERVER(inode);
5335 struct nfs_fattr fattr;
5336 struct nfs4_label label = {0, 0, buflen, buf};
5337
5338 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005339 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005340 .fh = NFS_FH(inode),
5341 .bitmask = bitmask,
5342 };
5343 struct nfs4_getattr_res res = {
5344 .fattr = &fattr,
5345 .label = &label,
5346 .server = server,
5347 };
5348 struct rpc_message msg = {
5349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005350 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005351 .rpc_resp = &res,
5352 };
5353 int ret;
5354
5355 nfs_fattr_init(&fattr);
5356
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005357 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005358 if (ret)
5359 return ret;
5360 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5361 return -ENOENT;
5362 if (buflen < label.len)
5363 return -ERANGE;
5364 return 0;
5365}
5366
5367static int nfs4_get_security_label(struct inode *inode, void *buf,
5368 size_t buflen)
5369{
5370 struct nfs4_exception exception = { };
5371 int err;
5372
5373 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5374 return -EOPNOTSUPP;
5375
5376 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005377 err = _nfs4_get_security_label(inode, buf, buflen);
5378 trace_nfs4_get_security_label(inode, err);
5379 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005380 &exception);
5381 } while (exception.retry);
5382 return err;
5383}
5384
5385static int _nfs4_do_set_security_label(struct inode *inode,
5386 struct nfs4_label *ilabel,
5387 struct nfs_fattr *fattr,
5388 struct nfs4_label *olabel)
5389{
5390
5391 struct iattr sattr = {0};
5392 struct nfs_server *server = NFS_SERVER(inode);
5393 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005394 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005395 .fh = NFS_FH(inode),
5396 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005397 .server = server,
5398 .bitmask = bitmask,
5399 .label = ilabel,
5400 };
5401 struct nfs_setattrres res = {
5402 .fattr = fattr,
5403 .label = olabel,
5404 .server = server,
5405 };
5406 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5408 .rpc_argp = &arg,
5409 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005410 };
5411 int status;
5412
Jeff Layton12207f62013-11-01 10:49:32 -04005413 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005414
Jeff Layton12207f62013-11-01 10:49:32 -04005415 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005416 if (status)
5417 dprintk("%s failed: %d\n", __func__, status);
5418
5419 return status;
5420}
5421
5422static int nfs4_do_set_security_label(struct inode *inode,
5423 struct nfs4_label *ilabel,
5424 struct nfs_fattr *fattr,
5425 struct nfs4_label *olabel)
5426{
5427 struct nfs4_exception exception = { };
5428 int err;
5429
5430 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005431 err = _nfs4_do_set_security_label(inode, ilabel,
5432 fattr, olabel);
5433 trace_nfs4_set_security_label(inode, err);
5434 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005435 &exception);
5436 } while (exception.retry);
5437 return err;
5438}
5439
5440static int
Al Viro59301222016-05-27 10:19:30 -04005441nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005442{
5443 struct nfs4_label ilabel, *olabel = NULL;
5444 struct nfs_fattr fattr;
5445 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005446 int status;
5447
5448 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5449 return -EOPNOTSUPP;
5450
5451 nfs_fattr_init(&fattr);
5452
5453 ilabel.pi = 0;
5454 ilabel.lfs = 0;
5455 ilabel.label = (char *)buf;
5456 ilabel.len = buflen;
5457
5458 cred = rpc_lookup_cred();
5459 if (IS_ERR(cred))
5460 return PTR_ERR(cred);
5461
5462 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5463 if (IS_ERR(olabel)) {
5464 status = -PTR_ERR(olabel);
5465 goto out;
5466 }
5467
5468 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5469 if (status == 0)
5470 nfs_setsecurity(inode, &fattr, olabel);
5471
5472 nfs4_label_free(olabel);
5473out:
5474 put_rpccred(cred);
5475 return status;
5476}
5477#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5478
5479
Chuck Leverf0920752012-05-21 22:45:41 -04005480static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5481 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005482{
5483 __be32 verf[2];
5484
Chuck Lever2c820d92012-05-21 22:45:33 -04005485 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5486 /* An impossible timestamp guarantees this value
5487 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005488 verf[0] = cpu_to_be32(U32_MAX);
5489 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005490 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005491 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005492 u64 ns = ktime_to_ns(nn->boot_time);
5493
5494 verf[0] = cpu_to_be32(ns >> 32);
5495 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005496 }
Chuck Levercd937102012-03-02 17:14:31 -05005497 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5498}
5499
Jeff Laytona3192682015-06-09 19:43:59 -04005500static int
5501nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005502{
Jeff Laytona3192682015-06-09 19:43:59 -04005503 size_t len;
5504 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005505
Trond Myklebustceb3a162015-01-03 15:16:04 -05005506 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005507 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005508
Jeff Laytona3192682015-06-09 19:43:59 -04005509 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005510 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005511 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5512 1 +
5513 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5514 1;
5515 rcu_read_unlock();
5516
5517 if (len > NFS4_OPAQUE_LIMIT + 1)
5518 return -EINVAL;
5519
5520 /*
5521 * Since this string is allocated at mount time, and held until the
5522 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5523 * about a memory-reclaim deadlock.
5524 */
5525 str = kmalloc(len, GFP_KERNEL);
5526 if (!str)
5527 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005528
Chuck Levere984a552012-09-14 17:24:21 -04005529 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005530 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005531 clp->cl_ipaddr,
5532 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5533 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005534 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005535
Jeff Laytona3192682015-06-09 19:43:59 -04005536 clp->cl_owner_id = str;
5537 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005538}
5539
Jeff Layton873e3852015-06-09 19:44:00 -04005540static int
5541nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005542{
Jeff Layton873e3852015-06-09 19:44:00 -04005543 size_t len;
5544 char *str;
5545
5546 len = 10 + 10 + 1 + 10 + 1 +
5547 strlen(nfs4_client_id_uniquifier) + 1 +
5548 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5549
5550 if (len > NFS4_OPAQUE_LIMIT + 1)
5551 return -EINVAL;
5552
5553 /*
5554 * Since this string is allocated at mount time, and held until the
5555 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5556 * about a memory-reclaim deadlock.
5557 */
5558 str = kmalloc(len, GFP_KERNEL);
5559 if (!str)
5560 return -ENOMEM;
5561
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005562 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005563 clp->rpc_ops->version, clp->cl_minorversion,
5564 nfs4_client_id_uniquifier,
5565 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005566 clp->cl_owner_id = str;
5567 return 0;
5568}
5569
5570static int
5571nfs4_init_uniform_client_string(struct nfs_client *clp)
5572{
Jeff Layton873e3852015-06-09 19:44:00 -04005573 size_t len;
5574 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005575
5576 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005577 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005578
5579 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005580 return nfs4_init_uniquifier_client_string(clp);
5581
5582 len = 10 + 10 + 1 + 10 + 1 +
5583 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5584
5585 if (len > NFS4_OPAQUE_LIMIT + 1)
5586 return -EINVAL;
5587
5588 /*
5589 * Since this string is allocated at mount time, and held until the
5590 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5591 * about a memory-reclaim deadlock.
5592 */
5593 str = kmalloc(len, GFP_KERNEL);
5594 if (!str)
5595 return -ENOMEM;
5596
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005597 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005598 clp->rpc_ops->version, clp->cl_minorversion,
5599 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005600 clp->cl_owner_id = str;
5601 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005602}
5603
Chuck Lever706cb8d2014-03-12 12:51:47 -04005604/*
5605 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5606 * services. Advertise one based on the address family of the
5607 * clientaddr.
5608 */
5609static unsigned int
5610nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5611{
5612 if (strchr(clp->cl_ipaddr, ':') != NULL)
5613 return scnprintf(buf, len, "tcp6");
5614 else
5615 return scnprintf(buf, len, "tcp");
5616}
5617
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005618static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5619{
5620 struct nfs4_setclientid *sc = calldata;
5621
5622 if (task->tk_status == 0)
5623 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5624}
5625
5626static const struct rpc_call_ops nfs4_setclientid_ops = {
5627 .rpc_call_done = nfs4_setclientid_done,
5628};
5629
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005630/**
5631 * nfs4_proc_setclientid - Negotiate client ID
5632 * @clp: state data structure
5633 * @program: RPC program for NFSv4 callback service
5634 * @port: IP port number for NFS4 callback service
5635 * @cred: RPC credential to use for this call
5636 * @res: where to place the result
5637 *
5638 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5639 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005640int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5641 unsigned short port, struct rpc_cred *cred,
5642 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
5644 nfs4_verifier sc_verifier;
5645 struct nfs4_setclientid setclientid = {
5646 .sc_verifier = &sc_verifier,
5647 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005648 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 };
5650 struct rpc_message msg = {
5651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5652 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005653 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005654 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005656 struct rpc_task *task;
5657 struct rpc_task_setup task_setup_data = {
5658 .rpc_client = clp->cl_rpcclient,
5659 .rpc_message = &msg,
5660 .callback_ops = &nfs4_setclientid_ops,
5661 .callback_data = &setclientid,
5662 .flags = RPC_TASK_TIMEOUT,
5663 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005664 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Chuck Leverde734832012-07-11 16:30:50 -04005666 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005667 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005668
5669 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5670 status = nfs4_init_uniform_client_string(clp);
5671 else
Jeff Laytona3192682015-06-09 19:43:59 -04005672 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005673
5674 if (status)
5675 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005676
Chuck Leverde734832012-07-11 16:30:50 -04005677 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005678 setclientid.sc_netid_len =
5679 nfs4_init_callback_netid(clp,
5680 setclientid.sc_netid,
5681 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005682 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005683 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 clp->cl_ipaddr, port >> 8, port & 255);
5685
Jeff Layton3a6bb732015-06-09 19:43:57 -04005686 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005687 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005688 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005689 task = rpc_run_task(&task_setup_data);
5690 if (IS_ERR(task)) {
5691 status = PTR_ERR(task);
5692 goto out;
5693 }
5694 status = task->tk_status;
5695 if (setclientid.sc_cred) {
5696 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5697 put_rpccred(setclientid.sc_cred);
5698 }
5699 rpc_put_task(task);
5700out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005701 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005702 dprintk("NFS reply setclientid: %d\n", status);
5703 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005706/**
5707 * nfs4_proc_setclientid_confirm - Confirm client ID
5708 * @clp: state data structure
5709 * @res: result of a previous SETCLIENTID
5710 * @cred: RPC credential to use for this call
5711 *
5712 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5713 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005714int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005715 struct nfs4_setclientid_res *arg,
5716 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 struct rpc_message msg = {
5719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005720 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005721 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 int status;
5724
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005725 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5726 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5727 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005728 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005729 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005730 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 return status;
5732}
5733
Trond Myklebustfe650402006-01-03 09:55:18 +01005734struct nfs4_delegreturndata {
5735 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005736 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005737 struct nfs_fh fh;
5738 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005739 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005740 struct {
5741 struct nfs4_layoutreturn_args arg;
5742 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005743 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005744 u32 roc_barrier;
5745 bool roc;
5746 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005747 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005748 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005749 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005750};
5751
Trond Myklebustfe650402006-01-03 09:55:18 +01005752static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5753{
5754 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005755
Trond Myklebust14516c32010-07-31 14:29:06 -04005756 if (!nfs4_sequence_done(task, &data->res.seq_res))
5757 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005758
Trond Myklebustca8acf82013-08-13 10:36:56 -04005759 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005760
5761 /* Handle Layoutreturn errors */
5762 if (data->args.lr_args && task->tk_status != 0) {
5763 switch(data->res.lr_ret) {
5764 default:
5765 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5766 break;
5767 case 0:
5768 data->args.lr_args = NULL;
5769 data->res.lr_res = NULL;
5770 break;
5771 case -NFS4ERR_ADMIN_REVOKED:
5772 case -NFS4ERR_DELEG_REVOKED:
5773 case -NFS4ERR_EXPIRED:
5774 case -NFS4ERR_BAD_STATEID:
5775 case -NFS4ERR_OLD_STATEID:
5776 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5777 case -NFS4ERR_WRONG_CRED:
5778 data->args.lr_args = NULL;
5779 data->res.lr_res = NULL;
5780 data->res.lr_ret = 0;
5781 rpc_restart_call_prepare(task);
5782 return;
5783 }
5784 }
5785
Ricardo Labiaga79708862009-12-07 09:23:21 -05005786 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005787 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005788 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005789 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005790 case -NFS4ERR_ADMIN_REVOKED:
5791 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005792 case -NFS4ERR_EXPIRED:
5793 nfs4_free_revoked_stateid(data->res.server,
5794 data->args.stateid,
5795 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005796 case -NFS4ERR_BAD_STATEID:
5797 case -NFS4ERR_OLD_STATEID:
5798 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005799 task->tk_status = 0;
5800 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005801 case -NFS4ERR_ACCESS:
5802 if (data->args.bitmask) {
5803 data->args.bitmask = NULL;
5804 data->res.fattr = NULL;
5805 task->tk_status = 0;
5806 rpc_restart_call_prepare(task);
5807 return;
5808 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005809 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005810 if (nfs4_async_handle_error(task, data->res.server,
5811 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005812 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005813 return;
5814 }
5815 }
5816 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005817}
5818
5819static void nfs4_delegreturn_release(void *calldata)
5820{
Peng Tao039b7562014-07-03 13:05:02 +08005821 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005822 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005823
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005824 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005825 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005826 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5827 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005828 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005829 nfs_iput_and_deactive(inode);
5830 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005831 kfree(calldata);
5832}
5833
Andy Adamson938e1012009-04-01 09:22:28 -04005834static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5835{
5836 struct nfs4_delegreturndata *d_data;
5837
5838 d_data = (struct nfs4_delegreturndata *)data;
5839
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005840 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005841 return;
5842
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005843 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005844 &d_data->args.seq_args,
5845 &d_data->res.seq_res,
5846 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005847}
Andy Adamson938e1012009-04-01 09:22:28 -04005848
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005849static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005850 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005851 .rpc_call_done = nfs4_delegreturn_done,
5852 .rpc_release = nfs4_delegreturn_release,
5853};
5854
Trond Myklebuste6f81072008-01-24 18:14:34 -05005855static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005856{
5857 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005858 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005859 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005860 struct rpc_message msg = {
5861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5862 .rpc_cred = cred,
5863 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005864 struct rpc_task_setup task_setup_data = {
5865 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005866 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005867 .callback_ops = &nfs4_delegreturn_ops,
5868 .flags = RPC_TASK_ASYNC,
5869 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005870 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005871
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005872 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005873 if (data == NULL)
5874 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005875 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005876
5877 nfs4_state_protect(server->nfs_client,
5878 NFS_SP4_MACH_CRED_CLEANUP,
5879 &task_setup_data.rpc_client, &msg);
5880
Trond Myklebustfe650402006-01-03 09:55:18 +01005881 data->args.fhandle = &data->fh;
5882 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005883 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005884 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005885 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005886 data->res.fattr = &data->fattr;
5887 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005888 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005889 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005890 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005891 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005892 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005893 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005894 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005895 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005896 if (data->lr.roc) {
5897 data->args.lr_args = &data->lr.arg;
5898 data->res.lr_res = &data->lr.res;
5899 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005900 } else if (data->lr.roc) {
5901 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5902 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005903 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005904
Trond Myklebustc970aa82007-07-14 15:39:59 -04005905 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005906 msg.rpc_argp = &data->args;
5907 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005908 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005909 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005910 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005911 if (!issync)
5912 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005913 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005914 if (status != 0)
5915 goto out;
5916 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005917out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005918 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005919 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920}
5921
Trond Myklebuste6f81072008-01-24 18:14:34 -05005922int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923{
5924 struct nfs_server *server = NFS_SERVER(inode);
5925 struct nfs4_exception exception = { };
5926 int err;
5927 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005928 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005929 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 switch (err) {
5931 case -NFS4ERR_STALE_STATEID:
5932 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 case 0:
5934 return 0;
5935 }
5936 err = nfs4_handle_exception(server, err, &exception);
5937 } while (exception.retry);
5938 return err;
5939}
5940
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5942{
5943 struct inode *inode = state->inode;
5944 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005945 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005946 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005948 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005950 struct nfs_lockt_res res = {
5951 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 };
5953 struct rpc_message msg = {
5954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005955 .rpc_argp = &arg,
5956 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 .rpc_cred = state->owner->so_cred,
5958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 struct nfs4_lock_state *lsp;
5960 int status;
5961
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005962 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005963 status = nfs4_set_lock_state(state, request);
5964 if (status != 0)
5965 goto out;
5966 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005967 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005968 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005969 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005970 switch (status) {
5971 case 0:
5972 request->fl_type = F_UNLCK;
5973 break;
5974 case -NFS4ERR_DENIED:
5975 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005977 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005978 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 return status;
5981}
5982
5983static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5984{
5985 struct nfs4_exception exception = { };
5986 int err;
5987
5988 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005989 err = _nfs4_proc_getlk(state, cmd, request);
5990 trace_nfs4_get_lock(request, state, cmd, err);
5991 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 &exception);
5993 } while (exception.retry);
5994 return err;
5995}
5996
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005997struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005998 struct nfs_locku_args arg;
5999 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006000 struct nfs4_lock_state *lsp;
6001 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006002 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006003 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006004 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006005 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006006};
6007
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006008static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6009 struct nfs_open_context *ctx,
6010 struct nfs4_lock_state *lsp,
6011 struct nfs_seqid *seqid)
6012{
6013 struct nfs4_unlockdata *p;
6014 struct inode *inode = lsp->ls_state->inode;
6015
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006016 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006017 if (p == NULL)
6018 return NULL;
6019 p->arg.fh = NFS_FH(inode);
6020 p->arg.fl = &p->fl;
6021 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006022 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006023 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006024 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006025 /* Ensure we don't close file until we're done freeing locks! */
6026 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006027 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006028 memcpy(&p->fl, fl, sizeof(p->fl));
6029 p->server = NFS_SERVER(inode);
6030 return p;
6031}
6032
Trond Myklebust06f814a2006-01-03 09:55:07 +01006033static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006034{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006035 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006036 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006037 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006038 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006039 put_nfs_open_context(calldata->ctx);
6040 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006041}
6042
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006043static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006044{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006045 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006046
Trond Myklebust14516c32010-07-31 14:29:06 -04006047 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6048 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006049 switch (task->tk_status) {
6050 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006051 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006052 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006053 if (nfs4_update_lock_stateid(calldata->lsp,
6054 &calldata->res.stateid))
6055 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006056 case -NFS4ERR_ADMIN_REVOKED:
6057 case -NFS4ERR_EXPIRED:
6058 nfs4_free_revoked_stateid(calldata->server,
6059 &calldata->arg.stateid,
6060 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006061 case -NFS4ERR_BAD_STATEID:
6062 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006063 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006064 if (!nfs4_stateid_match(&calldata->arg.stateid,
6065 &calldata->lsp->ls_stateid))
6066 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006067 break;
6068 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006069 if (nfs4_async_handle_error(task, calldata->server,
6070 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006071 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006072 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006073 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006074}
6075
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006076static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006077{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006078 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006080 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6081 nfs_async_iocounter_wait(task, calldata->l_ctx))
6082 return;
6083
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006084 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006085 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006086 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006087 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006088 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006089 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006090 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006091 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006092 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006093 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006094 &calldata->res.seq_res,
6095 task) != 0)
6096 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006097 return;
6098out_no_action:
6099 task->tk_action = NULL;
6100out_wait:
6101 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102}
6103
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006104static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006105 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006106 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006107 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006108};
6109
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006110static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6111 struct nfs_open_context *ctx,
6112 struct nfs4_lock_state *lsp,
6113 struct nfs_seqid *seqid)
6114{
6115 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006116 struct rpc_message msg = {
6117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6118 .rpc_cred = ctx->cred,
6119 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006120 struct rpc_task_setup task_setup_data = {
6121 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006122 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006123 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006124 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006125 .flags = RPC_TASK_ASYNC,
6126 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006127
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006128 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6129 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6130
Frank Filz137d6ac2007-07-09 15:32:29 -07006131 /* Ensure this is an unlock - when canceling a lock, the
6132 * canceled lock is passed in, and it won't be an unlock.
6133 */
6134 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006135 if (fl->fl_flags & FL_CLOSE)
6136 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006137
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6139 if (data == NULL) {
6140 nfs_free_seqid(seqid);
6141 return ERR_PTR(-ENOMEM);
6142 }
6143
Chuck Levera9c92d62013-08-09 12:48:18 -04006144 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006145 msg.rpc_argp = &data->arg;
6146 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006147 task_setup_data.callback_data = data;
6148 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006149}
6150
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6152{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006153 struct inode *inode = state->inode;
6154 struct nfs4_state_owner *sp = state->owner;
6155 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006156 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006157 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006158 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006159 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006160 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006161 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Trond Myklebust9b073572006-06-29 16:38:34 -04006163 status = nfs4_set_lock_state(state, request);
6164 /* Unlock _before_ we do the RPC call */
6165 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006166 /* Exclude nfs_delegation_claim_locks() */
6167 mutex_lock(&sp->so_delegreturn_mutex);
6168 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006169 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006170 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006171 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006172 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006173 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006174 }
6175 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006176 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006177 if (status != 0)
6178 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006179 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006180 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006181 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6182 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006183 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6184 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006185 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006186 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006187 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006188 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006189 status = PTR_ERR(task);
6190 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006191 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006192 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006193 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006194out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006195 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006196 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006197 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198}
6199
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006200struct nfs4_lockdata {
6201 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006202 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006203 struct nfs4_lock_state *lsp;
6204 struct nfs_open_context *ctx;
6205 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006206 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006207 int rpc_status;
6208 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006209 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006210};
6211
6212static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006213 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6214 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006215{
6216 struct nfs4_lockdata *p;
6217 struct inode *inode = lsp->ls_state->inode;
6218 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006219 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006220
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006221 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 if (p == NULL)
6223 return NULL;
6224
6225 p->arg.fh = NFS_FH(inode);
6226 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006227 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006228 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006229 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006230 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6231 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006232 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006233 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006234 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006235 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006236 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006237 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006238 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006239 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006240 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241 p->ctx = get_nfs_open_context(ctx);
6242 memcpy(&p->fl, fl, sizeof(p->fl));
6243 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006244out_free_seqid:
6245 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006246out_free:
6247 kfree(p);
6248 return NULL;
6249}
6250
6251static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6252{
6253 struct nfs4_lockdata *data = calldata;
6254 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
Harvey Harrison3110ff82008-05-02 13:42:44 -07006256 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006257 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006258 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006259 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006260 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006261 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006262 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006263 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006264 nfs4_stateid_copy(&data->arg.open_stateid,
6265 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006266 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006267 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006268 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006269 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006270 nfs4_stateid_copy(&data->arg.lock_stateid,
6271 &data->lsp->ls_stateid);
6272 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006273 if (!nfs4_valid_open_stateid(state)) {
6274 data->rpc_status = -EBADF;
6275 task->tk_action = NULL;
6276 goto out_release_open_seqid;
6277 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006278 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006279 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006280 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006281 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006282 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006283 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006284out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006285 nfs_release_seqid(data->arg.open_seqid);
6286out_release_lock_seqid:
6287 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006288out_wait:
6289 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006290 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006291}
6292
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6294{
6295 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006296 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006297
Harvey Harrison3110ff82008-05-02 13:42:44 -07006298 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006299
Trond Myklebust14516c32010-07-31 14:29:06 -04006300 if (!nfs4_sequence_done(task, &data->res.seq_res))
6301 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006302
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006303 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006304 switch (task->tk_status) {
6305 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006306 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006307 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006308 if (data->arg.new_lock) {
6309 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006310 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006311 rpc_restart_call_prepare(task);
6312 break;
6313 }
6314 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006315 if (data->arg.new_lock_owner != 0) {
6316 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6317 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6318 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6319 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6320 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006321 break;
6322 case -NFS4ERR_BAD_STATEID:
6323 case -NFS4ERR_OLD_STATEID:
6324 case -NFS4ERR_STALE_STATEID:
6325 case -NFS4ERR_EXPIRED:
6326 if (data->arg.new_lock_owner != 0) {
6327 if (!nfs4_stateid_match(&data->arg.open_stateid,
6328 &lsp->ls_state->open_stateid))
6329 rpc_restart_call_prepare(task);
6330 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6331 &lsp->ls_stateid))
6332 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006333 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006334 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006335}
6336
6337static void nfs4_lock_release(void *calldata)
6338{
6339 struct nfs4_lockdata *data = calldata;
6340
Harvey Harrison3110ff82008-05-02 13:42:44 -07006341 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006342 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006343 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006344 struct rpc_task *task;
6345 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6346 data->arg.lock_seqid);
6347 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006348 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006349 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006350 } else
6351 nfs_free_seqid(data->arg.lock_seqid);
6352 nfs4_put_lock_state(data->lsp);
6353 put_nfs_open_context(data->ctx);
6354 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006355 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006356}
6357
6358static const struct rpc_call_ops nfs4_lock_ops = {
6359 .rpc_call_prepare = nfs4_lock_prepare,
6360 .rpc_call_done = nfs4_lock_done,
6361 .rpc_release = nfs4_lock_release,
6362};
6363
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006364static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6365{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006366 switch (error) {
6367 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006368 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006369 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006370 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006371 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006372 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006373 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006374 break;
6375 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006376 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006377 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006378 };
6379}
6380
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006381static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006382{
6383 struct nfs4_lockdata *data;
6384 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006385 struct rpc_message msg = {
6386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6387 .rpc_cred = state->owner->so_cred,
6388 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006389 struct rpc_task_setup task_setup_data = {
6390 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006391 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006392 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006393 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006394 .flags = RPC_TASK_ASYNC,
6395 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006396 int ret;
6397
Harvey Harrison3110ff82008-05-02 13:42:44 -07006398 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006399 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006400 fl->fl_u.nfs4_fl.owner,
6401 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006402 if (data == NULL)
6403 return -ENOMEM;
6404 if (IS_SETLKW(cmd))
6405 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006406 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006407 msg.rpc_argp = &data->arg;
6408 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006409 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006410 if (recovery_type > NFS_LOCK_NEW) {
6411 if (recovery_type == NFS_LOCK_RECLAIM)
6412 data->arg.reclaim = NFS_LOCK_RECLAIM;
6413 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006414 } else
6415 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006416 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006417 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006418 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006419 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006420 if (ret == 0) {
6421 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006422 if (ret)
6423 nfs4_handle_setlk_error(data->server, data->lsp,
6424 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006425 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006426 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006427 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006428 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006429 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006430 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431}
6432
6433static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6434{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006435 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006436 struct nfs4_exception exception = {
6437 .inode = state->inode,
6438 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006439 int err;
6440
6441 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006442 /* Cache the lock if possible... */
6443 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6444 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006445 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006446 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006447 break;
6448 nfs4_handle_exception(server, err, &exception);
6449 } while (exception.retry);
6450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451}
6452
6453static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6454{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006455 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006456 struct nfs4_exception exception = {
6457 .inode = state->inode,
6458 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006459 int err;
6460
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006461 err = nfs4_set_lock_state(state, request);
6462 if (err != 0)
6463 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006464 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006465 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6466 return 0;
6467 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006468 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006469 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6470 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006471 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006472 switch (err) {
6473 default:
6474 goto out;
6475 case -NFS4ERR_GRACE:
6476 case -NFS4ERR_DELAY:
6477 nfs4_handle_exception(server, err, &exception);
6478 err = 0;
6479 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006480 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006481out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006482 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483}
6484
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006485#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006486static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6487{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006488 struct nfs4_lock_state *lsp;
6489 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006490
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006491 status = nfs4_set_lock_state(state, request);
6492 if (status != 0)
6493 return status;
6494 lsp = request->fl_u.nfs4_fl.owner;
6495 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6496 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6497 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006498 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006499}
6500#endif
6501
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6503{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006504 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006505 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006506 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006507 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508
Trond Myklebust01c3b862006-06-29 16:38:39 -04006509 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006510 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006511 if (status < 0)
6512 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006513 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006514 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006515 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006516 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006517 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006518 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006519 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006520 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006521 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006522 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006523 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006524 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006525 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006526 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006527out:
6528 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 return status;
6530}
6531
6532static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6533{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006534 struct nfs4_exception exception = {
6535 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006536 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006537 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 int err;
6539
6540 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006541 err = _nfs4_proc_setlk(state, cmd, request);
6542 if (err == -NFS4ERR_DENIED)
6543 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006545 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 } while (exception.retry);
6547 return err;
6548}
6549
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006550#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6551#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6552
6553static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006554nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6555 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006556{
6557 int status = -ERESTARTSYS;
6558 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6559
6560 while(!signalled()) {
6561 status = nfs4_proc_setlk(state, cmd, request);
6562 if ((status != -EAGAIN) || IS_SETLK(cmd))
6563 break;
6564 freezable_schedule_timeout_interruptible(timeout);
6565 timeout *= 2;
6566 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6567 status = -ERESTARTSYS;
6568 }
6569 return status;
6570}
6571
Jeff Laytona1d617d82016-09-17 18:17:39 -04006572#ifdef CONFIG_NFS_V4_1
6573struct nfs4_lock_waiter {
6574 struct task_struct *task;
6575 struct inode *inode;
6576 struct nfs_lowner *owner;
6577 bool notified;
6578};
6579
6580static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006581nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006582{
6583 int ret;
6584 struct cb_notify_lock_args *cbnl = key;
6585 struct nfs4_lock_waiter *waiter = wait->private;
6586 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6587 *wowner = waiter->owner;
6588
6589 /* Only wake if the callback was for the same owner */
6590 if (lowner->clientid != wowner->clientid ||
6591 lowner->id != wowner->id ||
6592 lowner->s_dev != wowner->s_dev)
6593 return 0;
6594
6595 /* Make sure it's for the right inode */
6596 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6597 return 0;
6598
6599 waiter->notified = true;
6600
6601 /* override "private" so we can use default_wake_function */
6602 wait->private = waiter->task;
6603 ret = autoremove_wake_function(wait, mode, flags, key);
6604 wait->private = waiter;
6605 return ret;
6606}
6607
6608static int
6609nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6610{
6611 int status = -ERESTARTSYS;
6612 unsigned long flags;
6613 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6614 struct nfs_server *server = NFS_SERVER(state->inode);
6615 struct nfs_client *clp = server->nfs_client;
6616 wait_queue_head_t *q = &clp->cl_lock_waitq;
6617 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6618 .id = lsp->ls_seqid.owner_id,
6619 .s_dev = server->s_dev };
6620 struct nfs4_lock_waiter waiter = { .task = current,
6621 .inode = state->inode,
6622 .owner = &owner,
6623 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006624 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006625
6626 /* Don't bother with waitqueue if we don't expect a callback */
6627 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6628 return nfs4_retry_setlk_simple(state, cmd, request);
6629
6630 init_wait(&wait);
6631 wait.private = &waiter;
6632 wait.func = nfs4_wake_lock_waiter;
6633 add_wait_queue(q, &wait);
6634
6635 while(!signalled()) {
6636 status = nfs4_proc_setlk(state, cmd, request);
6637 if ((status != -EAGAIN) || IS_SETLK(cmd))
6638 break;
6639
6640 status = -ERESTARTSYS;
6641 spin_lock_irqsave(&q->lock, flags);
6642 if (waiter.notified) {
6643 spin_unlock_irqrestore(&q->lock, flags);
6644 continue;
6645 }
6646 set_current_state(TASK_INTERRUPTIBLE);
6647 spin_unlock_irqrestore(&q->lock, flags);
6648
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006649 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006650 }
6651
6652 finish_wait(q, &wait);
6653 return status;
6654}
6655#else /* !CONFIG_NFS_V4_1 */
6656static inline int
6657nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6658{
6659 return nfs4_retry_setlk_simple(state, cmd, request);
6660}
6661#endif
6662
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663static int
6664nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6665{
6666 struct nfs_open_context *ctx;
6667 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 int status;
6669
6670 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006671 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 state = ctx->state;
6673
Trond Myklebustd9531262009-07-21 19:22:38 -04006674 if (IS_GETLK(cmd)) {
6675 if (state != NULL)
6676 return nfs4_proc_getlk(state, F_GETLK, request);
6677 return 0;
6678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
6680 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6681 return -EINVAL;
6682
Trond Myklebustd9531262009-07-21 19:22:38 -04006683 if (request->fl_type == F_UNLCK) {
6684 if (state != NULL)
6685 return nfs4_proc_unlck(state, cmd, request);
6686 return 0;
6687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
Trond Myklebustd9531262009-07-21 19:22:38 -04006689 if (state == NULL)
6690 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006691
6692 if ((request->fl_flags & FL_POSIX) &&
6693 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6694 return -ENOLCK;
6695
Jeff Layton1ea67db2016-09-17 18:17:37 -04006696 status = nfs4_set_lock_state(state, request);
6697 if (status != 0)
6698 return status;
6699
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006700 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701}
6702
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006703int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006704{
6705 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006706 int err;
6707
6708 err = nfs4_set_lock_state(state, fl);
6709 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006710 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006711 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006712 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006713}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006714
Trond Myklebustcf470c32012-03-07 13:49:12 -05006715struct nfs_release_lockowner_data {
6716 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006717 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006718 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006719 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006720 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006721};
6722
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006723static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6724{
6725 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006726 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006727 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6728 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006729 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006730 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006731}
6732
6733static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6734{
6735 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006736 struct nfs_server *server = data->server;
6737
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006738 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006739
6740 switch (task->tk_status) {
6741 case 0:
6742 renew_lease(server, data->timestamp);
6743 break;
6744 case -NFS4ERR_STALE_CLIENTID:
6745 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006746 nfs4_schedule_lease_recovery(server->nfs_client);
6747 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006748 case -NFS4ERR_LEASE_MOVED:
6749 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006750 if (nfs4_async_handle_error(task, server,
6751 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006752 rpc_restart_call_prepare(task);
6753 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006754}
6755
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006756static void nfs4_release_lockowner_release(void *calldata)
6757{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006758 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006759 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006760 kfree(calldata);
6761}
6762
Trond Myklebust17280172012-03-11 13:11:00 -04006763static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006764 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6765 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006766 .rpc_release = nfs4_release_lockowner_release,
6767};
6768
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006769static void
6770nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006771{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006772 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006773 struct rpc_message msg = {
6774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6775 };
6776
6777 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006778 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006779
Trond Myklebustcf470c32012-03-07 13:49:12 -05006780 data = kmalloc(sizeof(*data), GFP_NOFS);
6781 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006782 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006783 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006784 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006785 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6786 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6787 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006788
Trond Myklebustcf470c32012-03-07 13:49:12 -05006789 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006790 msg.rpc_resp = &data->res;
6791 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006792 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006793}
6794
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006795#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6796
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006797static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006798 struct dentry *unused, struct inode *inode,
6799 const char *key, const void *buf,
6800 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006801{
Al Viro59301222016-05-27 10:19:30 -04006802 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006803}
6804
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006805static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006806 struct dentry *unused, struct inode *inode,
6807 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006808{
Al Virob2968212016-04-10 20:48:24 -04006809 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006810}
6811
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006812static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006813{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006814 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006815}
6816
David Quigleyc9bccef2013-05-22 12:50:45 -04006817#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006818
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006819static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006820 struct dentry *unused, struct inode *inode,
6821 const char *key, const void *buf,
6822 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006823{
6824 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006825 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006826
6827 return -EOPNOTSUPP;
6828}
6829
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006830static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006831 struct dentry *unused, struct inode *inode,
6832 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006833{
6834 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006835 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006836 return -EOPNOTSUPP;
6837}
6838
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006839static ssize_t
6840nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006841{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006842 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006843
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006844 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6845 len = security_inode_listsecurity(inode, list, list_len);
6846 if (list_len && len > list_len)
6847 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006848 }
6849 return len;
6850}
6851
6852static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6853 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006854 .get = nfs4_xattr_get_nfs4_label,
6855 .set = nfs4_xattr_set_nfs4_label,
6856};
David Quigleyc9bccef2013-05-22 12:50:45 -04006857
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006858#else
6859
6860static ssize_t
6861nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6862{
6863 return 0;
6864}
6865
6866#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006867
Andy Adamson533eb462011-06-13 18:25:56 -04006868/*
6869 * nfs_fhget will use either the mounted_on_fileid or the fileid
6870 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006871static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6872{
Andy Adamson533eb462011-06-13 18:25:56 -04006873 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6874 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6875 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006876 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006877 return;
6878
6879 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006880 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006881 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6882 fattr->nlink = 2;
6883}
6884
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006885static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6886 const struct qstr *name,
6887 struct nfs4_fs_locations *fs_locations,
6888 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006889{
6890 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006891 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006892 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006893 };
6894 struct nfs4_fs_locations_arg args = {
6895 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006896 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006897 .page = page,
6898 .bitmask = bitmask,
6899 };
Benny Halevy22958462009-04-01 09:22:02 -04006900 struct nfs4_fs_locations_res res = {
6901 .fs_locations = fs_locations,
6902 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006903 struct rpc_message msg = {
6904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6905 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006906 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006907 };
6908 int status;
6909
Harvey Harrison3110ff82008-05-02 13:42:44 -07006910 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006911
6912 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6913 * is not supported */
6914 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6915 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6916 else
6917 bitmask[0] |= FATTR4_WORD0_FILEID;
6918
Trond Myklebustc228fd32007-01-13 02:28:11 -05006919 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006920 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006921 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006922 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006923 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006924 return status;
6925}
6926
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006927int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6928 const struct qstr *name,
6929 struct nfs4_fs_locations *fs_locations,
6930 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006931{
6932 struct nfs4_exception exception = { };
6933 int err;
6934 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006935 err = _nfs4_proc_fs_locations(client, dir, name,
6936 fs_locations, page);
6937 trace_nfs4_get_fs_locations(dir, name, err);
6938 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006939 &exception);
6940 } while (exception.retry);
6941 return err;
6942}
6943
Chuck Leverb03d7352013-10-17 14:12:50 -04006944/*
6945 * This operation also signals the server that this client is
6946 * performing migration recovery. The server can stop returning
6947 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6948 * appended to this compound to identify the client ID which is
6949 * performing recovery.
6950 */
6951static int _nfs40_proc_get_locations(struct inode *inode,
6952 struct nfs4_fs_locations *locations,
6953 struct page *page, struct rpc_cred *cred)
6954{
6955 struct nfs_server *server = NFS_SERVER(inode);
6956 struct rpc_clnt *clnt = server->client;
6957 u32 bitmask[2] = {
6958 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6959 };
6960 struct nfs4_fs_locations_arg args = {
6961 .clientid = server->nfs_client->cl_clientid,
6962 .fh = NFS_FH(inode),
6963 .page = page,
6964 .bitmask = bitmask,
6965 .migration = 1, /* skip LOOKUP */
6966 .renew = 1, /* append RENEW */
6967 };
6968 struct nfs4_fs_locations_res res = {
6969 .fs_locations = locations,
6970 .migration = 1,
6971 .renew = 1,
6972 };
6973 struct rpc_message msg = {
6974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6975 .rpc_argp = &args,
6976 .rpc_resp = &res,
6977 .rpc_cred = cred,
6978 };
6979 unsigned long now = jiffies;
6980 int status;
6981
6982 nfs_fattr_init(&locations->fattr);
6983 locations->server = server;
6984 locations->nlocations = 0;
6985
6986 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6987 nfs4_set_sequence_privileged(&args.seq_args);
6988 status = nfs4_call_sync_sequence(clnt, server, &msg,
6989 &args.seq_args, &res.seq_res);
6990 if (status)
6991 return status;
6992
6993 renew_lease(server, now);
6994 return 0;
6995}
6996
6997#ifdef CONFIG_NFS_V4_1
6998
6999/*
7000 * This operation also signals the server that this client is
7001 * performing migration recovery. The server can stop asserting
7002 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7003 * performing this operation is identified in the SEQUENCE
7004 * operation in this compound.
7005 *
7006 * When the client supports GETATTR(fs_locations_info), it can
7007 * be plumbed in here.
7008 */
7009static int _nfs41_proc_get_locations(struct inode *inode,
7010 struct nfs4_fs_locations *locations,
7011 struct page *page, struct rpc_cred *cred)
7012{
7013 struct nfs_server *server = NFS_SERVER(inode);
7014 struct rpc_clnt *clnt = server->client;
7015 u32 bitmask[2] = {
7016 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7017 };
7018 struct nfs4_fs_locations_arg args = {
7019 .fh = NFS_FH(inode),
7020 .page = page,
7021 .bitmask = bitmask,
7022 .migration = 1, /* skip LOOKUP */
7023 };
7024 struct nfs4_fs_locations_res res = {
7025 .fs_locations = locations,
7026 .migration = 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 int status;
7035
7036 nfs_fattr_init(&locations->fattr);
7037 locations->server = server;
7038 locations->nlocations = 0;
7039
7040 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7041 nfs4_set_sequence_privileged(&args.seq_args);
7042 status = nfs4_call_sync_sequence(clnt, server, &msg,
7043 &args.seq_args, &res.seq_res);
7044 if (status == NFS4_OK &&
7045 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7046 status = -NFS4ERR_LEASE_MOVED;
7047 return status;
7048}
7049
7050#endif /* CONFIG_NFS_V4_1 */
7051
7052/**
7053 * nfs4_proc_get_locations - discover locations for a migrated FSID
7054 * @inode: inode on FSID that is migrating
7055 * @locations: result of query
7056 * @page: buffer
7057 * @cred: credential to use for this operation
7058 *
7059 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7060 * operation failed, or a negative errno if a local error occurred.
7061 *
7062 * On success, "locations" is filled in, but if the server has
7063 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7064 * asserted.
7065 *
7066 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7067 * from this client that require migration recovery.
7068 */
7069int nfs4_proc_get_locations(struct inode *inode,
7070 struct nfs4_fs_locations *locations,
7071 struct page *page, struct rpc_cred *cred)
7072{
7073 struct nfs_server *server = NFS_SERVER(inode);
7074 struct nfs_client *clp = server->nfs_client;
7075 const struct nfs4_mig_recovery_ops *ops =
7076 clp->cl_mvops->mig_recovery_ops;
7077 struct nfs4_exception exception = { };
7078 int status;
7079
7080 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7081 (unsigned long long)server->fsid.major,
7082 (unsigned long long)server->fsid.minor,
7083 clp->cl_hostname);
7084 nfs_display_fhandle(NFS_FH(inode), __func__);
7085
7086 do {
7087 status = ops->get_locations(inode, locations, page, cred);
7088 if (status != -NFS4ERR_DELAY)
7089 break;
7090 nfs4_handle_exception(server, status, &exception);
7091 } while (exception.retry);
7092 return status;
7093}
7094
Chuck Lever44c99932013-10-17 14:13:30 -04007095/*
7096 * This operation also signals the server that this client is
7097 * performing "lease moved" recovery. The server can stop
7098 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7099 * is appended to this compound to identify the client ID which is
7100 * performing recovery.
7101 */
7102static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7103{
7104 struct nfs_server *server = NFS_SERVER(inode);
7105 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7106 struct rpc_clnt *clnt = server->client;
7107 struct nfs4_fsid_present_arg args = {
7108 .fh = NFS_FH(inode),
7109 .clientid = clp->cl_clientid,
7110 .renew = 1, /* append RENEW */
7111 };
7112 struct nfs4_fsid_present_res res = {
7113 .renew = 1,
7114 };
7115 struct rpc_message msg = {
7116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7117 .rpc_argp = &args,
7118 .rpc_resp = &res,
7119 .rpc_cred = cred,
7120 };
7121 unsigned long now = jiffies;
7122 int status;
7123
7124 res.fh = nfs_alloc_fhandle();
7125 if (res.fh == NULL)
7126 return -ENOMEM;
7127
7128 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7129 nfs4_set_sequence_privileged(&args.seq_args);
7130 status = nfs4_call_sync_sequence(clnt, server, &msg,
7131 &args.seq_args, &res.seq_res);
7132 nfs_free_fhandle(res.fh);
7133 if (status)
7134 return status;
7135
7136 do_renew_lease(clp, now);
7137 return 0;
7138}
7139
7140#ifdef CONFIG_NFS_V4_1
7141
7142/*
7143 * This operation also signals the server that this client is
7144 * performing "lease moved" recovery. The server can stop asserting
7145 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7146 * this operation is identified in the SEQUENCE operation in this
7147 * compound.
7148 */
7149static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7150{
7151 struct nfs_server *server = NFS_SERVER(inode);
7152 struct rpc_clnt *clnt = server->client;
7153 struct nfs4_fsid_present_arg args = {
7154 .fh = NFS_FH(inode),
7155 };
7156 struct nfs4_fsid_present_res res = {
7157 };
7158 struct rpc_message msg = {
7159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7160 .rpc_argp = &args,
7161 .rpc_resp = &res,
7162 .rpc_cred = cred,
7163 };
7164 int status;
7165
7166 res.fh = nfs_alloc_fhandle();
7167 if (res.fh == NULL)
7168 return -ENOMEM;
7169
7170 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7171 nfs4_set_sequence_privileged(&args.seq_args);
7172 status = nfs4_call_sync_sequence(clnt, server, &msg,
7173 &args.seq_args, &res.seq_res);
7174 nfs_free_fhandle(res.fh);
7175 if (status == NFS4_OK &&
7176 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7177 status = -NFS4ERR_LEASE_MOVED;
7178 return status;
7179}
7180
7181#endif /* CONFIG_NFS_V4_1 */
7182
7183/**
7184 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7185 * @inode: inode on FSID to check
7186 * @cred: credential to use for this operation
7187 *
7188 * Server indicates whether the FSID is present, moved, or not
7189 * recognized. This operation is necessary to clear a LEASE_MOVED
7190 * condition for this client ID.
7191 *
7192 * Returns NFS4_OK if the FSID is present on this server,
7193 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7194 * NFS4ERR code if some error occurred on the server, or a
7195 * negative errno if a local failure occurred.
7196 */
7197int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7198{
7199 struct nfs_server *server = NFS_SERVER(inode);
7200 struct nfs_client *clp = server->nfs_client;
7201 const struct nfs4_mig_recovery_ops *ops =
7202 clp->cl_mvops->mig_recovery_ops;
7203 struct nfs4_exception exception = { };
7204 int status;
7205
7206 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7207 (unsigned long long)server->fsid.major,
7208 (unsigned long long)server->fsid.minor,
7209 clp->cl_hostname);
7210 nfs_display_fhandle(NFS_FH(inode), __func__);
7211
7212 do {
7213 status = ops->fsid_present(inode, cred);
7214 if (status != -NFS4ERR_DELAY)
7215 break;
7216 nfs4_handle_exception(server, status, &exception);
7217 } while (exception.retry);
7218 return status;
7219}
7220
Andy Adamson5ec16a82013-08-08 10:57:55 -04007221/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007222 * If 'use_integrity' is true and the state managment nfs_client
7223 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7224 * and the machine credential as per RFC3530bis and RFC5661 Security
7225 * Considerations sections. Otherwise, just use the user cred with the
7226 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007227 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007228static 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 +00007229{
7230 int status;
7231 struct nfs4_secinfo_arg args = {
7232 .dir_fh = NFS_FH(dir),
7233 .name = name,
7234 };
7235 struct nfs4_secinfo_res res = {
7236 .flavors = flavors,
7237 };
7238 struct rpc_message msg = {
7239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7240 .rpc_argp = &args,
7241 .rpc_resp = &res,
7242 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007243 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007244 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007245
7246 if (use_integrity) {
7247 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007248 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7249 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007250 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007251
7252 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007253
7254 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7255 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7256
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007257 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7258 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007259 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007260
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007261 if (cred)
7262 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007263
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007264 return status;
7265}
7266
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007267int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7268 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007269{
7270 struct nfs4_exception exception = { };
7271 int err;
7272 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007273 err = -NFS4ERR_WRONGSEC;
7274
7275 /* try to use integrity protection with machine cred */
7276 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7277 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7278
7279 /*
7280 * if unable to use integrity protection, or SECINFO with
7281 * integrity protection returns NFS4ERR_WRONGSEC (which is
7282 * disallowed by spec, but exists in deployed servers) use
7283 * the current filesystem's rpc_client and the user cred.
7284 */
7285 if (err == -NFS4ERR_WRONGSEC)
7286 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7287
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007288 trace_nfs4_secinfo(dir, name, err);
7289 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007290 &exception);
7291 } while (exception.retry);
7292 return err;
7293}
7294
Andy Adamson557134a2009-04-01 09:21:53 -04007295#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007296/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007297 * Check the exchange flags returned by the server for invalid flags, having
7298 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7299 * DS flags set.
7300 */
7301static int nfs4_check_cl_exchange_flags(u32 flags)
7302{
7303 if (flags & ~EXCHGID4_FLAG_MASK_R)
7304 goto out_inval;
7305 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7306 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7307 goto out_inval;
7308 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7309 goto out_inval;
7310 return NFS_OK;
7311out_inval:
7312 return -NFS4ERR_INVAL;
7313}
7314
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007315static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007316nfs41_same_server_scope(struct nfs41_server_scope *a,
7317 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007318{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007319 if (a->server_scope_sz != b->server_scope_sz)
7320 return false;
7321 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007322}
7323
Andy Adamson02a95de2016-02-05 16:08:37 -05007324static void
7325nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7326{
7327}
7328
7329static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7330 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7331};
7332
Andy Adamson357f54d2010-12-14 10:11:57 -05007333/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007334 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007335 *
7336 * The 4.1 client currently uses the same TCP connection for the
7337 * fore and backchannel.
7338 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007339static
7340int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7341 struct rpc_xprt *xprt,
7342 struct nfs_client *clp,
7343 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007344{
7345 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007346 struct nfs41_bind_conn_to_session_args args = {
7347 .client = clp,
7348 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7349 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007350 struct nfs41_bind_conn_to_session_res res;
7351 struct rpc_message msg = {
7352 .rpc_proc =
7353 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007354 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007355 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007356 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007357 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007358 struct rpc_task_setup task_setup_data = {
7359 .rpc_client = clnt,
7360 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007361 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007362 .rpc_message = &msg,
7363 .flags = RPC_TASK_TIMEOUT,
7364 };
7365 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007366
Trond Myklebust71a097c2015-02-18 09:27:18 -08007367 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7368 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7369 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007370
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007371 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7372 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7373 args.dir = NFS4_CDFC4_FORE;
7374
7375 task = rpc_run_task(&task_setup_data);
7376 if (!IS_ERR(task)) {
7377 status = task->tk_status;
7378 rpc_put_task(task);
7379 } else
7380 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007381 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007382 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007383 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007384 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7385 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007386 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007387 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007388 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007389 dprintk("NFS: %s: Unexpected direction from server\n",
7390 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007391 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007392 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007393 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007394 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7395 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007396 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007397 }
7398 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007399
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007400 return status;
7401}
7402
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007403struct rpc_bind_conn_calldata {
7404 struct nfs_client *clp;
7405 struct rpc_cred *cred;
7406};
7407
7408static int
7409nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7410 struct rpc_xprt *xprt,
7411 void *calldata)
7412{
7413 struct rpc_bind_conn_calldata *p = calldata;
7414
7415 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7416}
7417
7418int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7419{
7420 struct rpc_bind_conn_calldata data = {
7421 .clp = clp,
7422 .cred = cred,
7423 };
7424 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7425 nfs4_proc_bind_conn_to_session_callback, &data);
7426}
7427
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007428/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007429 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7430 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007431 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007432static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7433 .how = SP4_MACH_CRED,
7434 .enforce.u.words = {
7435 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7436 1 << (OP_EXCHANGE_ID - 32) |
7437 1 << (OP_CREATE_SESSION - 32) |
7438 1 << (OP_DESTROY_SESSION - 32) |
7439 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007440 },
7441 .allow.u.words = {
7442 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007443 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007444 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007445 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007446 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007447 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007448 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007449 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007450 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007451 1 << (OP_FREE_STATEID - 32) |
7452 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007453 }
7454};
7455
7456/*
7457 * Select the state protection mode for client `clp' given the server results
7458 * from exchange_id in `sp'.
7459 *
7460 * Returns 0 on success, negative errno otherwise.
7461 */
7462static int nfs4_sp4_select_mode(struct nfs_client *clp,
7463 struct nfs41_state_protection *sp)
7464{
7465 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7466 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7467 1 << (OP_EXCHANGE_ID - 32) |
7468 1 << (OP_CREATE_SESSION - 32) |
7469 1 << (OP_DESTROY_SESSION - 32) |
7470 1 << (OP_DESTROY_CLIENTID - 32)
7471 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007472 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007473 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007474 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007475
7476 if (sp->how == SP4_MACH_CRED) {
7477 /* Print state protect result */
7478 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7479 for (i = 0; i <= LAST_NFS4_OP; i++) {
7480 if (test_bit(i, sp->enforce.u.longs))
7481 dfprintk(MOUNT, " enforce op %d\n", i);
7482 if (test_bit(i, sp->allow.u.longs))
7483 dfprintk(MOUNT, " allow op %d\n", i);
7484 }
7485
7486 /* make sure nothing is on enforce list that isn't supported */
7487 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7488 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7489 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007490 ret = -EINVAL;
7491 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007492 }
7493 }
7494
7495 /*
7496 * Minimal mode - state operations are allowed to use machine
7497 * credential. Note this already happens by default, so the
7498 * client doesn't have to do anything more than the negotiation.
7499 *
7500 * NOTE: we don't care if EXCHANGE_ID is in the list -
7501 * we're already using the machine cred for exchange_id
7502 * and will never use a different cred.
7503 */
7504 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7505 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7506 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7507 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7508 dfprintk(MOUNT, "sp4_mach_cred:\n");
7509 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007510 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007511 } else {
7512 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007513 ret = -EINVAL;
7514 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007515 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007516
7517 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007518 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7519 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007520 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7521 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007522 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007523 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007524
Andrew Elble99ade3c2015-12-02 09:39:51 -05007525 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7526 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007527 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007528 }
7529
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007530 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7531 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7532 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007533 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007534 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007535
7536 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7537 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7538 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007539 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007540 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007541
7542 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7543 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007544 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007545 }
7546
7547 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7548 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007549 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007550 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007551 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007552out:
7553 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007554 return 0;
7555}
7556
Andy Adamson8d89bd72016-09-09 09:22:18 -04007557struct nfs41_exchange_id_data {
7558 struct nfs41_exchange_id_res res;
7559 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007560};
7561
Andy Adamson8d89bd72016-09-09 09:22:18 -04007562static void nfs4_exchange_id_release(void *data)
7563{
7564 struct nfs41_exchange_id_data *cdata =
7565 (struct nfs41_exchange_id_data *)data;
7566
Olga Kornievskaia63513232017-03-13 10:36:19 -04007567 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007568 kfree(cdata->res.impl_id);
7569 kfree(cdata->res.server_scope);
7570 kfree(cdata->res.server_owner);
7571 kfree(cdata);
7572}
7573
7574static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007575 .rpc_release = nfs4_exchange_id_release,
7576};
7577
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007578/*
7579 * _nfs4_proc_exchange_id()
7580 *
7581 * Wrapper for EXCHANGE_ID operation.
7582 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007583static struct rpc_task *
7584nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007585 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007586{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007587 struct rpc_message msg = {
7588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007589 .rpc_cred = cred,
7590 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007591 struct rpc_task_setup task_setup_data = {
7592 .rpc_client = clp->cl_rpcclient,
7593 .callback_ops = &nfs4_exchange_id_call_ops,
7594 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007595 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007596 };
7597 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007598 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007599
Elena Reshetova212bf412017-10-20 12:53:38 +03007600 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007601 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007602
Trond Myklebust9c760d12017-07-31 18:38:50 -04007603 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007604 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007605 if (!calldata)
7606 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007607
Trond Myklebustfd405592017-08-01 16:02:47 -04007608 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007609
7610 status = nfs4_init_uniform_client_string(clp);
7611 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007612 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007613
Andy Adamson8d89bd72016-09-09 09:22:18 -04007614 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7615 GFP_NOFS);
7616 status = -ENOMEM;
7617 if (unlikely(calldata->res.server_owner == NULL))
7618 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007619
Andy Adamson8d89bd72016-09-09 09:22:18 -04007620 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007621 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007622 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007623 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007624
Andy Adamson8d89bd72016-09-09 09:22:18 -04007625 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7626 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007627 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007628
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007629 switch (sp4_how) {
7630 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007631 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007632 break;
7633
7634 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007635 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007636 break;
7637
7638 default:
7639 /* unsupported! */
7640 WARN_ON_ONCE(1);
7641 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007642 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007643 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007644 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007645 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007646 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007647 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7648 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007649 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007650 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007651 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7652 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007653#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007654 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007655#endif
7656 msg.rpc_argp = &calldata->args;
7657 msg.rpc_resp = &calldata->res;
7658 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007659
Trond Myklebust9c760d12017-07-31 18:38:50 -04007660 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007661
7662out_impl_id:
7663 kfree(calldata->res.impl_id);
7664out_server_scope:
7665 kfree(calldata->res.server_scope);
7666out_server_owner:
7667 kfree(calldata->res.server_owner);
7668out_calldata:
7669 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007670out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007671 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007672 return ERR_PTR(status);
7673}
7674
7675/*
7676 * _nfs4_proc_exchange_id()
7677 *
7678 * Wrapper for EXCHANGE_ID operation.
7679 */
7680static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7681 u32 sp4_how)
7682{
7683 struct rpc_task *task;
7684 struct nfs41_exchange_id_args *argp;
7685 struct nfs41_exchange_id_res *resp;
7686 int status;
7687
7688 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7689 if (IS_ERR(task))
7690 return PTR_ERR(task);
7691
7692 argp = task->tk_msg.rpc_argp;
7693 resp = task->tk_msg.rpc_resp;
7694 status = task->tk_status;
7695 if (status != 0)
7696 goto out;
7697
7698 status = nfs4_check_cl_exchange_flags(resp->flags);
7699 if (status != 0)
7700 goto out;
7701
7702 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7703 if (status != 0)
7704 goto out;
7705
7706 clp->cl_clientid = resp->clientid;
7707 clp->cl_exchange_flags = resp->flags;
7708 clp->cl_seqid = resp->seqid;
7709 /* Client ID is not confirmed */
7710 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7711 clear_bit(NFS4_SESSION_ESTABLISHED,
7712 &clp->cl_session->session_state);
7713
7714 if (clp->cl_serverscope != NULL &&
7715 !nfs41_same_server_scope(clp->cl_serverscope,
7716 resp->server_scope)) {
7717 dprintk("%s: server_scope mismatch detected\n",
7718 __func__);
7719 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7720 }
7721
7722 swap(clp->cl_serverowner, resp->server_owner);
7723 swap(clp->cl_serverscope, resp->server_scope);
7724 swap(clp->cl_implid, resp->impl_id);
7725
7726 /* Save the EXCHANGE_ID verifier session trunk tests */
7727 memcpy(clp->cl_confirm.data, argp->verifier.data,
7728 sizeof(clp->cl_confirm.data));
7729out:
7730 trace_nfs4_exchange_id(clp, status);
7731 rpc_put_task(task);
7732 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007733}
7734
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007735/*
7736 * nfs4_proc_exchange_id()
7737 *
7738 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7739 *
7740 * Since the clientid has expired, all compounds using sessions
7741 * associated with the stale clientid will be returning
7742 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7743 * be in some phase of session reset.
7744 *
7745 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7746 */
7747int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7748{
7749 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7750 int status;
7751
7752 /* try SP4_MACH_CRED if krb5i/p */
7753 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7754 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007755 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007756 if (!status)
7757 return 0;
7758 }
7759
7760 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007761 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007762}
7763
Andy Adamson04fa2c62016-09-09 09:22:29 -04007764/**
7765 * nfs4_test_session_trunk
7766 *
7767 * This is an add_xprt_test() test function called from
7768 * rpc_clnt_setup_test_and_add_xprt.
7769 *
7770 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7771 * and is dereferrenced in nfs4_exchange_id_release
7772 *
7773 * Upon success, add the new transport to the rpc_clnt
7774 *
7775 * @clnt: struct rpc_clnt to get new transport
7776 * @xprt: the rpc_xprt to test
7777 * @data: call data for _nfs4_proc_exchange_id.
7778 */
7779int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7780 void *data)
7781{
7782 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007783 struct rpc_task *task;
7784 int status;
7785
Andy Adamson04fa2c62016-09-09 09:22:29 -04007786 u32 sp4_how;
7787
7788 dprintk("--> %s try %s\n", __func__,
7789 xprt->address_strings[RPC_DISPLAY_ADDR]);
7790
7791 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7792
7793 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007794 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7795 if (IS_ERR(task))
7796 return PTR_ERR(task);
7797
7798 status = task->tk_status;
7799 if (status == 0)
7800 status = nfs4_detect_session_trunking(adata->clp,
7801 task->tk_msg.rpc_resp, xprt);
7802
7803 rpc_put_task(task);
7804 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007805}
7806EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7807
Trond Myklebust66245532012-05-25 17:18:09 -04007808static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7809 struct rpc_cred *cred)
7810{
7811 struct rpc_message msg = {
7812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7813 .rpc_argp = clp,
7814 .rpc_cred = cred,
7815 };
7816 int status;
7817
7818 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007819 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007820 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007821 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007822 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7823 return status;
7824}
7825
7826static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7827 struct rpc_cred *cred)
7828{
7829 unsigned int loop;
7830 int ret;
7831
7832 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7833 ret = _nfs4_proc_destroy_clientid(clp, cred);
7834 switch (ret) {
7835 case -NFS4ERR_DELAY:
7836 case -NFS4ERR_CLIENTID_BUSY:
7837 ssleep(1);
7838 break;
7839 default:
7840 return ret;
7841 }
7842 }
7843 return 0;
7844}
7845
7846int nfs4_destroy_clientid(struct nfs_client *clp)
7847{
7848 struct rpc_cred *cred;
7849 int ret = 0;
7850
7851 if (clp->cl_mvops->minor_version < 1)
7852 goto out;
7853 if (clp->cl_exchange_flags == 0)
7854 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007855 if (clp->cl_preserve_clid)
7856 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007857 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007858 ret = nfs4_proc_destroy_clientid(clp, cred);
7859 if (cred)
7860 put_rpccred(cred);
7861 switch (ret) {
7862 case 0:
7863 case -NFS4ERR_STALE_CLIENTID:
7864 clp->cl_exchange_flags = 0;
7865 }
7866out:
7867 return ret;
7868}
7869
Andy Adamson2050f0c2009-04-01 09:22:30 -04007870struct nfs4_get_lease_time_data {
7871 struct nfs4_get_lease_time_args *args;
7872 struct nfs4_get_lease_time_res *res;
7873 struct nfs_client *clp;
7874};
7875
7876static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7877 void *calldata)
7878{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007879 struct nfs4_get_lease_time_data *data =
7880 (struct nfs4_get_lease_time_data *)calldata;
7881
7882 dprintk("--> %s\n", __func__);
7883 /* just setup sequence, do not trigger session recovery
7884 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007885 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007886 &data->args->la_seq_args,
7887 &data->res->lr_seq_res,
7888 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007889 dprintk("<-- %s\n", __func__);
7890}
7891
7892/*
7893 * Called from nfs4_state_manager thread for session setup, so don't recover
7894 * from sequence operation or clientid errors.
7895 */
7896static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7897{
7898 struct nfs4_get_lease_time_data *data =
7899 (struct nfs4_get_lease_time_data *)calldata;
7900
7901 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007902 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7903 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007904 switch (task->tk_status) {
7905 case -NFS4ERR_DELAY:
7906 case -NFS4ERR_GRACE:
7907 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7908 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7909 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007910 /* fall through */
7911 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007912 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007913 return;
7914 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007915 dprintk("<-- %s\n", __func__);
7916}
7917
Trond Myklebust17280172012-03-11 13:11:00 -04007918static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007919 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7920 .rpc_call_done = nfs4_get_lease_time_done,
7921};
7922
7923int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7924{
7925 struct rpc_task *task;
7926 struct nfs4_get_lease_time_args args;
7927 struct nfs4_get_lease_time_res res = {
7928 .lr_fsinfo = fsinfo,
7929 };
7930 struct nfs4_get_lease_time_data data = {
7931 .args = &args,
7932 .res = &res,
7933 .clp = clp,
7934 };
7935 struct rpc_message msg = {
7936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7937 .rpc_argp = &args,
7938 .rpc_resp = &res,
7939 };
7940 struct rpc_task_setup task_setup = {
7941 .rpc_client = clp->cl_rpcclient,
7942 .rpc_message = &msg,
7943 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007944 .callback_data = &data,
7945 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007946 };
7947 int status;
7948
Chuck Levera9c92d62013-08-09 12:48:18 -04007949 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007950 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007951 task = rpc_run_task(&task_setup);
7952
7953 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007954 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007955
Anna Schumakerf6148712017-04-07 14:15:23 -04007956 status = task->tk_status;
7957 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007958 return status;
7959}
7960
Andy Adamsonfc931582009-04-01 09:22:31 -04007961/*
7962 * Initialize the values to be used by the client in CREATE_SESSION
7963 * If nfs4_init_session set the fore channel request and response sizes,
7964 * use them.
7965 *
7966 * Set the back channel max_resp_sz_cached to zero to force the client to
7967 * always set csa_cachethis to FALSE because the current implementation
7968 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7969 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007970static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7971 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007972{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007973 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007974 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007975
Andy Adamson18aad3d2013-06-26 12:21:49 -04007976 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7977 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7978
Andy Adamsonfc931582009-04-01 09:22:31 -04007979 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007980 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7981 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007982 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007983 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007984
7985 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007986 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007987 __func__,
7988 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007989 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007990
7991 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007992 args->bc_attrs.max_rqst_sz = max_bc_payload;
7993 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007994 args->bc_attrs.max_resp_sz_cached = 0;
7995 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007996 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007997
7998 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7999 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8000 __func__,
8001 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8002 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8003 args->bc_attrs.max_reqs);
8004}
8005
Trond Myklebust79969dd2015-02-18 11:30:18 -08008006static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8007 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008008{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008009 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008010 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008011
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008012 if (rcvd->max_resp_sz > sent->max_resp_sz)
8013 return -EINVAL;
8014 /*
8015 * Our requested max_ops is the minimum we need; we're not
8016 * prepared to break up compounds into smaller pieces than that.
8017 * So, no point even trying to continue if the server won't
8018 * cooperate:
8019 */
8020 if (rcvd->max_ops < sent->max_ops)
8021 return -EINVAL;
8022 if (rcvd->max_reqs == 0)
8023 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008024 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8025 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008026 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008027}
8028
Trond Myklebust79969dd2015-02-18 11:30:18 -08008029static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8030 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008031{
8032 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008033 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008034
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008035 if (!(res->flags & SESSION4_BACK_CHAN))
8036 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008037 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8038 return -EINVAL;
8039 if (rcvd->max_resp_sz < sent->max_resp_sz)
8040 return -EINVAL;
8041 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8042 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008043 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008044 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008045 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008046 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008047out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008048 return 0;
8049}
Andy Adamson8d353012009-04-01 09:22:32 -04008050
Andy Adamson8d353012009-04-01 09:22:32 -04008051static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008052 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008053{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008054 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008055
Trond Myklebust79969dd2015-02-18 11:30:18 -08008056 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008057 if (ret)
8058 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008059 return nfs4_verify_back_channel_attrs(args, res);
8060}
8061
8062static void nfs4_update_session(struct nfs4_session *session,
8063 struct nfs41_create_session_res *res)
8064{
8065 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008066 /* Mark client id and session as being confirmed */
8067 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8068 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008069 session->flags = res->flags;
8070 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008071 if (res->flags & SESSION4_BACK_CHAN)
8072 memcpy(&session->bc_attrs, &res->bc_attrs,
8073 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008074}
8075
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008076static int _nfs4_proc_create_session(struct nfs_client *clp,
8077 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008078{
8079 struct nfs4_session *session = clp->cl_session;
8080 struct nfs41_create_session_args args = {
8081 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008082 .clientid = clp->cl_clientid,
8083 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008084 .cb_program = NFS4_CALLBACK,
8085 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008086 struct nfs41_create_session_res res;
8087
Andy Adamsonfc931582009-04-01 09:22:31 -04008088 struct rpc_message msg = {
8089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8090 .rpc_argp = &args,
8091 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008092 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008093 };
8094 int status;
8095
Chuck Lever6b26cc82016-05-02 14:40:40 -04008096 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008097 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008098
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008099 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008100 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008101
Trond Myklebustb519d402016-09-11 14:50:01 -04008102 switch (status) {
8103 case -NFS4ERR_STALE_CLIENTID:
8104 case -NFS4ERR_DELAY:
8105 case -ETIMEDOUT:
8106 case -EACCES:
8107 case -EAGAIN:
8108 goto out;
8109 };
8110
8111 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008112 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008113 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008114 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008115 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008116 if (status)
8117 goto out;
8118 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008119 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008120out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008121 return status;
8122}
8123
8124/*
8125 * Issues a CREATE_SESSION operation to the server.
8126 * It is the responsibility of the caller to verify the session is
8127 * expired before calling this routine.
8128 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008129int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008130{
8131 int status;
8132 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008133 struct nfs4_session *session = clp->cl_session;
8134
8135 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8136
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008137 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008138 if (status)
8139 goto out;
8140
Andy Adamsonaacd5532011-11-09 13:58:21 -05008141 /* Init or reset the session slot tables */
8142 status = nfs4_setup_session_slot_tables(session);
8143 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008144 if (status)
8145 goto out;
8146
8147 ptr = (unsigned *)&session->sess_id.data[0];
8148 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8149 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008150out:
8151 dprintk("<-- %s\n", __func__);
8152 return status;
8153}
8154
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008155/*
8156 * Issue the over-the-wire RPC DESTROY_SESSION.
8157 * The caller must serialize access to this routine.
8158 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008159int nfs4_proc_destroy_session(struct nfs4_session *session,
8160 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008161{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008162 struct rpc_message msg = {
8163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8164 .rpc_argp = session,
8165 .rpc_cred = cred,
8166 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008167 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008168
8169 dprintk("--> nfs4_proc_destroy_session\n");
8170
8171 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008172 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8173 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008174
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008175 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008176 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008177
8178 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008179 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008180 "Session has been destroyed regardless...\n", status);
8181
8182 dprintk("<-- nfs4_proc_destroy_session\n");
8183 return status;
8184}
8185
Trond Myklebust7b38c362012-05-23 13:23:31 -04008186/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008187 * Renew the cl_session lease.
8188 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008189struct nfs4_sequence_data {
8190 struct nfs_client *clp;
8191 struct nfs4_sequence_args args;
8192 struct nfs4_sequence_res res;
8193};
8194
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008195static void nfs41_sequence_release(void *data)
8196{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008197 struct nfs4_sequence_data *calldata = data;
8198 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008199
Elena Reshetova212bf412017-10-20 12:53:38 +03008200 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008201 nfs4_schedule_state_renewal(clp);
8202 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008203 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008204}
8205
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008206static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8207{
8208 switch(task->tk_status) {
8209 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008210 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8211 return -EAGAIN;
8212 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008213 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008214 }
8215 return 0;
8216}
8217
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008218static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008219{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008220 struct nfs4_sequence_data *calldata = data;
8221 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008222
Trond Myklebust14516c32010-07-31 14:29:06 -04008223 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8224 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008225
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008226 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008227 if (task->tk_status < 0) {
8228 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008229 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008230 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008231
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008232 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8233 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008234 return;
8235 }
8236 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008237 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008238out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008239 dprintk("<-- %s\n", __func__);
8240}
8241
8242static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8243{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008244 struct nfs4_sequence_data *calldata = data;
8245 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008246 struct nfs4_sequence_args *args;
8247 struct nfs4_sequence_res *res;
8248
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008249 args = task->tk_msg.rpc_argp;
8250 res = task->tk_msg.rpc_resp;
8251
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008252 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008253}
8254
8255static const struct rpc_call_ops nfs41_sequence_ops = {
8256 .rpc_call_done = nfs41_sequence_call_done,
8257 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008258 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008259};
8260
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008261static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8262 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008263 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008264 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008265{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008266 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008267 struct rpc_message msg = {
8268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8269 .rpc_cred = cred,
8270 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008271 struct rpc_task_setup task_setup_data = {
8272 .rpc_client = clp->cl_rpcclient,
8273 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008274 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008275 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008276 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008277 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008278
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008279 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008280 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008281 goto out_err;
8282
8283 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008284 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008285 if (calldata == NULL)
8286 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008287 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008288 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008289 if (is_privileged)
8290 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008291 msg.rpc_argp = &calldata->args;
8292 msg.rpc_resp = &calldata->res;
8293 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008294 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008295
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008296 ret = rpc_run_task(&task_setup_data);
8297 if (IS_ERR(ret))
8298 goto out_err;
8299 return ret;
8300out_put_clp:
8301 nfs_put_client(clp);
8302out_err:
8303 nfs41_release_slot(slot);
8304 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008305}
8306
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008307static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008308{
8309 struct rpc_task *task;
8310 int ret = 0;
8311
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008312 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008313 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008314 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008315 if (IS_ERR(task))
8316 ret = PTR_ERR(task);
8317 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008318 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008319 dprintk("<-- %s status=%d\n", __func__, ret);
8320 return ret;
8321}
8322
8323static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8324{
8325 struct rpc_task *task;
8326 int ret;
8327
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008328 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008329 if (IS_ERR(task)) {
8330 ret = PTR_ERR(task);
8331 goto out;
8332 }
8333 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008334 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008335 ret = task->tk_status;
8336 rpc_put_task(task);
8337out:
8338 dprintk("<-- %s status=%d\n", __func__, ret);
8339 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008340}
8341
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008342struct nfs4_reclaim_complete_data {
8343 struct nfs_client *clp;
8344 struct nfs41_reclaim_complete_args arg;
8345 struct nfs41_reclaim_complete_res res;
8346};
8347
8348static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8349{
8350 struct nfs4_reclaim_complete_data *calldata = data;
8351
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008352 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008353 &calldata->arg.seq_args,
8354 &calldata->res.seq_res,
8355 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008356}
8357
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008358static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8359{
8360 switch(task->tk_status) {
8361 case 0:
8362 case -NFS4ERR_COMPLETE_ALREADY:
8363 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8364 break;
8365 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008366 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008367 /* fall through */
8368 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008369 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008370 case -NFS4ERR_BADSESSION:
8371 case -NFS4ERR_DEADSESSION:
8372 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8373 nfs4_schedule_session_recovery(clp->cl_session,
8374 task->tk_status);
8375 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008376 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008377 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008378 }
8379 return 0;
8380}
8381
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008382static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8383{
8384 struct nfs4_reclaim_complete_data *calldata = data;
8385 struct nfs_client *clp = calldata->clp;
8386 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8387
8388 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008389 if (!nfs41_sequence_done(task, res))
8390 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008391
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008392 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008393 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8394 rpc_restart_call_prepare(task);
8395 return;
8396 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008397 dprintk("<-- %s\n", __func__);
8398}
8399
8400static void nfs4_free_reclaim_complete_data(void *data)
8401{
8402 struct nfs4_reclaim_complete_data *calldata = data;
8403
8404 kfree(calldata);
8405}
8406
8407static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8408 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8409 .rpc_call_done = nfs4_reclaim_complete_done,
8410 .rpc_release = nfs4_free_reclaim_complete_data,
8411};
8412
8413/*
8414 * Issue a global reclaim complete.
8415 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008416static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8417 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008418{
8419 struct nfs4_reclaim_complete_data *calldata;
8420 struct rpc_task *task;
8421 struct rpc_message msg = {
8422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008423 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008424 };
8425 struct rpc_task_setup task_setup_data = {
8426 .rpc_client = clp->cl_rpcclient,
8427 .rpc_message = &msg,
8428 .callback_ops = &nfs4_reclaim_complete_call_ops,
8429 .flags = RPC_TASK_ASYNC,
8430 };
8431 int status = -ENOMEM;
8432
8433 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008434 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008435 if (calldata == NULL)
8436 goto out;
8437 calldata->clp = clp;
8438 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008439
Chuck Levera9c92d62013-08-09 12:48:18 -04008440 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008441 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008442 msg.rpc_argp = &calldata->arg;
8443 msg.rpc_resp = &calldata->res;
8444 task_setup_data.callback_data = calldata;
8445 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008446 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008447 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008448 goto out;
8449 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008450 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008451 if (status == 0)
8452 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008453 rpc_put_task(task);
8454out:
8455 dprintk("<-- %s status=%d\n", __func__, status);
8456 return status;
8457}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008458
8459static void
8460nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8461{
8462 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008463 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008464
8465 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008466 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008467 &lgp->res.seq_res, task);
8468 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008469}
8470
8471static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8472{
8473 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008474
8475 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008476 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008477 dprintk("<-- %s\n", __func__);
8478}
8479
8480static int
8481nfs4_layoutget_handle_exception(struct rpc_task *task,
8482 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8483{
Trond Myklebustee314c22012-10-01 17:25:48 -07008484 struct inode *inode = lgp->args.inode;
8485 struct nfs_server *server = NFS_SERVER(inode);
8486 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008487 int nfs4err = task->tk_status;
8488 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008489 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490
Boaz Harroshed7e5422014-01-22 20:34:54 +02008491 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008492
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008493 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008494 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008495 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008496
8497 /*
8498 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8499 * on the file. set tk_status to -ENODATA to tell upper layer to
8500 * retry go inband.
8501 */
8502 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008503 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008504 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008505 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008506 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8507 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8508 */
8509 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008510 status = -EOVERFLOW;
8511 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008512 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008513 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008514 * (or clients) writing to the same RAID stripe except when
8515 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008516 *
8517 * Treat it like we would RECALLCONFLICT -- we retry for a little
8518 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008519 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008520 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008521 if (lgp->args.minlength == 0) {
8522 status = -EOVERFLOW;
8523 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008524 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008525 status = -EBUSY;
8526 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008527 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008528 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008529 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008530 case -NFS4ERR_DELEG_REVOKED:
8531 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008532 case -NFS4ERR_EXPIRED:
8533 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008534 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008535 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008536 lo = NFS_I(inode)->layout;
8537 /* If the open stateid was bad, then recover it. */
8538 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008539 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008540 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008541 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008542 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008543 break;
8544 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008545
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008546 /*
8547 * Mark the bad layout state as invalid, then retry
8548 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008549 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008550 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008551 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008552 pnfs_free_lseg_list(&head);
8553 status = -EAGAIN;
8554 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008555 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008556
Trond Myklebust8ac09252017-01-23 22:44:12 -05008557 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008558 err = nfs4_handle_exception(server, nfs4err, exception);
8559 if (!status) {
8560 if (exception->retry)
8561 status = -EAGAIN;
8562 else
8563 status = err;
8564 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008565out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008566 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008567 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008568}
8569
Idan Kedar85541162012-08-02 11:47:10 +03008570static size_t max_response_pages(struct nfs_server *server)
8571{
8572 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8573 return nfs_page_array_len(0, max_resp_sz);
8574}
8575
8576static void nfs4_free_pages(struct page **pages, size_t size)
8577{
8578 int i;
8579
8580 if (!pages)
8581 return;
8582
8583 for (i = 0; i < size; i++) {
8584 if (!pages[i])
8585 break;
8586 __free_page(pages[i]);
8587 }
8588 kfree(pages);
8589}
8590
8591static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8592{
8593 struct page **pages;
8594 int i;
8595
8596 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8597 if (!pages) {
8598 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8599 return NULL;
8600 }
8601
8602 for (i = 0; i < size; i++) {
8603 pages[i] = alloc_page(gfp_flags);
8604 if (!pages[i]) {
8605 dprintk("%s: failed to allocate page\n", __func__);
8606 nfs4_free_pages(pages, size);
8607 return NULL;
8608 }
8609 }
8610
8611 return pages;
8612}
8613
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008614static void nfs4_layoutget_release(void *calldata)
8615{
8616 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008617 struct inode *inode = lgp->args.inode;
8618 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008619 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008620
8621 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008622 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008623 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008624 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008625 put_nfs_open_context(lgp->args.ctx);
8626 kfree(calldata);
8627 dprintk("<-- %s\n", __func__);
8628}
8629
8630static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8631 .rpc_call_prepare = nfs4_layoutget_prepare,
8632 .rpc_call_done = nfs4_layoutget_done,
8633 .rpc_release = nfs4_layoutget_release,
8634};
8635
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008636struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008637nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008638{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008639 struct inode *inode = lgp->args.inode;
8640 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008641 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008642 struct rpc_task *task;
8643 struct rpc_message msg = {
8644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8645 .rpc_argp = &lgp->args,
8646 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008647 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008648 };
8649 struct rpc_task_setup task_setup_data = {
8650 .rpc_client = server->client,
8651 .rpc_message = &msg,
8652 .callback_ops = &nfs4_layoutget_call_ops,
8653 .callback_data = lgp,
8654 .flags = RPC_TASK_ASYNC,
8655 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008656 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008657 struct nfs4_exception exception = {
8658 .inode = inode,
8659 .timeout = *timeout,
8660 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008661 int status = 0;
8662
8663 dprintk("--> %s\n", __func__);
8664
Peng Tao4bd5a982014-11-17 11:05:17 +08008665 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8666 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8667
Idan Kedar85541162012-08-02 11:47:10 +03008668 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8669 if (!lgp->args.layout.pages) {
8670 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008671 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008672 }
8673 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8674
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008675 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008676 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008677 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008678
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008679 task = rpc_run_task(&task_setup_data);
8680 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008681 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008682 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008683 if (status == 0) {
8684 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8685 *timeout = exception.timeout;
8686 }
8687
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008688 trace_nfs4_layoutget(lgp->args.ctx,
8689 &lgp->args.range,
8690 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008691 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008692 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008693
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008694 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8695 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008696 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008697 rpc_put_task(task);
8698 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008699 if (status)
8700 return ERR_PTR(status);
8701 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008702}
8703
Benny Halevycbe82602011-05-22 19:52:37 +03008704static void
8705nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8706{
8707 struct nfs4_layoutreturn *lrp = calldata;
8708
8709 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008710 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008711 &lrp->args.seq_args,
8712 &lrp->res.seq_res,
8713 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008714}
8715
8716static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8717{
8718 struct nfs4_layoutreturn *lrp = calldata;
8719 struct nfs_server *server;
8720
8721 dprintk("--> %s\n", __func__);
8722
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008723 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008724 return;
8725
8726 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008727 switch (task->tk_status) {
8728 default:
8729 task->tk_status = 0;
8730 case 0:
8731 break;
8732 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008733 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008734 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008735 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008736 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008737 return;
8738 }
Benny Halevycbe82602011-05-22 19:52:37 +03008739 dprintk("<-- %s\n", __func__);
8740}
8741
8742static void nfs4_layoutreturn_release(void *calldata)
8743{
8744 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008745 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008746
8747 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008748 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008749 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008750 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008751 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8752 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008753 pnfs_put_layout_hdr(lrp->args.layout);
8754 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008755 kfree(calldata);
8756 dprintk("<-- %s\n", __func__);
8757}
8758
8759static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8760 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8761 .rpc_call_done = nfs4_layoutreturn_done,
8762 .rpc_release = nfs4_layoutreturn_release,
8763};
8764
Peng Tao6c166052014-11-17 09:30:40 +08008765int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008766{
8767 struct rpc_task *task;
8768 struct rpc_message msg = {
8769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8770 .rpc_argp = &lrp->args,
8771 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008772 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008773 };
8774 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008775 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008776 .rpc_message = &msg,
8777 .callback_ops = &nfs4_layoutreturn_call_ops,
8778 .callback_data = lrp,
8779 };
Peng Tao6c166052014-11-17 09:30:40 +08008780 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008781
Andrew Elble99ade3c2015-12-02 09:39:51 -05008782 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8783 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8784 &task_setup_data.rpc_client, &msg);
8785
Benny Halevycbe82602011-05-22 19:52:37 +03008786 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008787 if (!sync) {
8788 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8789 if (!lrp->inode) {
8790 nfs4_layoutreturn_release(lrp);
8791 return -EAGAIN;
8792 }
8793 task_setup_data.flags |= RPC_TASK_ASYNC;
8794 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008795 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008796 task = rpc_run_task(&task_setup_data);
8797 if (IS_ERR(task))
8798 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008799 if (sync)
8800 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008801 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008802 dprintk("<-- %s status=%d\n", __func__, status);
8803 rpc_put_task(task);
8804 return status;
8805}
8806
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008807static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008808_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8809 struct pnfs_device *pdev,
8810 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008811{
8812 struct nfs4_getdeviceinfo_args args = {
8813 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008814 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8815 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008816 };
8817 struct nfs4_getdeviceinfo_res res = {
8818 .pdev = pdev,
8819 };
8820 struct rpc_message msg = {
8821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8822 .rpc_argp = &args,
8823 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008824 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008825 };
8826 int status;
8827
8828 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008829 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008830 if (res.notification & ~args.notify_types)
8831 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008832 if (res.notification != args.notify_types)
8833 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008834
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008835 dprintk("<-- %s status=%d\n", __func__, status);
8836
8837 return status;
8838}
8839
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008840int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8841 struct pnfs_device *pdev,
8842 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008843{
8844 struct nfs4_exception exception = { };
8845 int err;
8846
8847 do {
8848 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008849 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008850 &exception);
8851 } while (exception.retry);
8852 return err;
8853}
8854EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8855
Andy Adamson863a3c62011-03-23 13:27:54 +00008856static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8857{
8858 struct nfs4_layoutcommit_data *data = calldata;
8859 struct nfs_server *server = NFS_SERVER(data->args.inode);
8860
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008861 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008862 &data->args.seq_args,
8863 &data->res.seq_res,
8864 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008865}
8866
8867static void
8868nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8869{
8870 struct nfs4_layoutcommit_data *data = calldata;
8871 struct nfs_server *server = NFS_SERVER(data->args.inode);
8872
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008873 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008874 return;
8875
8876 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008877 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8878 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8879 case -NFS4ERR_BADLAYOUT: /* no layout */
8880 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008881 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008882 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008883 break;
8884 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008885 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008886 rpc_restart_call_prepare(task);
8887 return;
8888 }
8889 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008890}
8891
8892static void nfs4_layoutcommit_release(void *calldata)
8893{
8894 struct nfs4_layoutcommit_data *data = calldata;
8895
Andy Adamsondb29c082011-07-30 20:52:38 -04008896 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008897 nfs_post_op_update_inode_force_wcc(data->args.inode,
8898 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008899 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008900 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008901 kfree(data);
8902}
8903
8904static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8905 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8906 .rpc_call_done = nfs4_layoutcommit_done,
8907 .rpc_release = nfs4_layoutcommit_release,
8908};
8909
8910int
Andy Adamsonef311532011-03-12 02:58:10 -05008911nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008912{
8913 struct rpc_message msg = {
8914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8915 .rpc_argp = &data->args,
8916 .rpc_resp = &data->res,
8917 .rpc_cred = data->cred,
8918 };
8919 struct rpc_task_setup task_setup_data = {
8920 .task = &data->task,
8921 .rpc_client = NFS_CLIENT(data->args.inode),
8922 .rpc_message = &msg,
8923 .callback_ops = &nfs4_layoutcommit_ops,
8924 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008925 };
8926 struct rpc_task *task;
8927 int status = 0;
8928
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008929 dprintk("NFS: initiating layoutcommit call. sync %d "
8930 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008931 data->args.lastbytewritten,
8932 data->args.inode->i_ino);
8933
Trond Myklebust472e2592015-02-05 16:50:30 -05008934 if (!sync) {
8935 data->inode = nfs_igrab_and_active(data->args.inode);
8936 if (data->inode == NULL) {
8937 nfs4_layoutcommit_release(data);
8938 return -EAGAIN;
8939 }
8940 task_setup_data.flags = RPC_TASK_ASYNC;
8941 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008942 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008943 task = rpc_run_task(&task_setup_data);
8944 if (IS_ERR(task))
8945 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008946 if (sync)
8947 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008948 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008949 dprintk("%s: status %d\n", __func__, status);
8950 rpc_put_task(task);
8951 return status;
8952}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008953
Andy Adamson97431202013-08-08 10:57:56 -04008954/**
8955 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8956 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8957 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008958static int
8959_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008960 struct nfs_fsinfo *info,
8961 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008962{
8963 struct nfs41_secinfo_no_name_args args = {
8964 .style = SECINFO_STYLE_CURRENT_FH,
8965 };
8966 struct nfs4_secinfo_res res = {
8967 .flavors = flavors,
8968 };
8969 struct rpc_message msg = {
8970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8971 .rpc_argp = &args,
8972 .rpc_resp = &res,
8973 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008974 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008975 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008976 int status;
8977
8978 if (use_integrity) {
8979 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008980 cred = nfs4_get_clid_cred(server->nfs_client);
8981 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008982 }
8983
8984 dprintk("--> %s\n", __func__);
8985 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8986 &res.seq_res, 0);
8987 dprintk("<-- %s status=%d\n", __func__, status);
8988
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008989 if (cred)
8990 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008991
8992 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008993}
8994
8995static int
8996nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8997 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8998{
8999 struct nfs4_exception exception = { };
9000 int err;
9001 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009002 /* first try using integrity protection */
9003 err = -NFS4ERR_WRONGSEC;
9004
9005 /* try to use integrity protection with machine cred */
9006 if (_nfs4_is_integrity_protected(server->nfs_client))
9007 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9008 flavors, true);
9009
9010 /*
9011 * if unable to use integrity protection, or SECINFO with
9012 * integrity protection returns NFS4ERR_WRONGSEC (which is
9013 * disallowed by spec, but exists in deployed servers) use
9014 * the current filesystem's rpc_client and the user cred.
9015 */
9016 if (err == -NFS4ERR_WRONGSEC)
9017 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9018 flavors, false);
9019
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009020 switch (err) {
9021 case 0:
9022 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009023 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009024 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009025 default:
9026 err = nfs4_handle_exception(server, err, &exception);
9027 }
9028 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009029out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009030 return err;
9031}
9032
9033static int
9034nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9035 struct nfs_fsinfo *info)
9036{
9037 int err;
9038 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009039 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009040 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009041 struct nfs4_secinfo4 *secinfo;
9042 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009043
9044 page = alloc_page(GFP_KERNEL);
9045 if (!page) {
9046 err = -ENOMEM;
9047 goto out;
9048 }
9049
9050 flavors = page_address(page);
9051 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9052
9053 /*
9054 * Fall back on "guess and check" method if
9055 * the server doesn't support SECINFO_NO_NAME
9056 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009057 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009058 err = nfs4_find_root_sec(server, fhandle, info);
9059 goto out_freepage;
9060 }
9061 if (err)
9062 goto out_freepage;
9063
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009064 for (i = 0; i < flavors->num_flavors; i++) {
9065 secinfo = &flavors->flavors[i];
9066
9067 switch (secinfo->flavor) {
9068 case RPC_AUTH_NULL:
9069 case RPC_AUTH_UNIX:
9070 case RPC_AUTH_GSS:
9071 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9072 &secinfo->flavor_info);
9073 break;
9074 default:
9075 flavor = RPC_AUTH_MAXFLAVOR;
9076 break;
9077 }
9078
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009079 if (!nfs_auth_info_match(&server->auth_info, flavor))
9080 flavor = RPC_AUTH_MAXFLAVOR;
9081
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009082 if (flavor != RPC_AUTH_MAXFLAVOR) {
9083 err = nfs4_lookup_root_sec(server, fhandle,
9084 info, flavor);
9085 if (!err)
9086 break;
9087 }
9088 }
9089
9090 if (flavor == RPC_AUTH_MAXFLAVOR)
9091 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009092
9093out_freepage:
9094 put_page(page);
9095 if (err == -EACCES)
9096 return -EPERM;
9097out:
9098 return err;
9099}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009100
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009101static int _nfs41_test_stateid(struct nfs_server *server,
9102 nfs4_stateid *stateid,
9103 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009104{
9105 int status;
9106 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009107 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009108 };
9109 struct nfs41_test_stateid_res res;
9110 struct rpc_message msg = {
9111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9112 .rpc_argp = &args,
9113 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009114 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009115 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009116 struct rpc_clnt *rpc_client = server->client;
9117
9118 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9119 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009120
Chuck Lever38527b12012-07-11 16:30:23 -04009121 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009122 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009123 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009124 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009125 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009126 if (status != NFS_OK) {
9127 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009128 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009129 }
9130 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009131 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009132}
9133
Trond Myklebust43912bb2016-09-22 13:38:56 -04009134static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9135 int err, struct nfs4_exception *exception)
9136{
9137 exception->retry = 0;
9138 switch(err) {
9139 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009140 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009141 nfs4_handle_exception(server, err, exception);
9142 break;
9143 case -NFS4ERR_BADSESSION:
9144 case -NFS4ERR_BADSLOT:
9145 case -NFS4ERR_BAD_HIGH_SLOT:
9146 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9147 case -NFS4ERR_DEADSESSION:
9148 nfs4_do_handle_exception(server, err, exception);
9149 }
9150}
9151
Chuck Lever38527b12012-07-11 16:30:23 -04009152/**
9153 * nfs41_test_stateid - perform a TEST_STATEID operation
9154 *
9155 * @server: server / transport on which to perform the operation
9156 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009157 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009158 *
9159 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9160 * Otherwise a negative NFS4ERR value is returned if the operation
9161 * failed or the state ID is not currently valid.
9162 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009163static int nfs41_test_stateid(struct nfs_server *server,
9164 nfs4_stateid *stateid,
9165 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009166{
9167 struct nfs4_exception exception = { };
9168 int err;
9169 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009170 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009171 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009172 } while (exception.retry);
9173 return err;
9174}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009175
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009176struct nfs_free_stateid_data {
9177 struct nfs_server *server;
9178 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009179 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009180};
9181
9182static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9183{
9184 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009185 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009186 &data->args.seq_args,
9187 &data->res.seq_res,
9188 task);
9189}
9190
9191static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9192{
9193 struct nfs_free_stateid_data *data = calldata;
9194
9195 nfs41_sequence_done(task, &data->res.seq_res);
9196
9197 switch (task->tk_status) {
9198 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009199 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009200 rpc_restart_call_prepare(task);
9201 }
9202}
9203
9204static void nfs41_free_stateid_release(void *calldata)
9205{
9206 kfree(calldata);
9207}
9208
Trond Myklebust17f26b12013-08-21 15:48:42 -04009209static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009210 .rpc_call_prepare = nfs41_free_stateid_prepare,
9211 .rpc_call_done = nfs41_free_stateid_done,
9212 .rpc_release = nfs41_free_stateid_release,
9213};
9214
9215static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009216 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009217 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009218 bool privileged)
9219{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009220 struct rpc_message msg = {
9221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009222 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009223 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009224 struct rpc_task_setup task_setup = {
9225 .rpc_client = server->client,
9226 .rpc_message = &msg,
9227 .callback_ops = &nfs41_free_stateid_ops,
9228 .flags = RPC_TASK_ASYNC,
9229 };
9230 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009231
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009232 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9233 &task_setup.rpc_client, &msg);
9234
Chuck Lever38527b12012-07-11 16:30:23 -04009235 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009236 data = kmalloc(sizeof(*data), GFP_NOFS);
9237 if (!data)
9238 return ERR_PTR(-ENOMEM);
9239 data->server = server;
9240 nfs4_stateid_copy(&data->args.stateid, stateid);
9241
9242 task_setup.callback_data = data;
9243
9244 msg.rpc_argp = &data->args;
9245 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009246 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009247 if (privileged)
9248 nfs4_set_sequence_privileged(&data->args.seq_args);
9249
9250 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009251}
9252
Chuck Lever38527b12012-07-11 16:30:23 -04009253/**
9254 * nfs41_free_stateid - perform a FREE_STATEID operation
9255 *
9256 * @server: server / transport on which to perform the operation
9257 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009258 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009259 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009260 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009261 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009262 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009263static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009264 const nfs4_stateid *stateid,
9265 struct rpc_cred *cred,
9266 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009267{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009268 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009269
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009270 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009271 if (IS_ERR(task))
9272 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009273 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009274 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009275}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009276
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009277static void
9278nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009279{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009280 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009281
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009282 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009283 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009284}
9285
Trond Myklebust36281ca2012-03-04 18:13:56 -05009286static bool nfs41_match_stateid(const nfs4_stateid *s1,
9287 const nfs4_stateid *s2)
9288{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009289 if (s1->type != s2->type)
9290 return false;
9291
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009292 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009293 return false;
9294
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009295 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009296 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009297
Anna Schumaker045c5512017-01-11 16:59:48 -05009298 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009299}
9300
Andy Adamson557134a2009-04-01 09:21:53 -04009301#endif /* CONFIG_NFS_V4_1 */
9302
Trond Myklebust36281ca2012-03-04 18:13:56 -05009303static bool nfs4_match_stateid(const nfs4_stateid *s1,
9304 const nfs4_stateid *s2)
9305{
Trond Myklebustf597c532012-03-04 18:13:56 -05009306 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009307}
9308
9309
Trond Myklebust17280172012-03-11 13:11:00 -04009310static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009311 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009312 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313 .recover_open = nfs4_open_reclaim,
9314 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009315 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009316 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317};
9318
Andy Adamson591d71c2009-04-01 09:22:47 -04009319#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009320static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009321 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9322 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9323 .recover_open = nfs4_open_reclaim,
9324 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009325 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009326 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009327 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009328};
9329#endif /* CONFIG_NFS_V4_1 */
9330
Trond Myklebust17280172012-03-11 13:11:00 -04009331static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009332 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009333 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009334 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009336 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337};
9338
Andy Adamson591d71c2009-04-01 09:22:47 -04009339#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009340static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009341 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9342 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009343 .recover_open = nfs41_open_expired,
9344 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009345 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009346};
9347#endif /* CONFIG_NFS_V4_1 */
9348
Trond Myklebust17280172012-03-11 13:11:00 -04009349static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009350 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009351 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009352 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009353};
9354
9355#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009356static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009357 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009358 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009359 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009360};
9361#endif
9362
Chuck Leverec011fe2013-10-17 14:12:39 -04009363static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009364 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009365 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009366};
9367
9368#if defined(CONFIG_NFS_V4_1)
9369static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009370 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009371 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009372};
9373#endif /* CONFIG_NFS_V4_1 */
9374
Trond Myklebust97dc1352010-06-16 09:52:26 -04009375static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9376 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009377 .init_caps = NFS_CAP_READDIRPLUS
9378 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009379 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009380 .init_client = nfs40_init_client,
9381 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009382 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009383 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009384 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009385 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009386 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009387 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009388 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9389 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9390 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009391 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009392};
9393
9394#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009395static struct nfs_seqid *
9396nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9397{
9398 return NULL;
9399}
9400
Trond Myklebust97dc1352010-06-16 09:52:26 -04009401static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9402 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009403 .init_caps = NFS_CAP_READDIRPLUS
9404 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009405 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009406 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009407 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009408 .init_client = nfs41_init_client,
9409 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009410 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009411 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009412 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009413 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009414 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009415 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009416 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009417 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9418 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9419 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009420 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009421};
9422#endif
9423
Steve Dickson42c2c422013-05-22 12:50:38 -04009424#if defined(CONFIG_NFS_V4_2)
9425static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9426 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009427 .init_caps = NFS_CAP_READDIRPLUS
9428 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009429 | NFS_CAP_POSIX_LOCK
9430 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009431 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009432 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009433 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009434 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009435 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009436 | NFS_CAP_LAYOUTSTATS
9437 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009438 .init_client = nfs41_init_client,
9439 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009440 .match_stateid = nfs41_match_stateid,
9441 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009442 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009443 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009444 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009445 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009446 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009447 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9448 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9449 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009450 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009451};
9452#endif
9453
Trond Myklebust97dc1352010-06-16 09:52:26 -04009454const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9455 [0] = &nfs_v4_0_minor_ops,
9456#if defined(CONFIG_NFS_V4_1)
9457 [1] = &nfs_v4_1_minor_ops,
9458#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009459#if defined(CONFIG_NFS_V4_2)
9460 [2] = &nfs_v4_2_minor_ops,
9461#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009462};
9463
Trond Myklebust13997822016-07-24 17:10:52 -04009464static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009465{
9466 ssize_t error, error2;
9467
9468 error = generic_listxattr(dentry, list, size);
9469 if (error < 0)
9470 return error;
9471 if (list) {
9472 list += error;
9473 size -= error;
9474 }
9475
9476 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9477 if (error2 < 0)
9478 return error2;
9479 return error + error2;
9480}
9481
Trond Myklebust17f26b12013-08-21 15:48:42 -04009482static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009483 .create = nfs_create,
9484 .lookup = nfs_lookup,
9485 .atomic_open = nfs_atomic_open,
9486 .link = nfs_link,
9487 .unlink = nfs_unlink,
9488 .symlink = nfs_symlink,
9489 .mkdir = nfs_mkdir,
9490 .rmdir = nfs_rmdir,
9491 .mknod = nfs_mknod,
9492 .rename = nfs_rename,
9493 .permission = nfs_permission,
9494 .getattr = nfs_getattr,
9495 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009496 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009497};
9498
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009499static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009500 .permission = nfs_permission,
9501 .getattr = nfs_getattr,
9502 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009503 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009504};
9505
David Howells509de812006-08-22 20:06:11 -04009506const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 .version = 4, /* protocol version */
9508 .dentry_ops = &nfs4_dentry_operations,
9509 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009510 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009511 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009513 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009514 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009515 .getattr = nfs4_proc_getattr,
9516 .setattr = nfs4_proc_setattr,
9517 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009518 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009519 .access = nfs4_proc_access,
9520 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 .create = nfs4_proc_create,
9522 .remove = nfs4_proc_remove,
9523 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009524 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009526 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009527 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009528 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 .link = nfs4_proc_link,
9530 .symlink = nfs4_proc_symlink,
9531 .mkdir = nfs4_proc_mkdir,
9532 .rmdir = nfs4_proc_remove,
9533 .readdir = nfs4_proc_readdir,
9534 .mknod = nfs4_proc_mknod,
9535 .statfs = nfs4_proc_statfs,
9536 .fsinfo = nfs4_proc_fsinfo,
9537 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009538 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009540 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009541 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009542 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009543 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009544 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009546 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009547 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009549 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009550 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009551 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009552 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009553 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009554 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009555 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009556 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009557 .create_server = nfs4_create_server,
9558 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559};
9560
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009561static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009562 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009563 .list = nfs4_xattr_list_nfs4_acl,
9564 .get = nfs4_xattr_get_nfs4_acl,
9565 .set = nfs4_xattr_set_nfs4_acl,
9566};
9567
9568const struct xattr_handler *nfs4_xattr_handlers[] = {
9569 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009570#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9571 &nfs4_xattr_nfs4_label_handler,
9572#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009573 NULL
9574};
9575
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576/*
9577 * Local variables:
9578 * c-basic-offset: 8
9579 * End:
9580 */