blob: 4a5b01c91ab4f0c6732871892d0941a13a576c07 [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;
Trond Myklebust73800202017-11-06 15:28:07 -05003169 case -NFS4ERR_OLD_STATEID:
3170 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003171 calldata->inode))
3172 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003173 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003174 case -NFS4ERR_ADMIN_REVOKED:
3175 case -NFS4ERR_DELEG_REVOKED:
3176 case -NFS4ERR_EXPIRED:
3177 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003178 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3179 case -NFS4ERR_WRONG_CRED:
3180 calldata->arg.lr_args = NULL;
3181 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003182 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003183 }
3184 }
3185
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003186 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 * the state_owner. we keep this around to process errors
3188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 switch (task->tk_status) {
3190 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003191 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003192 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003193 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003194 case -NFS4ERR_ACCESS:
3195 if (calldata->arg.bitmask != NULL) {
3196 calldata->arg.bitmask = NULL;
3197 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003198 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003199
3200 }
3201 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003202 case -NFS4ERR_OLD_STATEID:
3203 /* Did we race with OPEN? */
3204 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003205 state))
3206 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003207 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003208 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003210 case -NFS4ERR_EXPIRED:
3211 nfs4_free_revoked_stateid(server,
3212 &calldata->arg.stateid,
3213 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003214 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003215 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 default:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003218 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
3219 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003221 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3222 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003223out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003224 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003225 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003226 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003227 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003228 return;
3229lr_restart:
3230 calldata->res.lr_ret = 0;
3231out_restart:
3232 task->tk_status = 0;
3233 rpc_restart_call_prepare(task);
3234 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003237static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003239 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003240 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003241 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003242 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003243 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003244
Chuck Levera3ca5652012-03-01 17:00:40 -05003245 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003247 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003248
Trond Myklebust88069f72009-12-08 08:33:16 -05003249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003250 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003251 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3252 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3253 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003254 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003255 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003256 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003257 if (state->n_rdonly == 0)
3258 call_close |= is_rdonly;
3259 else if (is_rdonly)
3260 calldata->arg.fmode |= FMODE_READ;
3261 if (state->n_wronly == 0)
3262 call_close |= is_wronly;
3263 else if (is_wronly)
3264 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003265 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3266 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003267 } else if (is_rdwr)
3268 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3269
Trond Myklebust5cc78612016-11-14 11:19:56 -05003270 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003271 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003272 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003273 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003274
3275 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003276 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003277 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003278 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003279
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003280 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003281 nfs_release_seqid(calldata->arg.seqid);
3282 goto out_wait;
3283 }
3284
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003285 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003286 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003287
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003288 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003289 /* Close-to-open cache consistency revalidation */
3290 if (!nfs4_have_delegation(inode, FMODE_READ))
3291 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3292 else
3293 calldata->arg.bitmask = NULL;
3294 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003295
Trond Myklebust6ae37332015-01-30 14:21:14 -05003296 calldata->arg.share_access =
3297 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3298 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003299
Trond Myklebustd8d84982016-12-19 12:14:44 -05003300 if (calldata->res.fattr == NULL)
3301 calldata->arg.bitmask = NULL;
3302 else if (calldata->arg.bitmask == NULL)
3303 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003304 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003305 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003306 &calldata->arg.seq_args,
3307 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003308 task) != 0)
3309 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003310 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003311 return;
3312out_no_action:
3313 task->tk_action = NULL;
3314out_wait:
3315 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003318static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003319 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003320 .rpc_call_done = nfs4_close_done,
3321 .rpc_release = nfs4_free_closedata,
3322};
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324/*
3325 * It is possible for data to be read/written from a mem-mapped file
3326 * after the sys_close call (which hits the vfs layer as a flush).
3327 * This means that we can't safely call nfsv4 close on a file until
3328 * the inode is cleared. This in turn means that we are not good
3329 * NFSv4 citizens - we do not indicate to the server to update the file's
3330 * share state even when we are done with one of the three share
3331 * stateid's in the inode.
3332 *
3333 * NOTE: Caller must be holding the sp->so_owner semaphore!
3334 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003335int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003337 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003338 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003340 struct nfs4_state_owner *sp = state->owner;
3341 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003342 struct rpc_message msg = {
3343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3344 .rpc_cred = state->owner->so_cred,
3345 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003346 struct rpc_task_setup task_setup_data = {
3347 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003348 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003349 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003350 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003351 .flags = RPC_TASK_ASYNC,
3352 };
Trond Myklebust95121352005-10-18 14:20:12 -07003353 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003355 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3356 &task_setup_data.rpc_client, &msg);
3357
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003358 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003360 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003361 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003362 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003364 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003365 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3366 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003368 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3369 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003370 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003371 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003372 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003373 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003374 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003375 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003376 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003377 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003378 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003379 calldata->lr.roc = pnfs_roc(state->inode,
3380 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3381 if (calldata->lr.roc) {
3382 calldata->arg.lr_args = &calldata->lr.arg;
3383 calldata->res.lr_res = &calldata->lr.res;
3384 }
Al Viro643168c2011-06-22 18:20:23 -04003385 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003386
Trond Myklebust1174dd12010-12-21 10:52:24 -05003387 msg.rpc_argp = &calldata->arg;
3388 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003389 task_setup_data.callback_data = calldata;
3390 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003391 if (IS_ERR(task))
3392 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003393 status = 0;
3394 if (wait)
3395 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003396 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003397 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003398out_free_calldata:
3399 kfree(calldata);
3400out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003401 nfs4_put_open_state(state);
3402 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
Trond Myklebust2b484292010-09-17 10:56:51 -04003406static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003407nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3408 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003411 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3412
3413 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Trond Myklebust565277f2007-10-15 18:17:53 -04003415 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003416 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003417
3418 nfs4_label_release_security(label);
3419
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003420 if (IS_ERR(state))
3421 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003422 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Trond Myklebust1185a552009-12-03 15:54:02 -05003425static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003426{
3427 if (ctx->state == NULL)
3428 return;
3429 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003430 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003431 else
Al Viro643168c2011-06-22 18:20:23 -04003432 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Trond Myklebustb944dba2013-11-04 15:20:20 -05003435#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3436#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003437#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3440{
Kinglong Mee8c612822015-08-26 21:12:58 +08003441 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003442 struct nfs4_server_caps_arg args = {
3443 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003444 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003445 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 struct nfs4_server_caps_res res = {};
3447 struct rpc_message msg = {
3448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003449 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 .rpc_resp = &res,
3451 };
3452 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003453 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Kinglong Mee8c612822015-08-26 21:12:58 +08003455 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3456 FATTR4_WORD0_FH_EXPIRE_TYPE |
3457 FATTR4_WORD0_LINK_SUPPORT |
3458 FATTR4_WORD0_SYMLINK_SUPPORT |
3459 FATTR4_WORD0_ACLSUPPORT;
3460 if (minorversion)
3461 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3462
Bryan Schumaker7c513052011-03-24 17:12:24 +00003463 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003465 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003466 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003467 case 0:
3468 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3469 res.attr_bitmask[2] = 0;
3470 break;
3471 case 1:
3472 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3473 break;
3474 case 2:
3475 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003478 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3479 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3480 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3481 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003482 NFS_CAP_CTIME|NFS_CAP_MTIME|
3483 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003484 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3485 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 server->caps |= NFS_CAP_ACLS;
3487 if (res.has_links != 0)
3488 server->caps |= NFS_CAP_HARDLINKS;
3489 if (res.has_symlinks != 0)
3490 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003491 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3492 server->caps |= NFS_CAP_FILEID;
3493 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3494 server->caps |= NFS_CAP_MODE;
3495 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3496 server->caps |= NFS_CAP_NLINK;
3497 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3498 server->caps |= NFS_CAP_OWNER;
3499 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3500 server->caps |= NFS_CAP_OWNER_GROUP;
3501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3502 server->caps |= NFS_CAP_ATIME;
3503 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3504 server->caps |= NFS_CAP_CTIME;
3505 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3506 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003507#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3508 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3509 server->caps |= NFS_CAP_SECURITY_LABEL;
3510#endif
3511 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3512 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003513 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003514
Trond Myklebusta65318b2009-03-11 14:10:28 -04003515 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3516 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3517 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003518 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003519
3520 /* Avoid a regression due to buggy server */
3521 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3522 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003523 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3524 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003527 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 return status;
3531}
3532
Trond Myklebust55a97592006-06-09 09:34:19 -04003533int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535 struct nfs4_exception exception = { };
3536 int err;
3537 do {
3538 err = nfs4_handle_exception(server,
3539 _nfs4_server_capabilities(server, fhandle),
3540 &exception);
3541 } while (exception.retry);
3542 return err;
3543}
3544
3545static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3546 struct nfs_fsinfo *info)
3547{
David Quigleyaa9c2662013-05-22 12:50:44 -04003548 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003550 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 };
3552 struct nfs4_lookup_res res = {
3553 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003554 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 .fh = fhandle,
3556 };
3557 struct rpc_message msg = {
3558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3559 .rpc_argp = &args,
3560 .rpc_resp = &res,
3561 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003562
David Quigleyaa9c2662013-05-22 12:50:44 -04003563 bitmask[0] = nfs4_fattr_bitmap[0];
3564 bitmask[1] = nfs4_fattr_bitmap[1];
3565 /*
3566 * Process the label in the upcoming getfattr
3567 */
3568 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3569
Trond Myklebust0e574af2005-10-27 22:12:38 -04003570 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003571 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572}
3573
3574static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3575 struct nfs_fsinfo *info)
3576{
3577 struct nfs4_exception exception = { };
3578 int err;
3579 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003580 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003581 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003582 switch (err) {
3583 case 0:
3584 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003585 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003586 default:
3587 err = nfs4_handle_exception(server, err, &exception);
3588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003590out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 return err;
3592}
3593
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003594static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3595 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3596{
Trond Myklebustc2190662013-08-26 19:23:04 -04003597 struct rpc_auth_create_args auth_args = {
3598 .pseudoflavor = flavor,
3599 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003600 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003601
Trond Myklebustc2190662013-08-26 19:23:04 -04003602 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003603 if (IS_ERR(auth))
3604 return -EACCES;
3605 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003606}
3607
Chuck Lever9a744ba2013-03-16 15:56:02 -04003608/*
3609 * Retry pseudoroot lookup with various security flavors. We do this when:
3610 *
3611 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3612 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3613 *
3614 * Returns zero on success, or a negative NFS4ERR value, or a
3615 * negative errno value.
3616 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003617static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003618 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003620 /* Per 3530bis 15.33.5 */
3621 static const rpc_authflavor_t flav_array[] = {
3622 RPC_AUTH_GSS_KRB5P,
3623 RPC_AUTH_GSS_KRB5I,
3624 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003625 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003626 RPC_AUTH_NULL,
3627 };
3628 int status = -EPERM;
3629 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003631 if (server->auth_info.flavor_len > 0) {
3632 /* try each flavor specified by user */
3633 for (i = 0; i < server->auth_info.flavor_len; i++) {
3634 status = nfs4_lookup_root_sec(server, fhandle, info,
3635 server->auth_info.flavors[i]);
3636 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3637 continue;
3638 break;
3639 }
3640 } else {
3641 /* no flavors specified by user, try default list */
3642 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3643 status = nfs4_lookup_root_sec(server, fhandle, info,
3644 flav_array[i]);
3645 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3646 continue;
3647 break;
3648 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003649 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003650
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003651 /*
3652 * -EACCESS could mean that the user doesn't have correct permissions
3653 * to access the mount. It could also mean that we tried to mount
3654 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3655 * existing mount programs don't handle -EACCES very well so it should
3656 * be mapped to -EPERM instead.
3657 */
3658 if (status == -EACCES)
3659 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003660 return status;
3661}
3662
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003663/**
3664 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3665 * @server: initialized nfs_server handle
3666 * @fhandle: we fill in the pseudo-fs root file handle
3667 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003668 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003669 *
3670 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003671 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003672int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003673 struct nfs_fsinfo *info,
3674 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003675{
Andre Przywarac7757072015-04-23 17:17:40 +01003676 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003677
Andre Przywarac7757072015-04-23 17:17:40 +01003678 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003679 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003680
3681 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003682 status = server->nfs_client->cl_mvops->find_root_sec(server,
3683 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 if (status == 0)
3686 status = nfs4_server_capabilities(server, fhandle);
3687 if (status == 0)
3688 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003689
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003690 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Bryan Schumakerbae36242012-05-10 15:07:31 -04003693static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3694 struct nfs_fsinfo *info)
3695{
3696 int error;
3697 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003698 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003699
3700 error = nfs4_server_capabilities(server, mntfh);
3701 if (error < 0) {
3702 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3703 return error;
3704 }
3705
David Quigley14c43f72013-05-22 12:50:43 -04003706 label = nfs4_label_alloc(server, GFP_KERNEL);
3707 if (IS_ERR(label))
3708 return PTR_ERR(label);
3709
David Quigley1775fd32013-05-22 12:50:42 -04003710 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003711 if (error < 0) {
3712 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003713 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003714 }
3715
3716 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3717 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3718 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3719
David Quigley14c43f72013-05-22 12:50:43 -04003720err_free_label:
3721 nfs4_label_free(label);
3722
Bryan Schumakerbae36242012-05-10 15:07:31 -04003723 return error;
3724}
3725
Manoj Naik6b97fd32006-06-09 09:34:29 -04003726/*
3727 * Get locations and (maybe) other attributes of a referral.
3728 * Note that we'll actually follow the referral later when
3729 * we detect fsid mismatch in inode revalidation
3730 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003731static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3732 const struct qstr *name, struct nfs_fattr *fattr,
3733 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003734{
3735 int status = -ENOMEM;
3736 struct page *page = NULL;
3737 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003738
3739 page = alloc_page(GFP_KERNEL);
3740 if (page == NULL)
3741 goto out;
3742 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3743 if (locations == NULL)
3744 goto out;
3745
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003746 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003747 if (status != 0)
3748 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003749
3750 /*
3751 * If the fsid didn't change, this is a migration event, not a
3752 * referral. Cause us to drop into the exception handler, which
3753 * will kick off migration recovery.
3754 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003755 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003756 dprintk("%s: server did not return a different fsid for"
3757 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003758 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003759 goto out;
3760 }
Andy Adamson533eb462011-06-13 18:25:56 -04003761 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3762 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003763
Andy Adamson533eb462011-06-13 18:25:56 -04003764 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003765 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003766 memset(fhandle, 0, sizeof(struct nfs_fh));
3767out:
3768 if (page)
3769 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003770 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003771 return status;
3772}
3773
David Quigley1775fd32013-05-22 12:50:42 -04003774static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3775 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776{
3777 struct nfs4_getattr_arg args = {
3778 .fh = fhandle,
3779 .bitmask = server->attr_bitmask,
3780 };
3781 struct nfs4_getattr_res res = {
3782 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003783 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 .server = server,
3785 };
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3788 .rpc_argp = &args,
3789 .rpc_resp = &res,
3790 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003791
3792 args.bitmask = nfs4_bitmask(server, label);
3793
Trond Myklebust0e574af2005-10-27 22:12:38 -04003794 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003795 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
David Quigley1775fd32013-05-22 12:50:42 -04003798static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3799 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
3801 struct nfs4_exception exception = { };
3802 int err;
3803 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003804 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3805 trace_nfs4_getattr(server, fhandle, fattr, err);
3806 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 &exception);
3808 } while (exception.retry);
3809 return err;
3810}
3811
3812/*
3813 * The file is not closed if it is opened due to the a request to change
3814 * the size of the file. The open call will not be needed once the
3815 * VFS layer lookup-intents are implemented.
3816 *
3817 * Close is called when the inode is destroyed.
3818 * If we haven't opened the file for O_WRONLY, we
3819 * need to in the size_change case to obtain a stateid.
3820 *
3821 * Got race?
3822 * Because OPEN is always done by name in nfsv4, it is
3823 * possible that we opened a different file by the same
3824 * name. We can recognize this race condition, but we
3825 * can't do anything about it besides returning an error.
3826 *
3827 * This will be fixed with VFS changes (lookup-intent).
3828 */
3829static int
3830nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3831 struct iattr *sattr)
3832{
David Howells2b0143b2015-03-17 22:25:59 +00003833 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003834 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003835 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003836 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 int status;
3838
Peng Tao88ac8152014-09-12 11:04:10 +08003839 if (pnfs_ld_layoutret_on_setattr(inode) &&
3840 sattr->ia_valid & ATTR_SIZE &&
3841 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003842 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003843
Trond Myklebust0e574af2005-10-27 22:12:38 -04003844 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Andy Adamson26699402012-05-30 16:12:24 -04003846 /* Deal with open(O_TRUNC) */
3847 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003848 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003849
3850 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003851 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003852 return 0;
3853
Trond Myklebustd5308382005-11-04 15:33:38 -05003854 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003855 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003856
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003857 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003858 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003859 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003860 }
3861
David Quigley14c43f72013-05-22 12:50:43 -04003862 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3863 if (IS_ERR(label))
3864 return PTR_ERR(label);
3865
NeilBrown29b59f92016-10-13 15:26:47 +11003866 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003867 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003868 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003869 nfs_setsecurity(inode, fattr, label);
3870 }
David Quigley14c43f72013-05-22 12:50:43 -04003871 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return status;
3873}
3874
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003875static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3876 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003877 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003878{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003879 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003880 int status;
3881 struct nfs4_lookup_arg args = {
3882 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003883 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003884 .name = name,
3885 };
3886 struct nfs4_lookup_res res = {
3887 .server = server,
3888 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003889 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003890 .fh = fhandle,
3891 };
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3894 .rpc_argp = &args,
3895 .rpc_resp = &res,
3896 };
3897
David Quigleyaa9c2662013-05-22 12:50:44 -04003898 args.bitmask = nfs4_bitmask(server, label);
3899
David Howells2b3de442006-08-22 20:06:09 -04003900 nfs_fattr_init(fattr);
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003903 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 dprintk("NFS reply lookup: %d\n", status);
3905 return status;
3906}
3907
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003908static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003909{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003910 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003911 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003912 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3913 fattr->nlink = 2;
3914}
3915
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003916static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003917 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003918 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003919{
3920 struct nfs4_exception exception = { };
3921 struct rpc_clnt *client = *clnt;
3922 int err;
3923 do {
David Quigley1775fd32013-05-22 12:50:42 -04003924 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003925 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003926 switch (err) {
3927 case -NFS4ERR_BADNAME:
3928 err = -ENOENT;
3929 goto out;
3930 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003931 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003932 if (err == -NFS4ERR_MOVED)
3933 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003934 goto out;
3935 case -NFS4ERR_WRONGSEC:
3936 err = -EPERM;
3937 if (client != *clnt)
3938 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003939 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003940 if (IS_ERR(client))
3941 return PTR_ERR(client);
3942
3943 exception.retry = 1;
3944 break;
3945 default:
3946 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3947 }
3948 } while (exception.retry);
3949
3950out:
3951 if (err == 0)
3952 *clnt = client;
3953 else if (client != *clnt)
3954 rpc_shutdown_client(client);
3955
3956 return err;
3957}
3958
Al Virobeffb8f2016-07-20 16:34:42 -04003959static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003960 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3961 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003963 int status;
3964 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003965
David Quigley1775fd32013-05-22 12:50:42 -04003966 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003967 if (client != NFS_CLIENT(dir)) {
3968 rpc_shutdown_client(client);
3969 nfs_fixup_secinfo_attributes(fattr);
3970 }
3971 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003974struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003975nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003976 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3977{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003978 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003979 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003980
David Quigley1775fd32013-05-22 12:50:42 -04003981 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003982 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003983 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003984 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003985}
3986
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003987static int _nfs4_proc_lookupp(struct inode *inode,
3988 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3989 struct nfs4_label *label)
3990{
3991 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3992 struct nfs_server *server = NFS_SERVER(inode);
3993 int status;
3994 struct nfs4_lookupp_arg args = {
3995 .bitmask = server->attr_bitmask,
3996 .fh = NFS_FH(inode),
3997 };
3998 struct nfs4_lookupp_res res = {
3999 .server = server,
4000 .fattr = fattr,
4001 .label = label,
4002 .fh = fhandle,
4003 };
4004 struct rpc_message msg = {
4005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4006 .rpc_argp = &args,
4007 .rpc_resp = &res,
4008 };
4009
4010 args.bitmask = nfs4_bitmask(server, label);
4011
4012 nfs_fattr_init(fattr);
4013
4014 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4015 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4016 &res.seq_res, 0);
4017 dprintk("NFS reply lookupp: %d\n", status);
4018 return status;
4019}
4020
4021static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4022 struct nfs_fattr *fattr, struct nfs4_label *label)
4023{
4024 struct nfs4_exception exception = { };
4025 int err;
4026 do {
4027 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4028 trace_nfs4_lookupp(inode, err);
4029 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4030 &exception);
4031 } while (exception.retry);
4032 return err;
4033}
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4036{
Trond Myklebust76b32992007-08-10 17:45:11 -04004037 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 struct nfs4_accessargs args = {
4039 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004040 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004041 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004043 struct nfs4_accessres res = {
4044 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004045 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 struct rpc_message msg = {
4047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4048 .rpc_argp = &args,
4049 .rpc_resp = &res,
4050 .rpc_cred = entry->cred,
4051 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004052 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Trond Myklebustc407d412010-04-16 16:22:48 -04004054 res.fattr = nfs_alloc_fattr();
4055 if (res.fattr == NULL)
4056 return -ENOMEM;
4057
Bryan Schumaker7c513052011-03-24 17:12:24 +00004058 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004060 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004061 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004063 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return status;
4065}
4066
4067static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4068{
4069 struct nfs4_exception exception = { };
4070 int err;
4071 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004072 err = _nfs4_proc_access(inode, entry);
4073 trace_nfs4_access(inode, err);
4074 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 &exception);
4076 } while (exception.retry);
4077 return err;
4078}
4079
4080/*
4081 * TODO: For the time being, we don't try to get any attributes
4082 * along with any of the zero-copy operations READ, READDIR,
4083 * READLINK, WRITE.
4084 *
4085 * In the case of the first three, we want to put the GETATTR
4086 * after the read-type operation -- this is because it is hard
4087 * to predict the length of a GETATTR response in v4, and thus
4088 * align the READ data correctly. This means that the GETATTR
4089 * may end up partially falling into the page cache, and we should
4090 * shift it into the 'tail' of the xdr_buf before processing.
4091 * To do this efficiently, we need to know the total length
4092 * of data received, which doesn't seem to be available outside
4093 * of the RPC layer.
4094 *
4095 * In the case of WRITE, we also want to put the GETATTR after
4096 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004097 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 *
4099 * Both of these changes to the XDR layer would in fact be quite
4100 * minor, but I decided to leave them for a subsequent patch.
4101 */
4102static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4103 unsigned int pgbase, unsigned int pglen)
4104{
4105 struct nfs4_readlink args = {
4106 .fh = NFS_FH(inode),
4107 .pgbase = pgbase,
4108 .pglen = pglen,
4109 .pages = &page,
4110 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004111 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 struct rpc_message msg = {
4113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4114 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004115 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 };
4117
Bryan Schumaker7c513052011-03-24 17:12:24 +00004118 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 -07004119}
4120
4121static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4122 unsigned int pgbase, unsigned int pglen)
4123{
4124 struct nfs4_exception exception = { };
4125 int err;
4126 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004127 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4128 trace_nfs4_readlink(inode, err);
4129 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 &exception);
4131 } while (exception.retry);
4132 return err;
4133}
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004136 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138static int
4139nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004140 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004142 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004143 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004144 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 int status = 0;
4147
NeilBrown532d4de2016-10-13 15:26:47 +11004148 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004149 if (IS_ERR(ctx))
4150 return PTR_ERR(ctx);
4151
David Quigleyaa9c2662013-05-22 12:50:44 -04004152 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4153
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004154 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4155 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004156 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 if (IS_ERR(state)) {
4158 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004162 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004163 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 return status;
4165}
4166
Al Virobeffb8f2016-07-20 16:34:42 -04004167static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Trond Myklebust16e42952005-10-27 22:12:44 -04004169 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004170 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004172 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004174 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004175 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4179 .rpc_argp = &args,
4180 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004182 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004183 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004184
Bryan Schumaker7c513052011-03-24 17:12:24 +00004185 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004186 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004187 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return status;
4189}
4190
Al Virobeffb8f2016-07-20 16:34:42 -04004191static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 struct nfs4_exception exception = { };
4194 int err;
4195 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004196 err = _nfs4_proc_remove(dir, name);
4197 trace_nfs4_remove(dir, name, err);
4198 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 &exception);
4200 } while (exception.retry);
4201 return err;
4202}
4203
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004204static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004206 struct nfs_server *server = NFS_SERVER(dir);
4207 struct nfs_removeargs *args = msg->rpc_argp;
4208 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004210 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004212 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004213
4214 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004217static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4218{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004219 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004220 &data->args.seq_args,
4221 &data->res.seq_res,
4222 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
4224
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004225static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004227 struct nfs_unlinkdata *data = task->tk_calldata;
4228 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004229
Trond Myklebust14516c32010-07-31 14:29:06 -04004230 if (!nfs4_sequence_done(task, &res->seq_res))
4231 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004232 if (nfs4_async_handle_error(task, res->server, NULL,
4233 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004234 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004235 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004236 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004237 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Jeff Laytond3d41522010-09-17 17:31:57 -04004240static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4241{
4242 struct nfs_server *server = NFS_SERVER(dir);
4243 struct nfs_renameargs *arg = msg->rpc_argp;
4244 struct nfs_renameres *res = msg->rpc_resp;
4245
4246 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004247 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004248 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004249}
4250
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004251static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4252{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004253 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004254 &data->args.seq_args,
4255 &data->res.seq_res,
4256 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004257}
4258
4259static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4260 struct inode *new_dir)
4261{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004262 struct nfs_renamedata *data = task->tk_calldata;
4263 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004264
4265 if (!nfs4_sequence_done(task, &res->seq_res))
4266 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004267 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004268 return 0;
4269
Trond Myklebustc733c492017-01-11 12:32:26 -05004270 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004271 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004272 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004273 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004274 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004275 return 1;
4276}
4277
Al Virobeffb8f2016-07-20 16:34:42 -04004278static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004280 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 struct nfs4_link_arg arg = {
4282 .fh = NFS_FH(inode),
4283 .dir_fh = NFS_FH(dir),
4284 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004285 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004287 struct nfs4_link_res res = {
4288 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004289 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004290 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 struct rpc_message msg = {
4292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4293 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004294 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004296 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Trond Myklebust136f2622010-04-16 16:22:49 -04004298 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004299 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004300 goto out;
4301
David Quigley14c43f72013-05-22 12:50:43 -04004302 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4303 if (IS_ERR(res.label)) {
4304 status = PTR_ERR(res.label);
4305 goto out;
4306 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004307 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004308
Bryan Schumaker7c513052011-03-24 17:12:24 +00004309 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004310 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004311 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004312 status = nfs_post_op_update_inode(inode, res.fattr);
4313 if (!status)
4314 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004315 }
David Quigley14c43f72013-05-22 12:50:43 -04004316
4317
4318 nfs4_label_free(res.label);
4319
Trond Myklebust136f2622010-04-16 16:22:49 -04004320out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004321 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return status;
4323}
4324
Al Virobeffb8f2016-07-20 16:34:42 -04004325static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 struct nfs4_exception exception = { };
4328 int err;
4329 do {
4330 err = nfs4_handle_exception(NFS_SERVER(inode),
4331 _nfs4_proc_link(inode, dir, name),
4332 &exception);
4333 } while (exception.retry);
4334 return err;
4335}
4336
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004337struct nfs4_createdata {
4338 struct rpc_message msg;
4339 struct nfs4_create_arg arg;
4340 struct nfs4_create_res res;
4341 struct nfs_fh fh;
4342 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004343 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004344};
4345
4346static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004347 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004348{
4349 struct nfs4_createdata *data;
4350
4351 data = kzalloc(sizeof(*data), GFP_KERNEL);
4352 if (data != NULL) {
4353 struct nfs_server *server = NFS_SERVER(dir);
4354
David Quigley14c43f72013-05-22 12:50:43 -04004355 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4356 if (IS_ERR(data->label))
4357 goto out_free;
4358
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004359 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4360 data->msg.rpc_argp = &data->arg;
4361 data->msg.rpc_resp = &data->res;
4362 data->arg.dir_fh = NFS_FH(dir);
4363 data->arg.server = server;
4364 data->arg.name = name;
4365 data->arg.attrs = sattr;
4366 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004367 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004368 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004369 data->res.server = server;
4370 data->res.fh = &data->fh;
4371 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004372 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004373 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004374 }
4375 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004376out_free:
4377 kfree(data);
4378 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004379}
4380
4381static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4382{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004383 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004384 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004385 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004386 update_changeattr(dir, &data->res.dir_cinfo,
4387 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004388 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004389 }
4390 return status;
4391}
4392
4393static void nfs4_free_createdata(struct nfs4_createdata *data)
4394{
David Quigley14c43f72013-05-22 12:50:43 -04004395 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 kfree(data);
4397}
4398
Chuck Lever4f390c12006-08-22 20:06:22 -04004399static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004400 struct page *page, unsigned int len, struct iattr *sattr,
4401 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004403 struct nfs4_createdata *data;
4404 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Chuck Lever94a6d752006-08-22 20:06:23 -04004406 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004407 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004408
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004409 status = -ENOMEM;
4410 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4411 if (data == NULL)
4412 goto out;
4413
4414 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4415 data->arg.u.symlink.pages = &page;
4416 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004417 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419 status = nfs4_do_create(dir, dentry, data);
4420
4421 nfs4_free_createdata(data);
4422out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return status;
4424}
4425
Chuck Lever4f390c12006-08-22 20:06:22 -04004426static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004427 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
4429 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004430 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004432
4433 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004436 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4437 trace_nfs4_symlink(dir, &dentry->d_name, err);
4438 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 &exception);
4440 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004441
4442 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 return err;
4444}
4445
4446static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004447 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449 struct nfs4_createdata *data;
4450 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004452 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4453 if (data == NULL)
4454 goto out;
4455
David Quigley1775fd32013-05-22 12:50:42 -04004456 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004457 status = nfs4_do_create(dir, dentry, data);
4458
4459 nfs4_free_createdata(data);
4460out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return status;
4462}
4463
4464static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4465 struct iattr *sattr)
4466{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004467 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004469 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004471
David Quigleyaa9c2662013-05-22 12:50:44 -04004472 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4473
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004474 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4475 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004477 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4478 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4479 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 &exception);
4481 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004482 nfs4_label_release_security(label);
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return err;
4485}
4486
4487static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004488 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
David Howells2b0143b2015-03-17 22:25:59 +00004490 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 struct nfs4_readdir_arg args = {
4492 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004493 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 .pgbase = 0,
4495 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004496 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004497 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 };
4499 struct nfs4_readdir_res res;
4500 struct rpc_message msg = {
4501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4502 .rpc_argp = &args,
4503 .rpc_resp = &res,
4504 .rpc_cred = cred,
4505 };
4506 int status;
4507
Al Viro6de14722013-09-16 10:53:17 -04004508 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4509 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004510 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004511 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004513 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 -05004514 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004515 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004516 status += args.pgbase;
4517 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004518
4519 nfs_invalidate_atime(dir);
4520
Harvey Harrison3110ff82008-05-02 13:42:44 -07004521 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 return status;
4523}
4524
4525static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004526 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
4528 struct nfs4_exception exception = { };
4529 int err;
4530 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004531 err = _nfs4_proc_readdir(dentry, cred, cookie,
4532 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004533 trace_nfs4_readdir(d_inode(dentry), err);
4534 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 &exception);
4536 } while (exception.retry);
4537 return err;
4538}
4539
4540static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004541 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004543 struct nfs4_createdata *data;
4544 int mode = sattr->ia_mode;
4545 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004547 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4548 if (data == NULL)
4549 goto out;
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004554 data->arg.ftype = NF4BLK;
4555 data->arg.u.device.specdata1 = MAJOR(rdev);
4556 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
4558 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004559 data->arg.ftype = NF4CHR;
4560 data->arg.u.device.specdata1 = MAJOR(rdev);
4561 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004562 } else if (!S_ISSOCK(mode)) {
4563 status = -EINVAL;
4564 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 }
David Quigley1775fd32013-05-22 12:50:42 -04004566
David Quigleyaa9c2662013-05-22 12:50:44 -04004567 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004568 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004569out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004570 nfs4_free_createdata(data);
4571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return status;
4573}
4574
4575static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4576 struct iattr *sattr, dev_t rdev)
4577{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004578 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004580 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004582
David Quigleyaa9c2662013-05-22 12:50:44 -04004583 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4584
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004585 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4586 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004588 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4589 trace_nfs4_mknod(dir, &dentry->d_name, err);
4590 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 &exception);
4592 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004593
4594 nfs4_label_release_security(label);
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 return err;
4597}
4598
4599static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4600 struct nfs_fsstat *fsstat)
4601{
4602 struct nfs4_statfs_arg args = {
4603 .fh = fhandle,
4604 .bitmask = server->attr_bitmask,
4605 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004606 struct nfs4_statfs_res res = {
4607 .fsstat = fsstat,
4608 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 struct rpc_message msg = {
4610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4611 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004612 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 };
4614
Trond Myklebust0e574af2005-10-27 22:12:38 -04004615 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004616 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
4619static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4620{
4621 struct nfs4_exception exception = { };
4622 int err;
4623 do {
4624 err = nfs4_handle_exception(server,
4625 _nfs4_proc_statfs(server, fhandle, fsstat),
4626 &exception);
4627 } while (exception.retry);
4628 return err;
4629}
4630
4631static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4632 struct nfs_fsinfo *fsinfo)
4633{
4634 struct nfs4_fsinfo_arg args = {
4635 .fh = fhandle,
4636 .bitmask = server->attr_bitmask,
4637 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004638 struct nfs4_fsinfo_res res = {
4639 .fsinfo = fsinfo,
4640 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 struct rpc_message msg = {
4642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4643 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004644 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 };
4646
Bryan Schumaker7c513052011-03-24 17:12:24 +00004647 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648}
4649
4650static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4651{
4652 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004653 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 int err;
4655
4656 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004657 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004658 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004659 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004660 nfs4_set_lease_period(server->nfs_client,
4661 fsinfo->lease_time * HZ,
4662 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004663 break;
4664 }
4665 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 } while (exception.retry);
4667 return err;
4668}
4669
4670static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4671{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004672 int error;
4673
Trond Myklebust0e574af2005-10-27 22:12:38 -04004674 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004675 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004676 if (error == 0) {
4677 /* block layout checks this! */
4678 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004679 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004680 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004681
4682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
4685static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4686 struct nfs_pathconf *pathconf)
4687{
4688 struct nfs4_pathconf_arg args = {
4689 .fh = fhandle,
4690 .bitmask = server->attr_bitmask,
4691 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004692 struct nfs4_pathconf_res res = {
4693 .pathconf = pathconf,
4694 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 struct rpc_message msg = {
4696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4697 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004698 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 };
4700
4701 /* None of the pathconf attributes are mandatory to implement */
4702 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4703 memset(pathconf, 0, sizeof(*pathconf));
4704 return 0;
4705 }
4706
Trond Myklebust0e574af2005-10-27 22:12:38 -04004707 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004708 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
4710
4711static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4712 struct nfs_pathconf *pathconf)
4713{
4714 struct nfs4_exception exception = { };
4715 int err;
4716
4717 do {
4718 err = nfs4_handle_exception(server,
4719 _nfs4_proc_pathconf(server, fhandle, pathconf),
4720 &exception);
4721 } while (exception.retry);
4722 return err;
4723}
4724
Trond Myklebust5521abf2013-03-16 20:54:34 -04004725int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004726 const struct nfs_open_context *ctx,
4727 const struct nfs_lock_context *l_ctx,
4728 fmode_t fmode)
4729{
NeilBrown17393472016-10-13 15:26:47 +11004730 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004731}
4732EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4733
Trond Myklebust5521abf2013-03-16 20:54:34 -04004734static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4735 const struct nfs_open_context *ctx,
4736 const struct nfs_lock_context *l_ctx,
4737 fmode_t fmode)
4738{
4739 nfs4_stateid current_stateid;
4740
Trond Myklebuste1253be2014-03-05 08:44:23 -05004741 /* If the current stateid represents a lost lock, then exit */
4742 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4743 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004744 return nfs4_stateid_match(stateid, &current_stateid);
4745}
4746
4747static bool nfs4_error_stateid_expired(int err)
4748{
4749 switch (err) {
4750 case -NFS4ERR_DELEG_REVOKED:
4751 case -NFS4ERR_ADMIN_REVOKED:
4752 case -NFS4ERR_BAD_STATEID:
4753 case -NFS4ERR_STALE_STATEID:
4754 case -NFS4ERR_OLD_STATEID:
4755 case -NFS4ERR_OPENMODE:
4756 case -NFS4ERR_EXPIRED:
4757 return true;
4758 }
4759 return false;
4760}
4761
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004766 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004767 if (task->tk_status < 0) {
4768 struct nfs4_exception exception = {
4769 .inode = hdr->inode,
4770 .state = hdr->args.context->state,
4771 .stateid = &hdr->args.stateid,
4772 };
4773 task->tk_status = nfs4_async_handle_exception(task,
4774 server, task->tk_status, &exception);
4775 if (exception.retry) {
4776 rpc_restart_call_prepare(task);
4777 return -EAGAIN;
4778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004782 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
Trond Myklebust5521abf2013-03-16 20:54:34 -04004786static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004787 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004788{
4789
4790 if (!nfs4_error_stateid_expired(task->tk_status) ||
4791 nfs4_stateid_is_current(&args->stateid,
4792 args->context,
4793 args->lock_context,
4794 FMODE_READ))
4795 return false;
4796 rpc_restart_call_prepare(task);
4797 return true;
4798}
4799
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004800static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004801{
4802
4803 dprintk("--> %s\n", __func__);
4804
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004805 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004806 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004807 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004808 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004809 if (task->tk_status > 0)
4810 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004811 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4812 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004813}
4814
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004815static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4816 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004818 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004819 if (!hdr->pgio_done_cb)
4820 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004821 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004822 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004825static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4826 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004827{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004828 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004829 &hdr->args.seq_args,
4830 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004831 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004832 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004833 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4834 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004835 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004836 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004837 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004838 return -EIO;
4839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004842static int nfs4_write_done_cb(struct rpc_task *task,
4843 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004845 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004846
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004847 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004848 if (task->tk_status < 0) {
4849 struct nfs4_exception exception = {
4850 .inode = hdr->inode,
4851 .state = hdr->args.context->state,
4852 .stateid = &hdr->args.stateid,
4853 };
4854 task->tk_status = nfs4_async_handle_exception(task,
4855 NFS_SERVER(inode), task->tk_status,
4856 &exception);
4857 if (exception.retry) {
4858 rpc_restart_call_prepare(task);
4859 return -EAGAIN;
4860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004862 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004863 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004864 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004865 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867}
4868
Trond Myklebust5521abf2013-03-16 20:54:34 -04004869static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004870 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004871{
4872
4873 if (!nfs4_error_stateid_expired(task->tk_status) ||
4874 nfs4_stateid_is_current(&args->stateid,
4875 args->context,
4876 args->lock_context,
4877 FMODE_WRITE))
4878 return false;
4879 rpc_restart_call_prepare(task);
4880 return true;
4881}
4882
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004884{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004885 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004886 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004888 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004889 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4890 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004891}
4892
Trond Myklebust5a37f852012-04-28 14:55:16 -04004893static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004894bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004895{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004896 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004897 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004898 return false;
4899 /* Otherwise, request attributes if and only if we don't hold
4900 * a delegation
4901 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004902 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004903}
Fred Isamana69aef12011-03-03 15:13:47 +00004904
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004905static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4906 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004909
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004910 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4911 hdr->args.bitmask = NULL;
4912 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004913 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004914 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004915
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004916 if (!hdr->pgio_done_cb)
4917 hdr->pgio_done_cb = nfs4_write_done_cb;
4918 hdr->res.server = server;
4919 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004921 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004922 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923}
4924
Fred Isaman0b7c0152012-04-20 14:47:39 -04004925static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4926{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004927 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004928 &data->args.seq_args,
4929 &data->res.seq_res,
4930 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
Fred Isaman0b7c0152012-04-20 14:47:39 -04004933static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004936
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004937 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004938 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4939 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004940 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004941 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
Fred Isaman0b7c0152012-04-20 14:47:39 -04004946static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004947{
4948 if (!nfs4_sequence_done(task, &data->res.seq_res))
4949 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004950 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004951}
4952
Fred Isaman0b7c0152012-04-20 14:47:39 -04004953static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004955 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004956
Fred Isaman0b7c0152012-04-20 14:47:39 -04004957 if (data->commit_done_cb == NULL)
4958 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004959 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004960 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004961 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962}
4963
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004964struct nfs4_renewdata {
4965 struct nfs_client *client;
4966 unsigned long timestamp;
4967};
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969/*
4970 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4971 * standalone procedure for queueing an asynchronous RENEW.
4972 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004973static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004974{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004975 struct nfs4_renewdata *data = calldata;
4976 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004977
Elena Reshetova212bf412017-10-20 12:53:38 +03004978 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004979 nfs4_schedule_state_renewal(clp);
4980 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004981 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004982}
4983
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004984static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004986 struct nfs4_renewdata *data = calldata;
4987 struct nfs_client *clp = data->client;
4988 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004990 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004991 switch (task->tk_status) {
4992 case 0:
4993 break;
4994 case -NFS4ERR_LEASE_MOVED:
4995 nfs4_schedule_lease_moved_recovery(clp);
4996 break;
4997 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004998 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004999 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5000 return;
5001 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005002 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005003 return;
5004 }
5005 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005007 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008}
5009
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005010static const struct rpc_call_ops nfs4_renew_ops = {
5011 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005012 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005013};
5014
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005015static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
5017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5019 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005020 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005022 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005024 if (renew_flags == 0)
5025 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005026 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005027 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005028 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005029 if (data == NULL) {
5030 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005031 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005032 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005033 data->client = clp;
5034 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005035 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005036 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005039static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
5041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5043 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005044 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 };
5046 unsigned long now = jiffies;
5047 int status;
5048
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005049 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 if (status < 0)
5051 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005052 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 return 0;
5054}
5055
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005056static inline int nfs4_server_supports_acls(struct nfs_server *server)
5057{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005058 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005059}
5060
Trond Myklebust21f498c2012-08-24 10:59:25 -04005061/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5062 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005063 * the stack.
5064 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005065#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005066
Neil Hormane9e3d722011-03-04 19:26:03 -05005067static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005068 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005069{
5070 struct page *newpage, **spages;
5071 int rc = 0;
5072 size_t len;
5073 spages = pages;
5074
5075 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005076 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005077 newpage = alloc_page(GFP_KERNEL);
5078
5079 if (newpage == NULL)
5080 goto unwind;
5081 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005082 buf += len;
5083 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005084 *pages++ = newpage;
5085 rc++;
5086 } while (buflen != 0);
5087
5088 return rc;
5089
5090unwind:
5091 for(; rc > 0; rc--)
5092 __free_page(spages[rc-1]);
5093 return -ENOMEM;
5094}
5095
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005096struct nfs4_cached_acl {
5097 int cached;
5098 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005099 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005100};
5101
5102static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005103{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005104 struct nfs_inode *nfsi = NFS_I(inode);
5105
5106 spin_lock(&inode->i_lock);
5107 kfree(nfsi->nfs4_acl);
5108 nfsi->nfs4_acl = acl;
5109 spin_unlock(&inode->i_lock);
5110}
5111
5112static void nfs4_zap_acl_attr(struct inode *inode)
5113{
5114 nfs4_set_cached_acl(inode, NULL);
5115}
5116
5117static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5118{
5119 struct nfs_inode *nfsi = NFS_I(inode);
5120 struct nfs4_cached_acl *acl;
5121 int ret = -ENOENT;
5122
5123 spin_lock(&inode->i_lock);
5124 acl = nfsi->nfs4_acl;
5125 if (acl == NULL)
5126 goto out;
5127 if (buf == NULL) /* user is just asking for length */
5128 goto out_len;
5129 if (acl->cached == 0)
5130 goto out;
5131 ret = -ERANGE; /* see getxattr(2) man page */
5132 if (acl->len > buflen)
5133 goto out;
5134 memcpy(buf, acl->data, acl->len);
5135out_len:
5136 ret = acl->len;
5137out:
5138 spin_unlock(&inode->i_lock);
5139 return ret;
5140}
5141
Sachin Prabhu5794d212012-04-17 14:36:40 +01005142static 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 +00005143{
5144 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005145 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005146
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005147 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005148 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005149 if (acl == NULL)
5150 goto out;
5151 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005152 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005153 } else {
5154 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5155 if (acl == NULL)
5156 goto out;
5157 acl->cached = 0;
5158 }
5159 acl->len = acl_len;
5160out:
5161 nfs4_set_cached_acl(inode, acl);
5162}
5163
Andy Adamsonbf118a32011-12-07 11:55:27 -05005164/*
5165 * The getxattr API returns the required buffer length when called with a
5166 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5167 * the required buf. On a NULL buf, we send a page of data to the server
5168 * guessing that the ACL request can be serviced by a page. If so, we cache
5169 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5170 * the cache. If not so, we throw away the page, and cache the required
5171 * length. The next getxattr call will then produce another round trip to
5172 * the server, this time with the input buf of the required size.
5173 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005174static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005175{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005176 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005177 struct nfs_getaclargs args = {
5178 .fh = NFS_FH(inode),
5179 .acl_pages = pages,
5180 .acl_len = buflen,
5181 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005182 struct nfs_getaclres res = {
5183 .acl_len = buflen,
5184 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005185 struct rpc_message msg = {
5186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5187 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005188 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005189 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005190 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005191 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005192
Trond Myklebust21f498c2012-08-24 10:59:25 -04005193 if (npages > ARRAY_SIZE(pages))
5194 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005195
Andy Adamsonbf118a32011-12-07 11:55:27 -05005196 for (i = 0; i < npages; i++) {
5197 pages[i] = alloc_page(GFP_KERNEL);
5198 if (!pages[i])
5199 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005200 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005201
5202 /* for decoding across pages */
5203 res.acl_scratch = alloc_page(GFP_KERNEL);
5204 if (!res.acl_scratch)
5205 goto out_free;
5206
Andy Adamsonbf118a32011-12-07 11:55:27 -05005207 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005208
Peng Taode040be2012-01-10 22:42:47 +08005209 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005210 __func__, buf, buflen, npages, args.acl_len);
5211 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5212 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005213 if (ret)
5214 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005215
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005216 /* Handle the case where the passed-in buffer is too short */
5217 if (res.acl_flags & NFS4_ACL_TRUNC) {
5218 /* Did the user only issue a request for the acl length? */
5219 if (buf == NULL)
5220 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005221 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005222 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005223 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005224 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005225 if (buf) {
5226 if (res.acl_len > buflen) {
5227 ret = -ERANGE;
5228 goto out_free;
5229 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005230 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005231 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005232out_ok:
5233 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005234out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005235 for (i = 0; i < npages; i++)
5236 if (pages[i])
5237 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005238 if (res.acl_scratch)
5239 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005240 return ret;
5241}
5242
Trond Myklebust16b42892006-08-24 12:27:15 -04005243static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5244{
5245 struct nfs4_exception exception = { };
5246 ssize_t ret;
5247 do {
5248 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005249 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005250 if (ret >= 0)
5251 break;
5252 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5253 } while (exception.retry);
5254 return ret;
5255}
5256
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005257static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5258{
5259 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005260 int ret;
5261
5262 if (!nfs4_server_supports_acls(server))
5263 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005264 ret = nfs_revalidate_inode(server, inode);
5265 if (ret < 0)
5266 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005267 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5268 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005269 ret = nfs4_read_cached_acl(inode, buf, buflen);
5270 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005271 /* -ENOENT is returned if there is no ACL or if there is an ACL
5272 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005273 return ret;
5274 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005275}
5276
Trond Myklebust16b42892006-08-24 12:27:15 -04005277static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005278{
5279 struct nfs_server *server = NFS_SERVER(inode);
5280 struct page *pages[NFS4ACL_MAXPAGES];
5281 struct nfs_setaclargs arg = {
5282 .fh = NFS_FH(inode),
5283 .acl_pages = pages,
5284 .acl_len = buflen,
5285 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005286 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5289 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005290 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005291 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005292 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005293 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005294
5295 if (!nfs4_server_supports_acls(server))
5296 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005297 if (npages > ARRAY_SIZE(pages))
5298 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005299 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005300 if (i < 0)
5301 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005302 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005303 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005304
5305 /*
5306 * Free each page after tx, so the only ref left is
5307 * held by the network stack
5308 */
5309 for (; i > 0; i--)
5310 put_page(pages[i-1]);
5311
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005312 /*
5313 * Acl update can result in inode attribute update.
5314 * so mark the attribute cache invalid.
5315 */
5316 spin_lock(&inode->i_lock);
5317 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5318 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005319 nfs_access_zap_cache(inode);
5320 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005321 return ret;
5322}
5323
Trond Myklebust16b42892006-08-24 12:27:15 -04005324static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5325{
5326 struct nfs4_exception exception = { };
5327 int err;
5328 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005329 err = __nfs4_proc_set_acl(inode, buf, buflen);
5330 trace_nfs4_set_acl(inode, err);
5331 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005332 &exception);
5333 } while (exception.retry);
5334 return err;
5335}
5336
David Quigleyaa9c2662013-05-22 12:50:44 -04005337#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5338static int _nfs4_get_security_label(struct inode *inode, void *buf,
5339 size_t buflen)
5340{
5341 struct nfs_server *server = NFS_SERVER(inode);
5342 struct nfs_fattr fattr;
5343 struct nfs4_label label = {0, 0, buflen, buf};
5344
5345 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005346 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005347 .fh = NFS_FH(inode),
5348 .bitmask = bitmask,
5349 };
5350 struct nfs4_getattr_res res = {
5351 .fattr = &fattr,
5352 .label = &label,
5353 .server = server,
5354 };
5355 struct rpc_message msg = {
5356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005357 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005358 .rpc_resp = &res,
5359 };
5360 int ret;
5361
5362 nfs_fattr_init(&fattr);
5363
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005364 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005365 if (ret)
5366 return ret;
5367 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5368 return -ENOENT;
5369 if (buflen < label.len)
5370 return -ERANGE;
5371 return 0;
5372}
5373
5374static int nfs4_get_security_label(struct inode *inode, void *buf,
5375 size_t buflen)
5376{
5377 struct nfs4_exception exception = { };
5378 int err;
5379
5380 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5381 return -EOPNOTSUPP;
5382
5383 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005384 err = _nfs4_get_security_label(inode, buf, buflen);
5385 trace_nfs4_get_security_label(inode, err);
5386 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005387 &exception);
5388 } while (exception.retry);
5389 return err;
5390}
5391
5392static int _nfs4_do_set_security_label(struct inode *inode,
5393 struct nfs4_label *ilabel,
5394 struct nfs_fattr *fattr,
5395 struct nfs4_label *olabel)
5396{
5397
5398 struct iattr sattr = {0};
5399 struct nfs_server *server = NFS_SERVER(inode);
5400 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005401 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005402 .fh = NFS_FH(inode),
5403 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005404 .server = server,
5405 .bitmask = bitmask,
5406 .label = ilabel,
5407 };
5408 struct nfs_setattrres res = {
5409 .fattr = fattr,
5410 .label = olabel,
5411 .server = server,
5412 };
5413 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5415 .rpc_argp = &arg,
5416 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005417 };
5418 int status;
5419
Jeff Layton12207f62013-11-01 10:49:32 -04005420 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005421
Jeff Layton12207f62013-11-01 10:49:32 -04005422 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005423 if (status)
5424 dprintk("%s failed: %d\n", __func__, status);
5425
5426 return status;
5427}
5428
5429static int nfs4_do_set_security_label(struct inode *inode,
5430 struct nfs4_label *ilabel,
5431 struct nfs_fattr *fattr,
5432 struct nfs4_label *olabel)
5433{
5434 struct nfs4_exception exception = { };
5435 int err;
5436
5437 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005438 err = _nfs4_do_set_security_label(inode, ilabel,
5439 fattr, olabel);
5440 trace_nfs4_set_security_label(inode, err);
5441 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005442 &exception);
5443 } while (exception.retry);
5444 return err;
5445}
5446
5447static int
Al Viro59301222016-05-27 10:19:30 -04005448nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005449{
5450 struct nfs4_label ilabel, *olabel = NULL;
5451 struct nfs_fattr fattr;
5452 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005453 int status;
5454
5455 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5456 return -EOPNOTSUPP;
5457
5458 nfs_fattr_init(&fattr);
5459
5460 ilabel.pi = 0;
5461 ilabel.lfs = 0;
5462 ilabel.label = (char *)buf;
5463 ilabel.len = buflen;
5464
5465 cred = rpc_lookup_cred();
5466 if (IS_ERR(cred))
5467 return PTR_ERR(cred);
5468
5469 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5470 if (IS_ERR(olabel)) {
5471 status = -PTR_ERR(olabel);
5472 goto out;
5473 }
5474
5475 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5476 if (status == 0)
5477 nfs_setsecurity(inode, &fattr, olabel);
5478
5479 nfs4_label_free(olabel);
5480out:
5481 put_rpccred(cred);
5482 return status;
5483}
5484#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5485
5486
Chuck Leverf0920752012-05-21 22:45:41 -04005487static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5488 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005489{
5490 __be32 verf[2];
5491
Chuck Lever2c820d92012-05-21 22:45:33 -04005492 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5493 /* An impossible timestamp guarantees this value
5494 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005495 verf[0] = cpu_to_be32(U32_MAX);
5496 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005497 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005498 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005499 u64 ns = ktime_to_ns(nn->boot_time);
5500
5501 verf[0] = cpu_to_be32(ns >> 32);
5502 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005503 }
Chuck Levercd937102012-03-02 17:14:31 -05005504 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5505}
5506
Jeff Laytona3192682015-06-09 19:43:59 -04005507static int
5508nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005509{
Jeff Laytona3192682015-06-09 19:43:59 -04005510 size_t len;
5511 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005512
Trond Myklebustceb3a162015-01-03 15:16:04 -05005513 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005514 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005515
Jeff Laytona3192682015-06-09 19:43:59 -04005516 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005517 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005518 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5519 1 +
5520 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5521 1;
5522 rcu_read_unlock();
5523
5524 if (len > NFS4_OPAQUE_LIMIT + 1)
5525 return -EINVAL;
5526
5527 /*
5528 * Since this string is allocated at mount time, and held until the
5529 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5530 * about a memory-reclaim deadlock.
5531 */
5532 str = kmalloc(len, GFP_KERNEL);
5533 if (!str)
5534 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005535
Chuck Levere984a552012-09-14 17:24:21 -04005536 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005537 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005538 clp->cl_ipaddr,
5539 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5540 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005541 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005542
Jeff Laytona3192682015-06-09 19:43:59 -04005543 clp->cl_owner_id = str;
5544 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005545}
5546
Jeff Layton873e3852015-06-09 19:44:00 -04005547static int
5548nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005549{
Jeff Layton873e3852015-06-09 19:44:00 -04005550 size_t len;
5551 char *str;
5552
5553 len = 10 + 10 + 1 + 10 + 1 +
5554 strlen(nfs4_client_id_uniquifier) + 1 +
5555 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5556
5557 if (len > NFS4_OPAQUE_LIMIT + 1)
5558 return -EINVAL;
5559
5560 /*
5561 * Since this string is allocated at mount time, and held until the
5562 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5563 * about a memory-reclaim deadlock.
5564 */
5565 str = kmalloc(len, GFP_KERNEL);
5566 if (!str)
5567 return -ENOMEM;
5568
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005569 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005570 clp->rpc_ops->version, clp->cl_minorversion,
5571 nfs4_client_id_uniquifier,
5572 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005573 clp->cl_owner_id = str;
5574 return 0;
5575}
5576
5577static int
5578nfs4_init_uniform_client_string(struct nfs_client *clp)
5579{
Jeff Layton873e3852015-06-09 19:44:00 -04005580 size_t len;
5581 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005582
5583 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005584 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005585
5586 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005587 return nfs4_init_uniquifier_client_string(clp);
5588
5589 len = 10 + 10 + 1 + 10 + 1 +
5590 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5591
5592 if (len > NFS4_OPAQUE_LIMIT + 1)
5593 return -EINVAL;
5594
5595 /*
5596 * Since this string is allocated at mount time, and held until the
5597 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5598 * about a memory-reclaim deadlock.
5599 */
5600 str = kmalloc(len, GFP_KERNEL);
5601 if (!str)
5602 return -ENOMEM;
5603
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005604 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005605 clp->rpc_ops->version, clp->cl_minorversion,
5606 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005607 clp->cl_owner_id = str;
5608 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005609}
5610
Chuck Lever706cb8d2014-03-12 12:51:47 -04005611/*
5612 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5613 * services. Advertise one based on the address family of the
5614 * clientaddr.
5615 */
5616static unsigned int
5617nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5618{
5619 if (strchr(clp->cl_ipaddr, ':') != NULL)
5620 return scnprintf(buf, len, "tcp6");
5621 else
5622 return scnprintf(buf, len, "tcp");
5623}
5624
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005625static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5626{
5627 struct nfs4_setclientid *sc = calldata;
5628
5629 if (task->tk_status == 0)
5630 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5631}
5632
5633static const struct rpc_call_ops nfs4_setclientid_ops = {
5634 .rpc_call_done = nfs4_setclientid_done,
5635};
5636
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005637/**
5638 * nfs4_proc_setclientid - Negotiate client ID
5639 * @clp: state data structure
5640 * @program: RPC program for NFSv4 callback service
5641 * @port: IP port number for NFS4 callback service
5642 * @cred: RPC credential to use for this call
5643 * @res: where to place the result
5644 *
5645 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5646 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005647int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5648 unsigned short port, struct rpc_cred *cred,
5649 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650{
5651 nfs4_verifier sc_verifier;
5652 struct nfs4_setclientid setclientid = {
5653 .sc_verifier = &sc_verifier,
5654 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005655 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 };
5657 struct rpc_message msg = {
5658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5659 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005660 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005661 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005663 struct rpc_task *task;
5664 struct rpc_task_setup task_setup_data = {
5665 .rpc_client = clp->cl_rpcclient,
5666 .rpc_message = &msg,
5667 .callback_ops = &nfs4_setclientid_ops,
5668 .callback_data = &setclientid,
5669 .flags = RPC_TASK_TIMEOUT,
5670 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005671 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Chuck Leverde734832012-07-11 16:30:50 -04005673 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005674 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005675
5676 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5677 status = nfs4_init_uniform_client_string(clp);
5678 else
Jeff Laytona3192682015-06-09 19:43:59 -04005679 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005680
5681 if (status)
5682 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005683
Chuck Leverde734832012-07-11 16:30:50 -04005684 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005685 setclientid.sc_netid_len =
5686 nfs4_init_callback_netid(clp,
5687 setclientid.sc_netid,
5688 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005689 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005690 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 clp->cl_ipaddr, port >> 8, port & 255);
5692
Jeff Layton3a6bb732015-06-09 19:43:57 -04005693 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005694 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005695 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005696 task = rpc_run_task(&task_setup_data);
5697 if (IS_ERR(task)) {
5698 status = PTR_ERR(task);
5699 goto out;
5700 }
5701 status = task->tk_status;
5702 if (setclientid.sc_cred) {
5703 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5704 put_rpccred(setclientid.sc_cred);
5705 }
5706 rpc_put_task(task);
5707out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005708 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005709 dprintk("NFS reply setclientid: %d\n", status);
5710 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711}
5712
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005713/**
5714 * nfs4_proc_setclientid_confirm - Confirm client ID
5715 * @clp: state data structure
5716 * @res: result of a previous SETCLIENTID
5717 * @cred: RPC credential to use for this call
5718 *
5719 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5720 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005721int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005722 struct nfs4_setclientid_res *arg,
5723 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 struct rpc_message msg = {
5726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005727 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005728 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 int status;
5731
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005732 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5733 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5734 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005735 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005736 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005737 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 return status;
5739}
5740
Trond Myklebustfe650402006-01-03 09:55:18 +01005741struct nfs4_delegreturndata {
5742 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005743 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005744 struct nfs_fh fh;
5745 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005746 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005747 struct {
5748 struct nfs4_layoutreturn_args arg;
5749 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005750 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005751 u32 roc_barrier;
5752 bool roc;
5753 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005754 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005755 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005756 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005757};
5758
Trond Myklebustfe650402006-01-03 09:55:18 +01005759static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5760{
5761 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005762
Trond Myklebust14516c32010-07-31 14:29:06 -04005763 if (!nfs4_sequence_done(task, &data->res.seq_res))
5764 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005765
Trond Myklebustca8acf82013-08-13 10:36:56 -04005766 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005767
5768 /* Handle Layoutreturn errors */
5769 if (data->args.lr_args && task->tk_status != 0) {
5770 switch(data->res.lr_ret) {
5771 default:
5772 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5773 break;
5774 case 0:
5775 data->args.lr_args = NULL;
5776 data->res.lr_res = NULL;
5777 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005778 case -NFS4ERR_OLD_STATEID:
5779 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
5780 data->inode)) {
5781 data->res.lr_ret = 0;
5782 rpc_restart_call_prepare(task);
5783 return;
5784 }
5785 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005786 case -NFS4ERR_ADMIN_REVOKED:
5787 case -NFS4ERR_DELEG_REVOKED:
5788 case -NFS4ERR_EXPIRED:
5789 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005790 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5791 case -NFS4ERR_WRONG_CRED:
5792 data->args.lr_args = NULL;
5793 data->res.lr_res = NULL;
5794 data->res.lr_ret = 0;
5795 rpc_restart_call_prepare(task);
5796 return;
5797 }
5798 }
5799
Ricardo Labiaga79708862009-12-07 09:23:21 -05005800 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005801 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005802 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005803 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005804 case -NFS4ERR_ADMIN_REVOKED:
5805 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005806 case -NFS4ERR_EXPIRED:
5807 nfs4_free_revoked_stateid(data->res.server,
5808 data->args.stateid,
5809 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005810 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005811 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005812 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005813 task->tk_status = 0;
5814 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005815 case -NFS4ERR_OLD_STATEID:
5816 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode)) {
5817 task->tk_status = 0;
5818 rpc_restart_call_prepare(task);
5819 return;
5820 }
5821 task->tk_status = 0;
5822 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005823 case -NFS4ERR_ACCESS:
5824 if (data->args.bitmask) {
5825 data->args.bitmask = NULL;
5826 data->res.fattr = NULL;
5827 task->tk_status = 0;
5828 rpc_restart_call_prepare(task);
5829 return;
5830 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005831 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005832 if (nfs4_async_handle_error(task, data->res.server,
5833 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005834 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005835 return;
5836 }
5837 }
5838 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005839}
5840
5841static void nfs4_delegreturn_release(void *calldata)
5842{
Peng Tao039b7562014-07-03 13:05:02 +08005843 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005844 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005845
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005846 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005847 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005848 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5849 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005850 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005851 nfs_iput_and_deactive(inode);
5852 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005853 kfree(calldata);
5854}
5855
Andy Adamson938e1012009-04-01 09:22:28 -04005856static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5857{
5858 struct nfs4_delegreturndata *d_data;
5859
5860 d_data = (struct nfs4_delegreturndata *)data;
5861
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005862 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005863 return;
5864
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005865 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005866 &d_data->args.seq_args,
5867 &d_data->res.seq_res,
5868 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005869}
Andy Adamson938e1012009-04-01 09:22:28 -04005870
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005871static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005872 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005873 .rpc_call_done = nfs4_delegreturn_done,
5874 .rpc_release = nfs4_delegreturn_release,
5875};
5876
Trond Myklebuste6f81072008-01-24 18:14:34 -05005877static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005878{
5879 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005880 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005881 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005882 struct rpc_message msg = {
5883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5884 .rpc_cred = cred,
5885 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005886 struct rpc_task_setup task_setup_data = {
5887 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005888 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005889 .callback_ops = &nfs4_delegreturn_ops,
5890 .flags = RPC_TASK_ASYNC,
5891 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005892 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005893
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005894 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005895 if (data == NULL)
5896 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005897 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005898
5899 nfs4_state_protect(server->nfs_client,
5900 NFS_SP4_MACH_CRED_CLEANUP,
5901 &task_setup_data.rpc_client, &msg);
5902
Trond Myklebustfe650402006-01-03 09:55:18 +01005903 data->args.fhandle = &data->fh;
5904 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005905 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005906 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005907 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005908 data->res.fattr = &data->fattr;
5909 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005910 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005911 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005912 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005913 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005914 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005915 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005916 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005917 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005918 if (data->lr.roc) {
5919 data->args.lr_args = &data->lr.arg;
5920 data->res.lr_res = &data->lr.res;
5921 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005922 } else if (data->lr.roc) {
5923 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5924 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005925 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005926
Trond Myklebustc970aa82007-07-14 15:39:59 -04005927 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005928 msg.rpc_argp = &data->args;
5929 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005930 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005931 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005932 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005933 if (!issync)
5934 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005935 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005936 if (status != 0)
5937 goto out;
5938 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005939out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005940 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005941 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942}
5943
Trond Myklebuste6f81072008-01-24 18:14:34 -05005944int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945{
5946 struct nfs_server *server = NFS_SERVER(inode);
5947 struct nfs4_exception exception = { };
5948 int err;
5949 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005950 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005951 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 switch (err) {
5953 case -NFS4ERR_STALE_STATEID:
5954 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 case 0:
5956 return 0;
5957 }
5958 err = nfs4_handle_exception(server, err, &exception);
5959 } while (exception.retry);
5960 return err;
5961}
5962
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5964{
5965 struct inode *inode = state->inode;
5966 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005967 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005968 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005970 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005972 struct nfs_lockt_res res = {
5973 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 };
5975 struct rpc_message msg = {
5976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005977 .rpc_argp = &arg,
5978 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 .rpc_cred = state->owner->so_cred,
5980 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 struct nfs4_lock_state *lsp;
5982 int status;
5983
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005984 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005985 status = nfs4_set_lock_state(state, request);
5986 if (status != 0)
5987 goto out;
5988 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005989 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005990 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005991 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005992 switch (status) {
5993 case 0:
5994 request->fl_type = F_UNLCK;
5995 break;
5996 case -NFS4ERR_DENIED:
5997 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005999 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006000 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 return status;
6003}
6004
6005static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6006{
6007 struct nfs4_exception exception = { };
6008 int err;
6009
6010 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006011 err = _nfs4_proc_getlk(state, cmd, request);
6012 trace_nfs4_get_lock(request, state, cmd, err);
6013 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 &exception);
6015 } while (exception.retry);
6016 return err;
6017}
6018
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006019struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006020 struct nfs_locku_args arg;
6021 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006022 struct nfs4_lock_state *lsp;
6023 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006024 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006025 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006026 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006027 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006028};
6029
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006030static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6031 struct nfs_open_context *ctx,
6032 struct nfs4_lock_state *lsp,
6033 struct nfs_seqid *seqid)
6034{
6035 struct nfs4_unlockdata *p;
6036 struct inode *inode = lsp->ls_state->inode;
6037
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006038 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006039 if (p == NULL)
6040 return NULL;
6041 p->arg.fh = NFS_FH(inode);
6042 p->arg.fl = &p->fl;
6043 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006044 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006045 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006046 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006047 /* Ensure we don't close file until we're done freeing locks! */
6048 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006049 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006050 memcpy(&p->fl, fl, sizeof(p->fl));
6051 p->server = NFS_SERVER(inode);
6052 return p;
6053}
6054
Trond Myklebust06f814a2006-01-03 09:55:07 +01006055static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006056{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006057 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006058 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006059 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006060 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006061 put_nfs_open_context(calldata->ctx);
6062 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006063}
6064
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006065static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006066{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006067 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006068
Trond Myklebust14516c32010-07-31 14:29:06 -04006069 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6070 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006071 switch (task->tk_status) {
6072 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006073 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006074 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006075 if (nfs4_update_lock_stateid(calldata->lsp,
6076 &calldata->res.stateid))
6077 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006078 case -NFS4ERR_ADMIN_REVOKED:
6079 case -NFS4ERR_EXPIRED:
6080 nfs4_free_revoked_stateid(calldata->server,
6081 &calldata->arg.stateid,
6082 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006083 case -NFS4ERR_BAD_STATEID:
6084 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006085 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006086 if (!nfs4_stateid_match(&calldata->arg.stateid,
6087 &calldata->lsp->ls_stateid))
6088 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006089 break;
6090 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006091 if (nfs4_async_handle_error(task, calldata->server,
6092 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006093 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006094 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006095 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006096}
6097
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006098static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006099{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006100 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006102 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6103 nfs_async_iocounter_wait(task, calldata->l_ctx))
6104 return;
6105
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006106 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006107 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006108 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006109 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006110 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006111 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006112 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006113 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006114 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006115 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006116 &calldata->res.seq_res,
6117 task) != 0)
6118 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006119 return;
6120out_no_action:
6121 task->tk_action = NULL;
6122out_wait:
6123 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124}
6125
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006126static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006127 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006128 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006129 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006130};
6131
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006132static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6133 struct nfs_open_context *ctx,
6134 struct nfs4_lock_state *lsp,
6135 struct nfs_seqid *seqid)
6136{
6137 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006138 struct rpc_message msg = {
6139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6140 .rpc_cred = ctx->cred,
6141 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006142 struct rpc_task_setup task_setup_data = {
6143 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006144 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006145 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006146 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006147 .flags = RPC_TASK_ASYNC,
6148 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006149
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006150 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6151 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6152
Frank Filz137d6ac2007-07-09 15:32:29 -07006153 /* Ensure this is an unlock - when canceling a lock, the
6154 * canceled lock is passed in, and it won't be an unlock.
6155 */
6156 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006157 if (fl->fl_flags & FL_CLOSE)
6158 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006159
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006160 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6161 if (data == NULL) {
6162 nfs_free_seqid(seqid);
6163 return ERR_PTR(-ENOMEM);
6164 }
6165
Chuck Levera9c92d62013-08-09 12:48:18 -04006166 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006167 msg.rpc_argp = &data->arg;
6168 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006169 task_setup_data.callback_data = data;
6170 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006171}
6172
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6174{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006175 struct inode *inode = state->inode;
6176 struct nfs4_state_owner *sp = state->owner;
6177 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006178 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006179 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006180 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006181 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006182 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006183 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
Trond Myklebust9b073572006-06-29 16:38:34 -04006185 status = nfs4_set_lock_state(state, request);
6186 /* Unlock _before_ we do the RPC call */
6187 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006188 /* Exclude nfs_delegation_claim_locks() */
6189 mutex_lock(&sp->so_delegreturn_mutex);
6190 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006191 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006192 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006193 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006194 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006195 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006196 }
6197 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006198 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006199 if (status != 0)
6200 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006201 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006202 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006203 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6204 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006205 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6206 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006207 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006208 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006209 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006210 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006211 status = PTR_ERR(task);
6212 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006213 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006214 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006215 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006216out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006217 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006218 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006219 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220}
6221
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222struct nfs4_lockdata {
6223 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006224 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006225 struct nfs4_lock_state *lsp;
6226 struct nfs_open_context *ctx;
6227 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006228 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006229 int rpc_status;
6230 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006231 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006232};
6233
6234static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006235 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6236 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006237{
6238 struct nfs4_lockdata *p;
6239 struct inode *inode = lsp->ls_state->inode;
6240 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006241 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006242
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006243 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006244 if (p == NULL)
6245 return NULL;
6246
6247 p->arg.fh = NFS_FH(inode);
6248 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006249 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006250 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006251 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006252 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6253 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006254 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006255 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006256 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006257 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006258 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006259 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006260 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006261 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006262 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006263 p->ctx = get_nfs_open_context(ctx);
6264 memcpy(&p->fl, fl, sizeof(p->fl));
6265 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006266out_free_seqid:
6267 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006268out_free:
6269 kfree(p);
6270 return NULL;
6271}
6272
6273static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6274{
6275 struct nfs4_lockdata *data = calldata;
6276 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277
Harvey Harrison3110ff82008-05-02 13:42:44 -07006278 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006279 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006280 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006281 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006282 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006283 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006284 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006285 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006286 nfs4_stateid_copy(&data->arg.open_stateid,
6287 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006288 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006289 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006290 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006291 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006292 nfs4_stateid_copy(&data->arg.lock_stateid,
6293 &data->lsp->ls_stateid);
6294 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006295 if (!nfs4_valid_open_stateid(state)) {
6296 data->rpc_status = -EBADF;
6297 task->tk_action = NULL;
6298 goto out_release_open_seqid;
6299 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006300 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006301 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006302 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006303 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006304 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006305 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006306out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006307 nfs_release_seqid(data->arg.open_seqid);
6308out_release_lock_seqid:
6309 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006310out_wait:
6311 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006312 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006313}
6314
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6316{
6317 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006318 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006319
Harvey Harrison3110ff82008-05-02 13:42:44 -07006320 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006321
Trond Myklebust14516c32010-07-31 14:29:06 -04006322 if (!nfs4_sequence_done(task, &data->res.seq_res))
6323 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006324
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006325 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006326 switch (task->tk_status) {
6327 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006328 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006329 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006330 if (data->arg.new_lock) {
6331 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006332 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006333 rpc_restart_call_prepare(task);
6334 break;
6335 }
6336 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006337 if (data->arg.new_lock_owner != 0) {
6338 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6339 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6340 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6341 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6342 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006343 break;
6344 case -NFS4ERR_BAD_STATEID:
6345 case -NFS4ERR_OLD_STATEID:
6346 case -NFS4ERR_STALE_STATEID:
6347 case -NFS4ERR_EXPIRED:
6348 if (data->arg.new_lock_owner != 0) {
6349 if (!nfs4_stateid_match(&data->arg.open_stateid,
6350 &lsp->ls_state->open_stateid))
6351 rpc_restart_call_prepare(task);
6352 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6353 &lsp->ls_stateid))
6354 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006355 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006356 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006357}
6358
6359static void nfs4_lock_release(void *calldata)
6360{
6361 struct nfs4_lockdata *data = calldata;
6362
Harvey Harrison3110ff82008-05-02 13:42:44 -07006363 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006364 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006365 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006366 struct rpc_task *task;
6367 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6368 data->arg.lock_seqid);
6369 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006370 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006371 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006372 } else
6373 nfs_free_seqid(data->arg.lock_seqid);
6374 nfs4_put_lock_state(data->lsp);
6375 put_nfs_open_context(data->ctx);
6376 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006377 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006378}
6379
6380static const struct rpc_call_ops nfs4_lock_ops = {
6381 .rpc_call_prepare = nfs4_lock_prepare,
6382 .rpc_call_done = nfs4_lock_done,
6383 .rpc_release = nfs4_lock_release,
6384};
6385
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006386static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6387{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006388 switch (error) {
6389 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006390 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006391 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006392 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006393 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006394 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006395 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006396 break;
6397 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006398 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006399 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006400 };
6401}
6402
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006403static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006404{
6405 struct nfs4_lockdata *data;
6406 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006407 struct rpc_message msg = {
6408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6409 .rpc_cred = state->owner->so_cred,
6410 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006411 struct rpc_task_setup task_setup_data = {
6412 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006413 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006414 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006415 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006416 .flags = RPC_TASK_ASYNC,
6417 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006418 int ret;
6419
Harvey Harrison3110ff82008-05-02 13:42:44 -07006420 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006421 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006422 fl->fl_u.nfs4_fl.owner,
6423 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006424 if (data == NULL)
6425 return -ENOMEM;
6426 if (IS_SETLKW(cmd))
6427 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006428 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006429 msg.rpc_argp = &data->arg;
6430 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006431 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006432 if (recovery_type > NFS_LOCK_NEW) {
6433 if (recovery_type == NFS_LOCK_RECLAIM)
6434 data->arg.reclaim = NFS_LOCK_RECLAIM;
6435 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006436 } else
6437 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006438 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006439 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006440 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006441 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006442 if (ret == 0) {
6443 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006444 if (ret)
6445 nfs4_handle_setlk_error(data->server, data->lsp,
6446 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006447 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006448 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006449 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006450 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006451 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453}
6454
6455static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6456{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006457 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006458 struct nfs4_exception exception = {
6459 .inode = state->inode,
6460 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006461 int err;
6462
6463 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006464 /* Cache the lock if possible... */
6465 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6466 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006467 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006468 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006469 break;
6470 nfs4_handle_exception(server, err, &exception);
6471 } while (exception.retry);
6472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473}
6474
6475static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6476{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006477 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006478 struct nfs4_exception exception = {
6479 .inode = state->inode,
6480 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006481 int err;
6482
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006483 err = nfs4_set_lock_state(state, request);
6484 if (err != 0)
6485 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006486 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006487 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6488 return 0;
6489 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006490 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006491 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6492 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006493 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006494 switch (err) {
6495 default:
6496 goto out;
6497 case -NFS4ERR_GRACE:
6498 case -NFS4ERR_DELAY:
6499 nfs4_handle_exception(server, err, &exception);
6500 err = 0;
6501 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006502 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006503out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006504 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505}
6506
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006507#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006508static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6509{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006510 struct nfs4_lock_state *lsp;
6511 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006512
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006513 status = nfs4_set_lock_state(state, request);
6514 if (status != 0)
6515 return status;
6516 lsp = request->fl_u.nfs4_fl.owner;
6517 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6518 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6519 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006520 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006521}
6522#endif
6523
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6525{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006526 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006527 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006528 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006529 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
Trond Myklebust01c3b862006-06-29 16:38:39 -04006531 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006532 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006533 if (status < 0)
6534 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006535 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006536 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006537 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006538 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006539 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006540 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006541 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006542 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006543 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006544 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006545 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006546 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006547 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006548 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006549out:
6550 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 return status;
6552}
6553
6554static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6555{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006556 struct nfs4_exception exception = {
6557 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006558 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006559 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 int err;
6561
6562 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006563 err = _nfs4_proc_setlk(state, cmd, request);
6564 if (err == -NFS4ERR_DENIED)
6565 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006567 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 } while (exception.retry);
6569 return err;
6570}
6571
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006572#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6573#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6574
6575static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006576nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6577 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006578{
6579 int status = -ERESTARTSYS;
6580 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6581
6582 while(!signalled()) {
6583 status = nfs4_proc_setlk(state, cmd, request);
6584 if ((status != -EAGAIN) || IS_SETLK(cmd))
6585 break;
6586 freezable_schedule_timeout_interruptible(timeout);
6587 timeout *= 2;
6588 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6589 status = -ERESTARTSYS;
6590 }
6591 return status;
6592}
6593
Jeff Laytona1d617d82016-09-17 18:17:39 -04006594#ifdef CONFIG_NFS_V4_1
6595struct nfs4_lock_waiter {
6596 struct task_struct *task;
6597 struct inode *inode;
6598 struct nfs_lowner *owner;
6599 bool notified;
6600};
6601
6602static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006603nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006604{
6605 int ret;
6606 struct cb_notify_lock_args *cbnl = key;
6607 struct nfs4_lock_waiter *waiter = wait->private;
6608 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6609 *wowner = waiter->owner;
6610
6611 /* Only wake if the callback was for the same owner */
6612 if (lowner->clientid != wowner->clientid ||
6613 lowner->id != wowner->id ||
6614 lowner->s_dev != wowner->s_dev)
6615 return 0;
6616
6617 /* Make sure it's for the right inode */
6618 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6619 return 0;
6620
6621 waiter->notified = true;
6622
6623 /* override "private" so we can use default_wake_function */
6624 wait->private = waiter->task;
6625 ret = autoremove_wake_function(wait, mode, flags, key);
6626 wait->private = waiter;
6627 return ret;
6628}
6629
6630static int
6631nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6632{
6633 int status = -ERESTARTSYS;
6634 unsigned long flags;
6635 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6636 struct nfs_server *server = NFS_SERVER(state->inode);
6637 struct nfs_client *clp = server->nfs_client;
6638 wait_queue_head_t *q = &clp->cl_lock_waitq;
6639 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6640 .id = lsp->ls_seqid.owner_id,
6641 .s_dev = server->s_dev };
6642 struct nfs4_lock_waiter waiter = { .task = current,
6643 .inode = state->inode,
6644 .owner = &owner,
6645 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006646 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006647
6648 /* Don't bother with waitqueue if we don't expect a callback */
6649 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6650 return nfs4_retry_setlk_simple(state, cmd, request);
6651
6652 init_wait(&wait);
6653 wait.private = &waiter;
6654 wait.func = nfs4_wake_lock_waiter;
6655 add_wait_queue(q, &wait);
6656
6657 while(!signalled()) {
6658 status = nfs4_proc_setlk(state, cmd, request);
6659 if ((status != -EAGAIN) || IS_SETLK(cmd))
6660 break;
6661
6662 status = -ERESTARTSYS;
6663 spin_lock_irqsave(&q->lock, flags);
6664 if (waiter.notified) {
6665 spin_unlock_irqrestore(&q->lock, flags);
6666 continue;
6667 }
6668 set_current_state(TASK_INTERRUPTIBLE);
6669 spin_unlock_irqrestore(&q->lock, flags);
6670
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006671 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006672 }
6673
6674 finish_wait(q, &wait);
6675 return status;
6676}
6677#else /* !CONFIG_NFS_V4_1 */
6678static inline int
6679nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6680{
6681 return nfs4_retry_setlk_simple(state, cmd, request);
6682}
6683#endif
6684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685static int
6686nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6687{
6688 struct nfs_open_context *ctx;
6689 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 int status;
6691
6692 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006693 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 state = ctx->state;
6695
Trond Myklebustd9531262009-07-21 19:22:38 -04006696 if (IS_GETLK(cmd)) {
6697 if (state != NULL)
6698 return nfs4_proc_getlk(state, F_GETLK, request);
6699 return 0;
6700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
6702 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6703 return -EINVAL;
6704
Trond Myklebustd9531262009-07-21 19:22:38 -04006705 if (request->fl_type == F_UNLCK) {
6706 if (state != NULL)
6707 return nfs4_proc_unlck(state, cmd, request);
6708 return 0;
6709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
Trond Myklebustd9531262009-07-21 19:22:38 -04006711 if (state == NULL)
6712 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006713
6714 if ((request->fl_flags & FL_POSIX) &&
6715 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6716 return -ENOLCK;
6717
Jeff Layton1ea67db2016-09-17 18:17:37 -04006718 status = nfs4_set_lock_state(state, request);
6719 if (status != 0)
6720 return status;
6721
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006722 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723}
6724
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006725int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006726{
6727 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006728 int err;
6729
6730 err = nfs4_set_lock_state(state, fl);
6731 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006732 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006733 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006734 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006735}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006736
Trond Myklebustcf470c32012-03-07 13:49:12 -05006737struct nfs_release_lockowner_data {
6738 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006739 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006740 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006741 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006742 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006743};
6744
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006745static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6746{
6747 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006748 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006749 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6750 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006751 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006752 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006753}
6754
6755static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6756{
6757 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006758 struct nfs_server *server = data->server;
6759
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006760 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006761
6762 switch (task->tk_status) {
6763 case 0:
6764 renew_lease(server, data->timestamp);
6765 break;
6766 case -NFS4ERR_STALE_CLIENTID:
6767 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006768 nfs4_schedule_lease_recovery(server->nfs_client);
6769 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006770 case -NFS4ERR_LEASE_MOVED:
6771 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006772 if (nfs4_async_handle_error(task, server,
6773 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006774 rpc_restart_call_prepare(task);
6775 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006776}
6777
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006778static void nfs4_release_lockowner_release(void *calldata)
6779{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006780 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006781 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006782 kfree(calldata);
6783}
6784
Trond Myklebust17280172012-03-11 13:11:00 -04006785static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006786 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6787 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006788 .rpc_release = nfs4_release_lockowner_release,
6789};
6790
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006791static void
6792nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006793{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006794 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006795 struct rpc_message msg = {
6796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6797 };
6798
6799 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006800 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006801
Trond Myklebustcf470c32012-03-07 13:49:12 -05006802 data = kmalloc(sizeof(*data), GFP_NOFS);
6803 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006804 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006805 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006806 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006807 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6808 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6809 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006810
Trond Myklebustcf470c32012-03-07 13:49:12 -05006811 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006812 msg.rpc_resp = &data->res;
6813 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006814 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006815}
6816
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006817#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6818
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006819static int nfs4_xattr_set_nfs4_acl(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)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006823{
Al Viro59301222016-05-27 10:19:30 -04006824 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006825}
6826
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006827static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006828 struct dentry *unused, struct inode *inode,
6829 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006830{
Al Virob2968212016-04-10 20:48:24 -04006831 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006832}
6833
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006834static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006835{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006836 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006837}
6838
David Quigleyc9bccef2013-05-22 12:50:45 -04006839#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006840
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006841static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006842 struct dentry *unused, struct inode *inode,
6843 const char *key, const void *buf,
6844 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006845{
6846 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006847 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006848
6849 return -EOPNOTSUPP;
6850}
6851
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006852static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006853 struct dentry *unused, struct inode *inode,
6854 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006855{
6856 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006857 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006858 return -EOPNOTSUPP;
6859}
6860
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006861static ssize_t
6862nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006863{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006864 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006865
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006866 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6867 len = security_inode_listsecurity(inode, list, list_len);
6868 if (list_len && len > list_len)
6869 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006870 }
6871 return len;
6872}
6873
6874static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6875 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006876 .get = nfs4_xattr_get_nfs4_label,
6877 .set = nfs4_xattr_set_nfs4_label,
6878};
David Quigleyc9bccef2013-05-22 12:50:45 -04006879
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006880#else
6881
6882static ssize_t
6883nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6884{
6885 return 0;
6886}
6887
6888#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006889
Andy Adamson533eb462011-06-13 18:25:56 -04006890/*
6891 * nfs_fhget will use either the mounted_on_fileid or the fileid
6892 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006893static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6894{
Andy Adamson533eb462011-06-13 18:25:56 -04006895 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6896 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6897 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006898 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006899 return;
6900
6901 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006902 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006903 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6904 fattr->nlink = 2;
6905}
6906
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006907static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6908 const struct qstr *name,
6909 struct nfs4_fs_locations *fs_locations,
6910 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006911{
6912 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006913 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006914 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006915 };
6916 struct nfs4_fs_locations_arg args = {
6917 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006918 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006919 .page = page,
6920 .bitmask = bitmask,
6921 };
Benny Halevy22958462009-04-01 09:22:02 -04006922 struct nfs4_fs_locations_res res = {
6923 .fs_locations = fs_locations,
6924 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006925 struct rpc_message msg = {
6926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6927 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006928 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006929 };
6930 int status;
6931
Harvey Harrison3110ff82008-05-02 13:42:44 -07006932 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006933
6934 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6935 * is not supported */
6936 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6937 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6938 else
6939 bitmask[0] |= FATTR4_WORD0_FILEID;
6940
Trond Myklebustc228fd32007-01-13 02:28:11 -05006941 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006942 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006943 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006944 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006945 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006946 return status;
6947}
6948
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006949int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6950 const struct qstr *name,
6951 struct nfs4_fs_locations *fs_locations,
6952 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006953{
6954 struct nfs4_exception exception = { };
6955 int err;
6956 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006957 err = _nfs4_proc_fs_locations(client, dir, name,
6958 fs_locations, page);
6959 trace_nfs4_get_fs_locations(dir, name, err);
6960 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006961 &exception);
6962 } while (exception.retry);
6963 return err;
6964}
6965
Chuck Leverb03d7352013-10-17 14:12:50 -04006966/*
6967 * This operation also signals the server that this client is
6968 * performing migration recovery. The server can stop returning
6969 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6970 * appended to this compound to identify the client ID which is
6971 * performing recovery.
6972 */
6973static int _nfs40_proc_get_locations(struct inode *inode,
6974 struct nfs4_fs_locations *locations,
6975 struct page *page, struct rpc_cred *cred)
6976{
6977 struct nfs_server *server = NFS_SERVER(inode);
6978 struct rpc_clnt *clnt = server->client;
6979 u32 bitmask[2] = {
6980 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6981 };
6982 struct nfs4_fs_locations_arg args = {
6983 .clientid = server->nfs_client->cl_clientid,
6984 .fh = NFS_FH(inode),
6985 .page = page,
6986 .bitmask = bitmask,
6987 .migration = 1, /* skip LOOKUP */
6988 .renew = 1, /* append RENEW */
6989 };
6990 struct nfs4_fs_locations_res res = {
6991 .fs_locations = locations,
6992 .migration = 1,
6993 .renew = 1,
6994 };
6995 struct rpc_message msg = {
6996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6997 .rpc_argp = &args,
6998 .rpc_resp = &res,
6999 .rpc_cred = cred,
7000 };
7001 unsigned long now = jiffies;
7002 int status;
7003
7004 nfs_fattr_init(&locations->fattr);
7005 locations->server = server;
7006 locations->nlocations = 0;
7007
7008 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7009 nfs4_set_sequence_privileged(&args.seq_args);
7010 status = nfs4_call_sync_sequence(clnt, server, &msg,
7011 &args.seq_args, &res.seq_res);
7012 if (status)
7013 return status;
7014
7015 renew_lease(server, now);
7016 return 0;
7017}
7018
7019#ifdef CONFIG_NFS_V4_1
7020
7021/*
7022 * This operation also signals the server that this client is
7023 * performing migration recovery. The server can stop asserting
7024 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7025 * performing this operation is identified in the SEQUENCE
7026 * operation in this compound.
7027 *
7028 * When the client supports GETATTR(fs_locations_info), it can
7029 * be plumbed in here.
7030 */
7031static int _nfs41_proc_get_locations(struct inode *inode,
7032 struct nfs4_fs_locations *locations,
7033 struct page *page, struct rpc_cred *cred)
7034{
7035 struct nfs_server *server = NFS_SERVER(inode);
7036 struct rpc_clnt *clnt = server->client;
7037 u32 bitmask[2] = {
7038 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7039 };
7040 struct nfs4_fs_locations_arg args = {
7041 .fh = NFS_FH(inode),
7042 .page = page,
7043 .bitmask = bitmask,
7044 .migration = 1, /* skip LOOKUP */
7045 };
7046 struct nfs4_fs_locations_res res = {
7047 .fs_locations = locations,
7048 .migration = 1,
7049 };
7050 struct rpc_message msg = {
7051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7052 .rpc_argp = &args,
7053 .rpc_resp = &res,
7054 .rpc_cred = cred,
7055 };
7056 int status;
7057
7058 nfs_fattr_init(&locations->fattr);
7059 locations->server = server;
7060 locations->nlocations = 0;
7061
7062 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7063 nfs4_set_sequence_privileged(&args.seq_args);
7064 status = nfs4_call_sync_sequence(clnt, server, &msg,
7065 &args.seq_args, &res.seq_res);
7066 if (status == NFS4_OK &&
7067 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7068 status = -NFS4ERR_LEASE_MOVED;
7069 return status;
7070}
7071
7072#endif /* CONFIG_NFS_V4_1 */
7073
7074/**
7075 * nfs4_proc_get_locations - discover locations for a migrated FSID
7076 * @inode: inode on FSID that is migrating
7077 * @locations: result of query
7078 * @page: buffer
7079 * @cred: credential to use for this operation
7080 *
7081 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7082 * operation failed, or a negative errno if a local error occurred.
7083 *
7084 * On success, "locations" is filled in, but if the server has
7085 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7086 * asserted.
7087 *
7088 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7089 * from this client that require migration recovery.
7090 */
7091int nfs4_proc_get_locations(struct inode *inode,
7092 struct nfs4_fs_locations *locations,
7093 struct page *page, struct rpc_cred *cred)
7094{
7095 struct nfs_server *server = NFS_SERVER(inode);
7096 struct nfs_client *clp = server->nfs_client;
7097 const struct nfs4_mig_recovery_ops *ops =
7098 clp->cl_mvops->mig_recovery_ops;
7099 struct nfs4_exception exception = { };
7100 int status;
7101
7102 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7103 (unsigned long long)server->fsid.major,
7104 (unsigned long long)server->fsid.minor,
7105 clp->cl_hostname);
7106 nfs_display_fhandle(NFS_FH(inode), __func__);
7107
7108 do {
7109 status = ops->get_locations(inode, locations, page, cred);
7110 if (status != -NFS4ERR_DELAY)
7111 break;
7112 nfs4_handle_exception(server, status, &exception);
7113 } while (exception.retry);
7114 return status;
7115}
7116
Chuck Lever44c99932013-10-17 14:13:30 -04007117/*
7118 * This operation also signals the server that this client is
7119 * performing "lease moved" recovery. The server can stop
7120 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7121 * is appended to this compound to identify the client ID which is
7122 * performing recovery.
7123 */
7124static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7125{
7126 struct nfs_server *server = NFS_SERVER(inode);
7127 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7128 struct rpc_clnt *clnt = server->client;
7129 struct nfs4_fsid_present_arg args = {
7130 .fh = NFS_FH(inode),
7131 .clientid = clp->cl_clientid,
7132 .renew = 1, /* append RENEW */
7133 };
7134 struct nfs4_fsid_present_res res = {
7135 .renew = 1,
7136 };
7137 struct rpc_message msg = {
7138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7139 .rpc_argp = &args,
7140 .rpc_resp = &res,
7141 .rpc_cred = cred,
7142 };
7143 unsigned long now = jiffies;
7144 int status;
7145
7146 res.fh = nfs_alloc_fhandle();
7147 if (res.fh == NULL)
7148 return -ENOMEM;
7149
7150 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7151 nfs4_set_sequence_privileged(&args.seq_args);
7152 status = nfs4_call_sync_sequence(clnt, server, &msg,
7153 &args.seq_args, &res.seq_res);
7154 nfs_free_fhandle(res.fh);
7155 if (status)
7156 return status;
7157
7158 do_renew_lease(clp, now);
7159 return 0;
7160}
7161
7162#ifdef CONFIG_NFS_V4_1
7163
7164/*
7165 * This operation also signals the server that this client is
7166 * performing "lease moved" recovery. The server can stop asserting
7167 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7168 * this operation is identified in the SEQUENCE operation in this
7169 * compound.
7170 */
7171static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7172{
7173 struct nfs_server *server = NFS_SERVER(inode);
7174 struct rpc_clnt *clnt = server->client;
7175 struct nfs4_fsid_present_arg args = {
7176 .fh = NFS_FH(inode),
7177 };
7178 struct nfs4_fsid_present_res res = {
7179 };
7180 struct rpc_message msg = {
7181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7182 .rpc_argp = &args,
7183 .rpc_resp = &res,
7184 .rpc_cred = cred,
7185 };
7186 int status;
7187
7188 res.fh = nfs_alloc_fhandle();
7189 if (res.fh == NULL)
7190 return -ENOMEM;
7191
7192 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7193 nfs4_set_sequence_privileged(&args.seq_args);
7194 status = nfs4_call_sync_sequence(clnt, server, &msg,
7195 &args.seq_args, &res.seq_res);
7196 nfs_free_fhandle(res.fh);
7197 if (status == NFS4_OK &&
7198 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7199 status = -NFS4ERR_LEASE_MOVED;
7200 return status;
7201}
7202
7203#endif /* CONFIG_NFS_V4_1 */
7204
7205/**
7206 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7207 * @inode: inode on FSID to check
7208 * @cred: credential to use for this operation
7209 *
7210 * Server indicates whether the FSID is present, moved, or not
7211 * recognized. This operation is necessary to clear a LEASE_MOVED
7212 * condition for this client ID.
7213 *
7214 * Returns NFS4_OK if the FSID is present on this server,
7215 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7216 * NFS4ERR code if some error occurred on the server, or a
7217 * negative errno if a local failure occurred.
7218 */
7219int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7220{
7221 struct nfs_server *server = NFS_SERVER(inode);
7222 struct nfs_client *clp = server->nfs_client;
7223 const struct nfs4_mig_recovery_ops *ops =
7224 clp->cl_mvops->mig_recovery_ops;
7225 struct nfs4_exception exception = { };
7226 int status;
7227
7228 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7229 (unsigned long long)server->fsid.major,
7230 (unsigned long long)server->fsid.minor,
7231 clp->cl_hostname);
7232 nfs_display_fhandle(NFS_FH(inode), __func__);
7233
7234 do {
7235 status = ops->fsid_present(inode, cred);
7236 if (status != -NFS4ERR_DELAY)
7237 break;
7238 nfs4_handle_exception(server, status, &exception);
7239 } while (exception.retry);
7240 return status;
7241}
7242
Andy Adamson5ec16a82013-08-08 10:57:55 -04007243/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007244 * If 'use_integrity' is true and the state managment nfs_client
7245 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7246 * and the machine credential as per RFC3530bis and RFC5661 Security
7247 * Considerations sections. Otherwise, just use the user cred with the
7248 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007249 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007250static 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 +00007251{
7252 int status;
7253 struct nfs4_secinfo_arg args = {
7254 .dir_fh = NFS_FH(dir),
7255 .name = name,
7256 };
7257 struct nfs4_secinfo_res res = {
7258 .flavors = flavors,
7259 };
7260 struct rpc_message msg = {
7261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7262 .rpc_argp = &args,
7263 .rpc_resp = &res,
7264 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007265 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007266 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007267
7268 if (use_integrity) {
7269 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007270 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7271 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007272 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007273
7274 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007275
7276 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7277 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7278
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007279 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7280 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007281 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007282
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007283 if (cred)
7284 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007285
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007286 return status;
7287}
7288
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007289int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7290 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007291{
7292 struct nfs4_exception exception = { };
7293 int err;
7294 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007295 err = -NFS4ERR_WRONGSEC;
7296
7297 /* try to use integrity protection with machine cred */
7298 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7299 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7300
7301 /*
7302 * if unable to use integrity protection, or SECINFO with
7303 * integrity protection returns NFS4ERR_WRONGSEC (which is
7304 * disallowed by spec, but exists in deployed servers) use
7305 * the current filesystem's rpc_client and the user cred.
7306 */
7307 if (err == -NFS4ERR_WRONGSEC)
7308 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7309
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007310 trace_nfs4_secinfo(dir, name, err);
7311 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007312 &exception);
7313 } while (exception.retry);
7314 return err;
7315}
7316
Andy Adamson557134a2009-04-01 09:21:53 -04007317#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007318/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007319 * Check the exchange flags returned by the server for invalid flags, having
7320 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7321 * DS flags set.
7322 */
7323static int nfs4_check_cl_exchange_flags(u32 flags)
7324{
7325 if (flags & ~EXCHGID4_FLAG_MASK_R)
7326 goto out_inval;
7327 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7328 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7329 goto out_inval;
7330 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7331 goto out_inval;
7332 return NFS_OK;
7333out_inval:
7334 return -NFS4ERR_INVAL;
7335}
7336
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007337static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007338nfs41_same_server_scope(struct nfs41_server_scope *a,
7339 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007340{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007341 if (a->server_scope_sz != b->server_scope_sz)
7342 return false;
7343 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007344}
7345
Andy Adamson02a95de2016-02-05 16:08:37 -05007346static void
7347nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7348{
7349}
7350
7351static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7352 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7353};
7354
Andy Adamson357f54d2010-12-14 10:11:57 -05007355/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007356 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007357 *
7358 * The 4.1 client currently uses the same TCP connection for the
7359 * fore and backchannel.
7360 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007361static
7362int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7363 struct rpc_xprt *xprt,
7364 struct nfs_client *clp,
7365 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007366{
7367 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007368 struct nfs41_bind_conn_to_session_args args = {
7369 .client = clp,
7370 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7371 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007372 struct nfs41_bind_conn_to_session_res res;
7373 struct rpc_message msg = {
7374 .rpc_proc =
7375 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007376 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007377 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007378 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007379 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007380 struct rpc_task_setup task_setup_data = {
7381 .rpc_client = clnt,
7382 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007383 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007384 .rpc_message = &msg,
7385 .flags = RPC_TASK_TIMEOUT,
7386 };
7387 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007388
Trond Myklebust71a097c2015-02-18 09:27:18 -08007389 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7390 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7391 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007392
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007393 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7394 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7395 args.dir = NFS4_CDFC4_FORE;
7396
7397 task = rpc_run_task(&task_setup_data);
7398 if (!IS_ERR(task)) {
7399 status = task->tk_status;
7400 rpc_put_task(task);
7401 } else
7402 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007403 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007404 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007405 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007406 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7407 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007408 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007409 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007410 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007411 dprintk("NFS: %s: Unexpected direction from server\n",
7412 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007413 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007414 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007415 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007416 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7417 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007418 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007419 }
7420 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007421
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007422 return status;
7423}
7424
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007425struct rpc_bind_conn_calldata {
7426 struct nfs_client *clp;
7427 struct rpc_cred *cred;
7428};
7429
7430static int
7431nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7432 struct rpc_xprt *xprt,
7433 void *calldata)
7434{
7435 struct rpc_bind_conn_calldata *p = calldata;
7436
7437 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7438}
7439
7440int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7441{
7442 struct rpc_bind_conn_calldata data = {
7443 .clp = clp,
7444 .cred = cred,
7445 };
7446 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7447 nfs4_proc_bind_conn_to_session_callback, &data);
7448}
7449
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007450/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007451 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7452 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007453 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007454static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7455 .how = SP4_MACH_CRED,
7456 .enforce.u.words = {
7457 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7458 1 << (OP_EXCHANGE_ID - 32) |
7459 1 << (OP_CREATE_SESSION - 32) |
7460 1 << (OP_DESTROY_SESSION - 32) |
7461 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007462 },
7463 .allow.u.words = {
7464 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007465 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007466 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007467 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007468 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007469 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007470 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007471 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007472 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007473 1 << (OP_FREE_STATEID - 32) |
7474 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007475 }
7476};
7477
7478/*
7479 * Select the state protection mode for client `clp' given the server results
7480 * from exchange_id in `sp'.
7481 *
7482 * Returns 0 on success, negative errno otherwise.
7483 */
7484static int nfs4_sp4_select_mode(struct nfs_client *clp,
7485 struct nfs41_state_protection *sp)
7486{
7487 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7488 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7489 1 << (OP_EXCHANGE_ID - 32) |
7490 1 << (OP_CREATE_SESSION - 32) |
7491 1 << (OP_DESTROY_SESSION - 32) |
7492 1 << (OP_DESTROY_CLIENTID - 32)
7493 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007494 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007495 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007496 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007497
7498 if (sp->how == SP4_MACH_CRED) {
7499 /* Print state protect result */
7500 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7501 for (i = 0; i <= LAST_NFS4_OP; i++) {
7502 if (test_bit(i, sp->enforce.u.longs))
7503 dfprintk(MOUNT, " enforce op %d\n", i);
7504 if (test_bit(i, sp->allow.u.longs))
7505 dfprintk(MOUNT, " allow op %d\n", i);
7506 }
7507
7508 /* make sure nothing is on enforce list that isn't supported */
7509 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7510 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7511 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007512 ret = -EINVAL;
7513 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007514 }
7515 }
7516
7517 /*
7518 * Minimal mode - state operations are allowed to use machine
7519 * credential. Note this already happens by default, so the
7520 * client doesn't have to do anything more than the negotiation.
7521 *
7522 * NOTE: we don't care if EXCHANGE_ID is in the list -
7523 * we're already using the machine cred for exchange_id
7524 * and will never use a different cred.
7525 */
7526 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7527 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7528 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7529 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7530 dfprintk(MOUNT, "sp4_mach_cred:\n");
7531 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007532 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007533 } else {
7534 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007535 ret = -EINVAL;
7536 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007537 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007538
7539 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007540 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7541 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007542 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7543 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007544 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007545 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007546
Andrew Elble99ade3c2015-12-02 09:39:51 -05007547 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7548 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007549 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007550 }
7551
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007552 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7553 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7554 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007555 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007556 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007557
7558 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7559 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7560 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007561 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007562 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007563
7564 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7565 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007566 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007567 }
7568
7569 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7570 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007571 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007572 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007573 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007574out:
7575 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007576 return 0;
7577}
7578
Andy Adamson8d89bd72016-09-09 09:22:18 -04007579struct nfs41_exchange_id_data {
7580 struct nfs41_exchange_id_res res;
7581 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007582};
7583
Andy Adamson8d89bd72016-09-09 09:22:18 -04007584static void nfs4_exchange_id_release(void *data)
7585{
7586 struct nfs41_exchange_id_data *cdata =
7587 (struct nfs41_exchange_id_data *)data;
7588
Olga Kornievskaia63513232017-03-13 10:36:19 -04007589 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007590 kfree(cdata->res.impl_id);
7591 kfree(cdata->res.server_scope);
7592 kfree(cdata->res.server_owner);
7593 kfree(cdata);
7594}
7595
7596static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007597 .rpc_release = nfs4_exchange_id_release,
7598};
7599
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007600/*
7601 * _nfs4_proc_exchange_id()
7602 *
7603 * Wrapper for EXCHANGE_ID operation.
7604 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007605static struct rpc_task *
7606nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007607 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007608{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007609 struct rpc_message msg = {
7610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007611 .rpc_cred = cred,
7612 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007613 struct rpc_task_setup task_setup_data = {
7614 .rpc_client = clp->cl_rpcclient,
7615 .callback_ops = &nfs4_exchange_id_call_ops,
7616 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007617 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007618 };
7619 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007620 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007621
Elena Reshetova212bf412017-10-20 12:53:38 +03007622 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007623 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007624
Trond Myklebust9c760d12017-07-31 18:38:50 -04007625 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007626 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007627 if (!calldata)
7628 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007629
Trond Myklebustfd405592017-08-01 16:02:47 -04007630 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007631
7632 status = nfs4_init_uniform_client_string(clp);
7633 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007634 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007635
Andy Adamson8d89bd72016-09-09 09:22:18 -04007636 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7637 GFP_NOFS);
7638 status = -ENOMEM;
7639 if (unlikely(calldata->res.server_owner == NULL))
7640 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007641
Andy Adamson8d89bd72016-09-09 09:22:18 -04007642 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007643 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007644 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007645 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007646
Andy Adamson8d89bd72016-09-09 09:22:18 -04007647 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7648 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007649 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007650
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007651 switch (sp4_how) {
7652 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007653 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007654 break;
7655
7656 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007657 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007658 break;
7659
7660 default:
7661 /* unsupported! */
7662 WARN_ON_ONCE(1);
7663 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007664 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007665 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007666 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007667 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007668 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007669 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7670 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007671 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007672 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007673 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7674 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007675#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007676 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007677#endif
7678 msg.rpc_argp = &calldata->args;
7679 msg.rpc_resp = &calldata->res;
7680 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007681
Trond Myklebust9c760d12017-07-31 18:38:50 -04007682 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007683
7684out_impl_id:
7685 kfree(calldata->res.impl_id);
7686out_server_scope:
7687 kfree(calldata->res.server_scope);
7688out_server_owner:
7689 kfree(calldata->res.server_owner);
7690out_calldata:
7691 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007692out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007693 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007694 return ERR_PTR(status);
7695}
7696
7697/*
7698 * _nfs4_proc_exchange_id()
7699 *
7700 * Wrapper for EXCHANGE_ID operation.
7701 */
7702static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7703 u32 sp4_how)
7704{
7705 struct rpc_task *task;
7706 struct nfs41_exchange_id_args *argp;
7707 struct nfs41_exchange_id_res *resp;
7708 int status;
7709
7710 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7711 if (IS_ERR(task))
7712 return PTR_ERR(task);
7713
7714 argp = task->tk_msg.rpc_argp;
7715 resp = task->tk_msg.rpc_resp;
7716 status = task->tk_status;
7717 if (status != 0)
7718 goto out;
7719
7720 status = nfs4_check_cl_exchange_flags(resp->flags);
7721 if (status != 0)
7722 goto out;
7723
7724 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7725 if (status != 0)
7726 goto out;
7727
7728 clp->cl_clientid = resp->clientid;
7729 clp->cl_exchange_flags = resp->flags;
7730 clp->cl_seqid = resp->seqid;
7731 /* Client ID is not confirmed */
7732 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7733 clear_bit(NFS4_SESSION_ESTABLISHED,
7734 &clp->cl_session->session_state);
7735
7736 if (clp->cl_serverscope != NULL &&
7737 !nfs41_same_server_scope(clp->cl_serverscope,
7738 resp->server_scope)) {
7739 dprintk("%s: server_scope mismatch detected\n",
7740 __func__);
7741 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7742 }
7743
7744 swap(clp->cl_serverowner, resp->server_owner);
7745 swap(clp->cl_serverscope, resp->server_scope);
7746 swap(clp->cl_implid, resp->impl_id);
7747
7748 /* Save the EXCHANGE_ID verifier session trunk tests */
7749 memcpy(clp->cl_confirm.data, argp->verifier.data,
7750 sizeof(clp->cl_confirm.data));
7751out:
7752 trace_nfs4_exchange_id(clp, status);
7753 rpc_put_task(task);
7754 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007755}
7756
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007757/*
7758 * nfs4_proc_exchange_id()
7759 *
7760 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7761 *
7762 * Since the clientid has expired, all compounds using sessions
7763 * associated with the stale clientid will be returning
7764 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7765 * be in some phase of session reset.
7766 *
7767 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7768 */
7769int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7770{
7771 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7772 int status;
7773
7774 /* try SP4_MACH_CRED if krb5i/p */
7775 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7776 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007777 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007778 if (!status)
7779 return 0;
7780 }
7781
7782 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007783 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007784}
7785
Andy Adamson04fa2c62016-09-09 09:22:29 -04007786/**
7787 * nfs4_test_session_trunk
7788 *
7789 * This is an add_xprt_test() test function called from
7790 * rpc_clnt_setup_test_and_add_xprt.
7791 *
7792 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7793 * and is dereferrenced in nfs4_exchange_id_release
7794 *
7795 * Upon success, add the new transport to the rpc_clnt
7796 *
7797 * @clnt: struct rpc_clnt to get new transport
7798 * @xprt: the rpc_xprt to test
7799 * @data: call data for _nfs4_proc_exchange_id.
7800 */
7801int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7802 void *data)
7803{
7804 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007805 struct rpc_task *task;
7806 int status;
7807
Andy Adamson04fa2c62016-09-09 09:22:29 -04007808 u32 sp4_how;
7809
7810 dprintk("--> %s try %s\n", __func__,
7811 xprt->address_strings[RPC_DISPLAY_ADDR]);
7812
7813 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7814
7815 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007816 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7817 if (IS_ERR(task))
7818 return PTR_ERR(task);
7819
7820 status = task->tk_status;
7821 if (status == 0)
7822 status = nfs4_detect_session_trunking(adata->clp,
7823 task->tk_msg.rpc_resp, xprt);
7824
7825 rpc_put_task(task);
7826 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007827}
7828EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7829
Trond Myklebust66245532012-05-25 17:18:09 -04007830static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7831 struct rpc_cred *cred)
7832{
7833 struct rpc_message msg = {
7834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7835 .rpc_argp = clp,
7836 .rpc_cred = cred,
7837 };
7838 int status;
7839
7840 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007841 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007842 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007843 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007844 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7845 return status;
7846}
7847
7848static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7849 struct rpc_cred *cred)
7850{
7851 unsigned int loop;
7852 int ret;
7853
7854 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7855 ret = _nfs4_proc_destroy_clientid(clp, cred);
7856 switch (ret) {
7857 case -NFS4ERR_DELAY:
7858 case -NFS4ERR_CLIENTID_BUSY:
7859 ssleep(1);
7860 break;
7861 default:
7862 return ret;
7863 }
7864 }
7865 return 0;
7866}
7867
7868int nfs4_destroy_clientid(struct nfs_client *clp)
7869{
7870 struct rpc_cred *cred;
7871 int ret = 0;
7872
7873 if (clp->cl_mvops->minor_version < 1)
7874 goto out;
7875 if (clp->cl_exchange_flags == 0)
7876 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007877 if (clp->cl_preserve_clid)
7878 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007879 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007880 ret = nfs4_proc_destroy_clientid(clp, cred);
7881 if (cred)
7882 put_rpccred(cred);
7883 switch (ret) {
7884 case 0:
7885 case -NFS4ERR_STALE_CLIENTID:
7886 clp->cl_exchange_flags = 0;
7887 }
7888out:
7889 return ret;
7890}
7891
Andy Adamson2050f0c2009-04-01 09:22:30 -04007892struct nfs4_get_lease_time_data {
7893 struct nfs4_get_lease_time_args *args;
7894 struct nfs4_get_lease_time_res *res;
7895 struct nfs_client *clp;
7896};
7897
7898static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7899 void *calldata)
7900{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007901 struct nfs4_get_lease_time_data *data =
7902 (struct nfs4_get_lease_time_data *)calldata;
7903
7904 dprintk("--> %s\n", __func__);
7905 /* just setup sequence, do not trigger session recovery
7906 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007907 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007908 &data->args->la_seq_args,
7909 &data->res->lr_seq_res,
7910 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007911 dprintk("<-- %s\n", __func__);
7912}
7913
7914/*
7915 * Called from nfs4_state_manager thread for session setup, so don't recover
7916 * from sequence operation or clientid errors.
7917 */
7918static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7919{
7920 struct nfs4_get_lease_time_data *data =
7921 (struct nfs4_get_lease_time_data *)calldata;
7922
7923 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007924 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7925 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007926 switch (task->tk_status) {
7927 case -NFS4ERR_DELAY:
7928 case -NFS4ERR_GRACE:
7929 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7930 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7931 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007932 /* fall through */
7933 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007934 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007935 return;
7936 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007937 dprintk("<-- %s\n", __func__);
7938}
7939
Trond Myklebust17280172012-03-11 13:11:00 -04007940static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007941 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7942 .rpc_call_done = nfs4_get_lease_time_done,
7943};
7944
7945int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7946{
7947 struct rpc_task *task;
7948 struct nfs4_get_lease_time_args args;
7949 struct nfs4_get_lease_time_res res = {
7950 .lr_fsinfo = fsinfo,
7951 };
7952 struct nfs4_get_lease_time_data data = {
7953 .args = &args,
7954 .res = &res,
7955 .clp = clp,
7956 };
7957 struct rpc_message msg = {
7958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7959 .rpc_argp = &args,
7960 .rpc_resp = &res,
7961 };
7962 struct rpc_task_setup task_setup = {
7963 .rpc_client = clp->cl_rpcclient,
7964 .rpc_message = &msg,
7965 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007966 .callback_data = &data,
7967 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007968 };
7969 int status;
7970
Chuck Levera9c92d62013-08-09 12:48:18 -04007971 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007972 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007973 task = rpc_run_task(&task_setup);
7974
7975 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007976 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007977
Anna Schumakerf6148712017-04-07 14:15:23 -04007978 status = task->tk_status;
7979 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007980 return status;
7981}
7982
Andy Adamsonfc931582009-04-01 09:22:31 -04007983/*
7984 * Initialize the values to be used by the client in CREATE_SESSION
7985 * If nfs4_init_session set the fore channel request and response sizes,
7986 * use them.
7987 *
7988 * Set the back channel max_resp_sz_cached to zero to force the client to
7989 * always set csa_cachethis to FALSE because the current implementation
7990 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7991 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007992static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7993 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007994{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007995 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007996 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007997
Andy Adamson18aad3d2013-06-26 12:21:49 -04007998 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7999 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8000
Andy Adamsonfc931582009-04-01 09:22:31 -04008001 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008002 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8003 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008004 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008005 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008006
8007 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008008 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008009 __func__,
8010 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008011 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008012
8013 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008014 args->bc_attrs.max_rqst_sz = max_bc_payload;
8015 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008016 args->bc_attrs.max_resp_sz_cached = 0;
8017 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008018 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008019
8020 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8021 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8022 __func__,
8023 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8024 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8025 args->bc_attrs.max_reqs);
8026}
8027
Trond Myklebust79969dd2015-02-18 11:30:18 -08008028static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8029 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008030{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008031 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008032 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008033
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008034 if (rcvd->max_resp_sz > sent->max_resp_sz)
8035 return -EINVAL;
8036 /*
8037 * Our requested max_ops is the minimum we need; we're not
8038 * prepared to break up compounds into smaller pieces than that.
8039 * So, no point even trying to continue if the server won't
8040 * cooperate:
8041 */
8042 if (rcvd->max_ops < sent->max_ops)
8043 return -EINVAL;
8044 if (rcvd->max_reqs == 0)
8045 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008046 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8047 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008048 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008049}
8050
Trond Myklebust79969dd2015-02-18 11:30:18 -08008051static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8052 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008053{
8054 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008055 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008056
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008057 if (!(res->flags & SESSION4_BACK_CHAN))
8058 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008059 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8060 return -EINVAL;
8061 if (rcvd->max_resp_sz < sent->max_resp_sz)
8062 return -EINVAL;
8063 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8064 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008065 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008066 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008067 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008068 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008069out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008070 return 0;
8071}
Andy Adamson8d353012009-04-01 09:22:32 -04008072
Andy Adamson8d353012009-04-01 09:22:32 -04008073static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008074 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008075{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008076 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008077
Trond Myklebust79969dd2015-02-18 11:30:18 -08008078 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008079 if (ret)
8080 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008081 return nfs4_verify_back_channel_attrs(args, res);
8082}
8083
8084static void nfs4_update_session(struct nfs4_session *session,
8085 struct nfs41_create_session_res *res)
8086{
8087 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008088 /* Mark client id and session as being confirmed */
8089 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8090 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008091 session->flags = res->flags;
8092 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008093 if (res->flags & SESSION4_BACK_CHAN)
8094 memcpy(&session->bc_attrs, &res->bc_attrs,
8095 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008096}
8097
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008098static int _nfs4_proc_create_session(struct nfs_client *clp,
8099 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008100{
8101 struct nfs4_session *session = clp->cl_session;
8102 struct nfs41_create_session_args args = {
8103 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008104 .clientid = clp->cl_clientid,
8105 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008106 .cb_program = NFS4_CALLBACK,
8107 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008108 struct nfs41_create_session_res res;
8109
Andy Adamsonfc931582009-04-01 09:22:31 -04008110 struct rpc_message msg = {
8111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8112 .rpc_argp = &args,
8113 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008114 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008115 };
8116 int status;
8117
Chuck Lever6b26cc82016-05-02 14:40:40 -04008118 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008119 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008120
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008121 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008122 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008123
Trond Myklebustb519d402016-09-11 14:50:01 -04008124 switch (status) {
8125 case -NFS4ERR_STALE_CLIENTID:
8126 case -NFS4ERR_DELAY:
8127 case -ETIMEDOUT:
8128 case -EACCES:
8129 case -EAGAIN:
8130 goto out;
8131 };
8132
8133 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008134 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008135 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008136 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008137 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008138 if (status)
8139 goto out;
8140 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008141 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008142out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008143 return status;
8144}
8145
8146/*
8147 * Issues a CREATE_SESSION operation to the server.
8148 * It is the responsibility of the caller to verify the session is
8149 * expired before calling this routine.
8150 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008151int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008152{
8153 int status;
8154 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008155 struct nfs4_session *session = clp->cl_session;
8156
8157 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8158
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008159 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008160 if (status)
8161 goto out;
8162
Andy Adamsonaacd5532011-11-09 13:58:21 -05008163 /* Init or reset the session slot tables */
8164 status = nfs4_setup_session_slot_tables(session);
8165 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008166 if (status)
8167 goto out;
8168
8169 ptr = (unsigned *)&session->sess_id.data[0];
8170 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8171 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008172out:
8173 dprintk("<-- %s\n", __func__);
8174 return status;
8175}
8176
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008177/*
8178 * Issue the over-the-wire RPC DESTROY_SESSION.
8179 * The caller must serialize access to this routine.
8180 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008181int nfs4_proc_destroy_session(struct nfs4_session *session,
8182 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008183{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008184 struct rpc_message msg = {
8185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8186 .rpc_argp = session,
8187 .rpc_cred = cred,
8188 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008189 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008190
8191 dprintk("--> nfs4_proc_destroy_session\n");
8192
8193 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008194 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8195 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008196
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008197 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008198 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008199
8200 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008201 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008202 "Session has been destroyed regardless...\n", status);
8203
8204 dprintk("<-- nfs4_proc_destroy_session\n");
8205 return status;
8206}
8207
Trond Myklebust7b38c362012-05-23 13:23:31 -04008208/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008209 * Renew the cl_session lease.
8210 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008211struct nfs4_sequence_data {
8212 struct nfs_client *clp;
8213 struct nfs4_sequence_args args;
8214 struct nfs4_sequence_res res;
8215};
8216
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008217static void nfs41_sequence_release(void *data)
8218{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008219 struct nfs4_sequence_data *calldata = data;
8220 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008221
Elena Reshetova212bf412017-10-20 12:53:38 +03008222 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008223 nfs4_schedule_state_renewal(clp);
8224 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008225 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008226}
8227
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008228static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8229{
8230 switch(task->tk_status) {
8231 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008232 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8233 return -EAGAIN;
8234 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008235 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008236 }
8237 return 0;
8238}
8239
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008240static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008241{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008242 struct nfs4_sequence_data *calldata = data;
8243 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008244
Trond Myklebust14516c32010-07-31 14:29:06 -04008245 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8246 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008247
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008248 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008249 if (task->tk_status < 0) {
8250 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008251 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008252 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008253
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008254 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8255 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008256 return;
8257 }
8258 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008259 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008260out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008261 dprintk("<-- %s\n", __func__);
8262}
8263
8264static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8265{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008266 struct nfs4_sequence_data *calldata = data;
8267 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008268 struct nfs4_sequence_args *args;
8269 struct nfs4_sequence_res *res;
8270
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008271 args = task->tk_msg.rpc_argp;
8272 res = task->tk_msg.rpc_resp;
8273
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008274 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008275}
8276
8277static const struct rpc_call_ops nfs41_sequence_ops = {
8278 .rpc_call_done = nfs41_sequence_call_done,
8279 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008280 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008281};
8282
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008283static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8284 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008285 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008286 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008287{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008288 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008289 struct rpc_message msg = {
8290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8291 .rpc_cred = cred,
8292 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008293 struct rpc_task_setup task_setup_data = {
8294 .rpc_client = clp->cl_rpcclient,
8295 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008296 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008297 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008298 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008299 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008300
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008301 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008302 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008303 goto out_err;
8304
8305 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008306 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008307 if (calldata == NULL)
8308 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008309 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008310 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008311 if (is_privileged)
8312 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008313 msg.rpc_argp = &calldata->args;
8314 msg.rpc_resp = &calldata->res;
8315 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008316 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008317
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008318 ret = rpc_run_task(&task_setup_data);
8319 if (IS_ERR(ret))
8320 goto out_err;
8321 return ret;
8322out_put_clp:
8323 nfs_put_client(clp);
8324out_err:
8325 nfs41_release_slot(slot);
8326 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008327}
8328
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008329static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008330{
8331 struct rpc_task *task;
8332 int ret = 0;
8333
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008334 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008335 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008336 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008337 if (IS_ERR(task))
8338 ret = PTR_ERR(task);
8339 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008340 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008341 dprintk("<-- %s status=%d\n", __func__, ret);
8342 return ret;
8343}
8344
8345static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8346{
8347 struct rpc_task *task;
8348 int ret;
8349
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008350 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008351 if (IS_ERR(task)) {
8352 ret = PTR_ERR(task);
8353 goto out;
8354 }
8355 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008356 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008357 ret = task->tk_status;
8358 rpc_put_task(task);
8359out:
8360 dprintk("<-- %s status=%d\n", __func__, ret);
8361 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008362}
8363
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008364struct nfs4_reclaim_complete_data {
8365 struct nfs_client *clp;
8366 struct nfs41_reclaim_complete_args arg;
8367 struct nfs41_reclaim_complete_res res;
8368};
8369
8370static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8371{
8372 struct nfs4_reclaim_complete_data *calldata = data;
8373
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008374 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008375 &calldata->arg.seq_args,
8376 &calldata->res.seq_res,
8377 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008378}
8379
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008380static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8381{
8382 switch(task->tk_status) {
8383 case 0:
8384 case -NFS4ERR_COMPLETE_ALREADY:
8385 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8386 break;
8387 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008388 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008389 /* fall through */
8390 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008391 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008392 case -NFS4ERR_BADSESSION:
8393 case -NFS4ERR_DEADSESSION:
8394 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8395 nfs4_schedule_session_recovery(clp->cl_session,
8396 task->tk_status);
8397 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008398 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008399 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008400 }
8401 return 0;
8402}
8403
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008404static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8405{
8406 struct nfs4_reclaim_complete_data *calldata = data;
8407 struct nfs_client *clp = calldata->clp;
8408 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8409
8410 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008411 if (!nfs41_sequence_done(task, res))
8412 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008413
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008414 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008415 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8416 rpc_restart_call_prepare(task);
8417 return;
8418 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008419 dprintk("<-- %s\n", __func__);
8420}
8421
8422static void nfs4_free_reclaim_complete_data(void *data)
8423{
8424 struct nfs4_reclaim_complete_data *calldata = data;
8425
8426 kfree(calldata);
8427}
8428
8429static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8430 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8431 .rpc_call_done = nfs4_reclaim_complete_done,
8432 .rpc_release = nfs4_free_reclaim_complete_data,
8433};
8434
8435/*
8436 * Issue a global reclaim complete.
8437 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008438static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8439 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008440{
8441 struct nfs4_reclaim_complete_data *calldata;
8442 struct rpc_task *task;
8443 struct rpc_message msg = {
8444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008445 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008446 };
8447 struct rpc_task_setup task_setup_data = {
8448 .rpc_client = clp->cl_rpcclient,
8449 .rpc_message = &msg,
8450 .callback_ops = &nfs4_reclaim_complete_call_ops,
8451 .flags = RPC_TASK_ASYNC,
8452 };
8453 int status = -ENOMEM;
8454
8455 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008456 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008457 if (calldata == NULL)
8458 goto out;
8459 calldata->clp = clp;
8460 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008461
Chuck Levera9c92d62013-08-09 12:48:18 -04008462 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008463 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008464 msg.rpc_argp = &calldata->arg;
8465 msg.rpc_resp = &calldata->res;
8466 task_setup_data.callback_data = calldata;
8467 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008468 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008469 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008470 goto out;
8471 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008472 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008473 if (status == 0)
8474 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008475 rpc_put_task(task);
8476out:
8477 dprintk("<-- %s status=%d\n", __func__, status);
8478 return status;
8479}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008480
8481static void
8482nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8483{
8484 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008485 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008486
8487 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008488 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008489 &lgp->res.seq_res, task);
8490 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008491}
8492
8493static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8494{
8495 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008496
8497 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008498 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008499 dprintk("<-- %s\n", __func__);
8500}
8501
8502static int
8503nfs4_layoutget_handle_exception(struct rpc_task *task,
8504 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8505{
Trond Myklebustee314c22012-10-01 17:25:48 -07008506 struct inode *inode = lgp->args.inode;
8507 struct nfs_server *server = NFS_SERVER(inode);
8508 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008509 int nfs4err = task->tk_status;
8510 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008511 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008512
Boaz Harroshed7e5422014-01-22 20:34:54 +02008513 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008514
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008515 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008516 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008517 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008518
8519 /*
8520 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8521 * on the file. set tk_status to -ENODATA to tell upper layer to
8522 * retry go inband.
8523 */
8524 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008525 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008526 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008527 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008528 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8529 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8530 */
8531 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008532 status = -EOVERFLOW;
8533 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008534 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008535 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008536 * (or clients) writing to the same RAID stripe except when
8537 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008538 *
8539 * Treat it like we would RECALLCONFLICT -- we retry for a little
8540 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008541 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008542 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008543 if (lgp->args.minlength == 0) {
8544 status = -EOVERFLOW;
8545 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008546 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008547 status = -EBUSY;
8548 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008549 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008550 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008551 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008552 case -NFS4ERR_DELEG_REVOKED:
8553 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008554 case -NFS4ERR_EXPIRED:
8555 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008556 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008557 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008558 lo = NFS_I(inode)->layout;
8559 /* If the open stateid was bad, then recover it. */
8560 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008561 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008562 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008563 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008564 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008565 break;
8566 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008567
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008568 /*
8569 * Mark the bad layout state as invalid, then retry
8570 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008571 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008572 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008573 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008574 pnfs_free_lseg_list(&head);
8575 status = -EAGAIN;
8576 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008577 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008578
Trond Myklebust8ac09252017-01-23 22:44:12 -05008579 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008580 err = nfs4_handle_exception(server, nfs4err, exception);
8581 if (!status) {
8582 if (exception->retry)
8583 status = -EAGAIN;
8584 else
8585 status = err;
8586 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008587out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008588 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008589 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008590}
8591
Idan Kedar85541162012-08-02 11:47:10 +03008592static size_t max_response_pages(struct nfs_server *server)
8593{
8594 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8595 return nfs_page_array_len(0, max_resp_sz);
8596}
8597
8598static void nfs4_free_pages(struct page **pages, size_t size)
8599{
8600 int i;
8601
8602 if (!pages)
8603 return;
8604
8605 for (i = 0; i < size; i++) {
8606 if (!pages[i])
8607 break;
8608 __free_page(pages[i]);
8609 }
8610 kfree(pages);
8611}
8612
8613static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8614{
8615 struct page **pages;
8616 int i;
8617
8618 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8619 if (!pages) {
8620 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8621 return NULL;
8622 }
8623
8624 for (i = 0; i < size; i++) {
8625 pages[i] = alloc_page(gfp_flags);
8626 if (!pages[i]) {
8627 dprintk("%s: failed to allocate page\n", __func__);
8628 nfs4_free_pages(pages, size);
8629 return NULL;
8630 }
8631 }
8632
8633 return pages;
8634}
8635
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008636static void nfs4_layoutget_release(void *calldata)
8637{
8638 struct nfs4_layoutget *lgp = calldata;
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
8643 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008644 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008645 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008646 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008647 put_nfs_open_context(lgp->args.ctx);
8648 kfree(calldata);
8649 dprintk("<-- %s\n", __func__);
8650}
8651
8652static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8653 .rpc_call_prepare = nfs4_layoutget_prepare,
8654 .rpc_call_done = nfs4_layoutget_done,
8655 .rpc_release = nfs4_layoutget_release,
8656};
8657
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008658struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008659nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008660{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008661 struct inode *inode = lgp->args.inode;
8662 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008663 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008664 struct rpc_task *task;
8665 struct rpc_message msg = {
8666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8667 .rpc_argp = &lgp->args,
8668 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008669 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008670 };
8671 struct rpc_task_setup task_setup_data = {
8672 .rpc_client = server->client,
8673 .rpc_message = &msg,
8674 .callback_ops = &nfs4_layoutget_call_ops,
8675 .callback_data = lgp,
8676 .flags = RPC_TASK_ASYNC,
8677 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008678 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008679 struct nfs4_exception exception = {
8680 .inode = inode,
8681 .timeout = *timeout,
8682 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 int status = 0;
8684
8685 dprintk("--> %s\n", __func__);
8686
Peng Tao4bd5a982014-11-17 11:05:17 +08008687 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8688 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8689
Idan Kedar85541162012-08-02 11:47:10 +03008690 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8691 if (!lgp->args.layout.pages) {
8692 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008693 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008694 }
8695 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8696
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008697 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008698 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008699 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008700
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008701 task = rpc_run_task(&task_setup_data);
8702 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008703 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008704 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008705 if (status == 0) {
8706 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8707 *timeout = exception.timeout;
8708 }
8709
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008710 trace_nfs4_layoutget(lgp->args.ctx,
8711 &lgp->args.range,
8712 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008713 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008714 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008715
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008716 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8717 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008718 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008719 rpc_put_task(task);
8720 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008721 if (status)
8722 return ERR_PTR(status);
8723 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008724}
8725
Benny Halevycbe82602011-05-22 19:52:37 +03008726static void
8727nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8728{
8729 struct nfs4_layoutreturn *lrp = calldata;
8730
8731 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008732 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008733 &lrp->args.seq_args,
8734 &lrp->res.seq_res,
8735 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008736}
8737
8738static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8739{
8740 struct nfs4_layoutreturn *lrp = calldata;
8741 struct nfs_server *server;
8742
8743 dprintk("--> %s\n", __func__);
8744
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008745 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008746 return;
8747
8748 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008749 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008750 case -NFS4ERR_OLD_STATEID:
8751 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8752 lrp->args.inode))
8753 goto out_restart;
8754 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008755 default:
8756 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008757 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008758 case 0:
8759 break;
8760 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008761 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008762 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008763 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008764 }
Benny Halevycbe82602011-05-22 19:52:37 +03008765 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008766 return;
8767out_restart:
8768 task->tk_status = 0;
8769 nfs4_sequence_free_slot(&lrp->res.seq_res);
8770 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008771}
8772
8773static void nfs4_layoutreturn_release(void *calldata)
8774{
8775 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008776 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008777
8778 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008779 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008780 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008781 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008782 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8783 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008784 pnfs_put_layout_hdr(lrp->args.layout);
8785 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008786 kfree(calldata);
8787 dprintk("<-- %s\n", __func__);
8788}
8789
8790static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8791 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8792 .rpc_call_done = nfs4_layoutreturn_done,
8793 .rpc_release = nfs4_layoutreturn_release,
8794};
8795
Peng Tao6c166052014-11-17 09:30:40 +08008796int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008797{
8798 struct rpc_task *task;
8799 struct rpc_message msg = {
8800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8801 .rpc_argp = &lrp->args,
8802 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008803 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008804 };
8805 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008806 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008807 .rpc_message = &msg,
8808 .callback_ops = &nfs4_layoutreturn_call_ops,
8809 .callback_data = lrp,
8810 };
Peng Tao6c166052014-11-17 09:30:40 +08008811 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008812
Andrew Elble99ade3c2015-12-02 09:39:51 -05008813 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8814 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8815 &task_setup_data.rpc_client, &msg);
8816
Benny Halevycbe82602011-05-22 19:52:37 +03008817 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008818 if (!sync) {
8819 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8820 if (!lrp->inode) {
8821 nfs4_layoutreturn_release(lrp);
8822 return -EAGAIN;
8823 }
8824 task_setup_data.flags |= RPC_TASK_ASYNC;
8825 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008826 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008827 task = rpc_run_task(&task_setup_data);
8828 if (IS_ERR(task))
8829 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008830 if (sync)
8831 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008832 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008833 dprintk("<-- %s status=%d\n", __func__, status);
8834 rpc_put_task(task);
8835 return status;
8836}
8837
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008838static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008839_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8840 struct pnfs_device *pdev,
8841 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008842{
8843 struct nfs4_getdeviceinfo_args args = {
8844 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008845 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8846 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008847 };
8848 struct nfs4_getdeviceinfo_res res = {
8849 .pdev = pdev,
8850 };
8851 struct rpc_message msg = {
8852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8853 .rpc_argp = &args,
8854 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008855 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008856 };
8857 int status;
8858
8859 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008860 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008861 if (res.notification & ~args.notify_types)
8862 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008863 if (res.notification != args.notify_types)
8864 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008865
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008866 dprintk("<-- %s status=%d\n", __func__, status);
8867
8868 return status;
8869}
8870
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008871int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8872 struct pnfs_device *pdev,
8873 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008874{
8875 struct nfs4_exception exception = { };
8876 int err;
8877
8878 do {
8879 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008880 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008881 &exception);
8882 } while (exception.retry);
8883 return err;
8884}
8885EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8886
Andy Adamson863a3c62011-03-23 13:27:54 +00008887static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8888{
8889 struct nfs4_layoutcommit_data *data = calldata;
8890 struct nfs_server *server = NFS_SERVER(data->args.inode);
8891
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008892 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008893 &data->args.seq_args,
8894 &data->res.seq_res,
8895 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008896}
8897
8898static void
8899nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8900{
8901 struct nfs4_layoutcommit_data *data = calldata;
8902 struct nfs_server *server = NFS_SERVER(data->args.inode);
8903
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008904 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008905 return;
8906
8907 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008908 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8909 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8910 case -NFS4ERR_BADLAYOUT: /* no layout */
8911 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008912 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008913 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008914 break;
8915 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008916 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008917 rpc_restart_call_prepare(task);
8918 return;
8919 }
8920 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008921}
8922
8923static void nfs4_layoutcommit_release(void *calldata)
8924{
8925 struct nfs4_layoutcommit_data *data = calldata;
8926
Andy Adamsondb29c082011-07-30 20:52:38 -04008927 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008928 nfs_post_op_update_inode_force_wcc(data->args.inode,
8929 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008930 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008931 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008932 kfree(data);
8933}
8934
8935static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8936 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8937 .rpc_call_done = nfs4_layoutcommit_done,
8938 .rpc_release = nfs4_layoutcommit_release,
8939};
8940
8941int
Andy Adamsonef311532011-03-12 02:58:10 -05008942nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008943{
8944 struct rpc_message msg = {
8945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8946 .rpc_argp = &data->args,
8947 .rpc_resp = &data->res,
8948 .rpc_cred = data->cred,
8949 };
8950 struct rpc_task_setup task_setup_data = {
8951 .task = &data->task,
8952 .rpc_client = NFS_CLIENT(data->args.inode),
8953 .rpc_message = &msg,
8954 .callback_ops = &nfs4_layoutcommit_ops,
8955 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008956 };
8957 struct rpc_task *task;
8958 int status = 0;
8959
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008960 dprintk("NFS: initiating layoutcommit call. sync %d "
8961 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008962 data->args.lastbytewritten,
8963 data->args.inode->i_ino);
8964
Trond Myklebust472e2592015-02-05 16:50:30 -05008965 if (!sync) {
8966 data->inode = nfs_igrab_and_active(data->args.inode);
8967 if (data->inode == NULL) {
8968 nfs4_layoutcommit_release(data);
8969 return -EAGAIN;
8970 }
8971 task_setup_data.flags = RPC_TASK_ASYNC;
8972 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008973 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008974 task = rpc_run_task(&task_setup_data);
8975 if (IS_ERR(task))
8976 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008977 if (sync)
8978 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008979 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008980 dprintk("%s: status %d\n", __func__, status);
8981 rpc_put_task(task);
8982 return status;
8983}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008984
Andy Adamson97431202013-08-08 10:57:56 -04008985/**
8986 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8987 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8988 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008989static int
8990_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008991 struct nfs_fsinfo *info,
8992 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008993{
8994 struct nfs41_secinfo_no_name_args args = {
8995 .style = SECINFO_STYLE_CURRENT_FH,
8996 };
8997 struct nfs4_secinfo_res res = {
8998 .flavors = flavors,
8999 };
9000 struct rpc_message msg = {
9001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9002 .rpc_argp = &args,
9003 .rpc_resp = &res,
9004 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009005 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009006 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009007 int status;
9008
9009 if (use_integrity) {
9010 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009011 cred = nfs4_get_clid_cred(server->nfs_client);
9012 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009013 }
9014
9015 dprintk("--> %s\n", __func__);
9016 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9017 &res.seq_res, 0);
9018 dprintk("<-- %s status=%d\n", __func__, status);
9019
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009020 if (cred)
9021 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009022
9023 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009024}
9025
9026static int
9027nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9028 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9029{
9030 struct nfs4_exception exception = { };
9031 int err;
9032 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009033 /* first try using integrity protection */
9034 err = -NFS4ERR_WRONGSEC;
9035
9036 /* try to use integrity protection with machine cred */
9037 if (_nfs4_is_integrity_protected(server->nfs_client))
9038 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9039 flavors, true);
9040
9041 /*
9042 * if unable to use integrity protection, or SECINFO with
9043 * integrity protection returns NFS4ERR_WRONGSEC (which is
9044 * disallowed by spec, but exists in deployed servers) use
9045 * the current filesystem's rpc_client and the user cred.
9046 */
9047 if (err == -NFS4ERR_WRONGSEC)
9048 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9049 flavors, false);
9050
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009051 switch (err) {
9052 case 0:
9053 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009054 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009055 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009056 default:
9057 err = nfs4_handle_exception(server, err, &exception);
9058 }
9059 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009060out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009061 return err;
9062}
9063
9064static int
9065nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9066 struct nfs_fsinfo *info)
9067{
9068 int err;
9069 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009070 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009071 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009072 struct nfs4_secinfo4 *secinfo;
9073 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009074
9075 page = alloc_page(GFP_KERNEL);
9076 if (!page) {
9077 err = -ENOMEM;
9078 goto out;
9079 }
9080
9081 flavors = page_address(page);
9082 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9083
9084 /*
9085 * Fall back on "guess and check" method if
9086 * the server doesn't support SECINFO_NO_NAME
9087 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009088 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009089 err = nfs4_find_root_sec(server, fhandle, info);
9090 goto out_freepage;
9091 }
9092 if (err)
9093 goto out_freepage;
9094
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009095 for (i = 0; i < flavors->num_flavors; i++) {
9096 secinfo = &flavors->flavors[i];
9097
9098 switch (secinfo->flavor) {
9099 case RPC_AUTH_NULL:
9100 case RPC_AUTH_UNIX:
9101 case RPC_AUTH_GSS:
9102 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9103 &secinfo->flavor_info);
9104 break;
9105 default:
9106 flavor = RPC_AUTH_MAXFLAVOR;
9107 break;
9108 }
9109
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009110 if (!nfs_auth_info_match(&server->auth_info, flavor))
9111 flavor = RPC_AUTH_MAXFLAVOR;
9112
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009113 if (flavor != RPC_AUTH_MAXFLAVOR) {
9114 err = nfs4_lookup_root_sec(server, fhandle,
9115 info, flavor);
9116 if (!err)
9117 break;
9118 }
9119 }
9120
9121 if (flavor == RPC_AUTH_MAXFLAVOR)
9122 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009123
9124out_freepage:
9125 put_page(page);
9126 if (err == -EACCES)
9127 return -EPERM;
9128out:
9129 return err;
9130}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009131
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009132static int _nfs41_test_stateid(struct nfs_server *server,
9133 nfs4_stateid *stateid,
9134 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009135{
9136 int status;
9137 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009138 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009139 };
9140 struct nfs41_test_stateid_res res;
9141 struct rpc_message msg = {
9142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9143 .rpc_argp = &args,
9144 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009145 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009146 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009147 struct rpc_clnt *rpc_client = server->client;
9148
9149 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9150 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009151
Chuck Lever38527b12012-07-11 16:30:23 -04009152 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009153 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009154 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009155 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009156 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009157 if (status != NFS_OK) {
9158 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009159 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009160 }
9161 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009162 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009163}
9164
Trond Myklebust43912bb2016-09-22 13:38:56 -04009165static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9166 int err, struct nfs4_exception *exception)
9167{
9168 exception->retry = 0;
9169 switch(err) {
9170 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009171 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009172 nfs4_handle_exception(server, err, exception);
9173 break;
9174 case -NFS4ERR_BADSESSION:
9175 case -NFS4ERR_BADSLOT:
9176 case -NFS4ERR_BAD_HIGH_SLOT:
9177 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9178 case -NFS4ERR_DEADSESSION:
9179 nfs4_do_handle_exception(server, err, exception);
9180 }
9181}
9182
Chuck Lever38527b12012-07-11 16:30:23 -04009183/**
9184 * nfs41_test_stateid - perform a TEST_STATEID operation
9185 *
9186 * @server: server / transport on which to perform the operation
9187 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009188 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009189 *
9190 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9191 * Otherwise a negative NFS4ERR value is returned if the operation
9192 * failed or the state ID is not currently valid.
9193 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009194static int nfs41_test_stateid(struct nfs_server *server,
9195 nfs4_stateid *stateid,
9196 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009197{
9198 struct nfs4_exception exception = { };
9199 int err;
9200 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009201 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009202 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009203 } while (exception.retry);
9204 return err;
9205}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009206
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009207struct nfs_free_stateid_data {
9208 struct nfs_server *server;
9209 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009210 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009211};
9212
9213static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9214{
9215 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009216 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009217 &data->args.seq_args,
9218 &data->res.seq_res,
9219 task);
9220}
9221
9222static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9223{
9224 struct nfs_free_stateid_data *data = calldata;
9225
9226 nfs41_sequence_done(task, &data->res.seq_res);
9227
9228 switch (task->tk_status) {
9229 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009230 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009231 rpc_restart_call_prepare(task);
9232 }
9233}
9234
9235static void nfs41_free_stateid_release(void *calldata)
9236{
9237 kfree(calldata);
9238}
9239
Trond Myklebust17f26b12013-08-21 15:48:42 -04009240static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009241 .rpc_call_prepare = nfs41_free_stateid_prepare,
9242 .rpc_call_done = nfs41_free_stateid_done,
9243 .rpc_release = nfs41_free_stateid_release,
9244};
9245
9246static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009247 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009248 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009249 bool privileged)
9250{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009251 struct rpc_message msg = {
9252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009253 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009254 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009255 struct rpc_task_setup task_setup = {
9256 .rpc_client = server->client,
9257 .rpc_message = &msg,
9258 .callback_ops = &nfs41_free_stateid_ops,
9259 .flags = RPC_TASK_ASYNC,
9260 };
9261 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009262
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009263 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9264 &task_setup.rpc_client, &msg);
9265
Chuck Lever38527b12012-07-11 16:30:23 -04009266 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009267 data = kmalloc(sizeof(*data), GFP_NOFS);
9268 if (!data)
9269 return ERR_PTR(-ENOMEM);
9270 data->server = server;
9271 nfs4_stateid_copy(&data->args.stateid, stateid);
9272
9273 task_setup.callback_data = data;
9274
9275 msg.rpc_argp = &data->args;
9276 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009277 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009278 if (privileged)
9279 nfs4_set_sequence_privileged(&data->args.seq_args);
9280
9281 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009282}
9283
Chuck Lever38527b12012-07-11 16:30:23 -04009284/**
9285 * nfs41_free_stateid - perform a FREE_STATEID operation
9286 *
9287 * @server: server / transport on which to perform the operation
9288 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009289 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009290 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009291 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009292 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009293 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009294static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009295 const nfs4_stateid *stateid,
9296 struct rpc_cred *cred,
9297 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009298{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009299 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009300
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009301 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009302 if (IS_ERR(task))
9303 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009304 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009305 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009306}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009307
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009308static void
9309nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009310{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009311 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009312
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009313 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009314 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009315}
9316
Trond Myklebust36281ca2012-03-04 18:13:56 -05009317static bool nfs41_match_stateid(const nfs4_stateid *s1,
9318 const nfs4_stateid *s2)
9319{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009320 if (s1->type != s2->type)
9321 return false;
9322
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009323 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009324 return false;
9325
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009326 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009327 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009328
Anna Schumaker045c5512017-01-11 16:59:48 -05009329 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009330}
9331
Andy Adamson557134a2009-04-01 09:21:53 -04009332#endif /* CONFIG_NFS_V4_1 */
9333
Trond Myklebust36281ca2012-03-04 18:13:56 -05009334static bool nfs4_match_stateid(const nfs4_stateid *s1,
9335 const nfs4_stateid *s2)
9336{
Trond Myklebustf597c532012-03-04 18:13:56 -05009337 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009338}
9339
9340
Trond Myklebust17280172012-03-11 13:11:00 -04009341static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009342 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009343 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 .recover_open = nfs4_open_reclaim,
9345 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009346 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009347 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348};
9349
Andy Adamson591d71c2009-04-01 09:22:47 -04009350#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009351static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009352 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9353 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9354 .recover_open = nfs4_open_reclaim,
9355 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009356 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009357 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009358 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009359};
9360#endif /* CONFIG_NFS_V4_1 */
9361
Trond Myklebust17280172012-03-11 13:11:00 -04009362static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009363 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009364 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009365 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009366 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009367 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368};
9369
Andy Adamson591d71c2009-04-01 09:22:47 -04009370#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009371static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009372 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9373 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009374 .recover_open = nfs41_open_expired,
9375 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009376 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009377};
9378#endif /* CONFIG_NFS_V4_1 */
9379
Trond Myklebust17280172012-03-11 13:11:00 -04009380static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009381 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009382 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009383 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009384};
9385
9386#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009387static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009388 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009389 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009390 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009391};
9392#endif
9393
Chuck Leverec011fe2013-10-17 14:12:39 -04009394static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009395 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009396 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009397};
9398
9399#if defined(CONFIG_NFS_V4_1)
9400static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009401 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009402 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009403};
9404#endif /* CONFIG_NFS_V4_1 */
9405
Trond Myklebust97dc1352010-06-16 09:52:26 -04009406static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9407 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009408 .init_caps = NFS_CAP_READDIRPLUS
9409 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009410 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009411 .init_client = nfs40_init_client,
9412 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009413 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009414 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009415 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009416 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009417 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009418 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009419 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9420 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9421 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009422 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009423};
9424
9425#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009426static struct nfs_seqid *
9427nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9428{
9429 return NULL;
9430}
9431
Trond Myklebust97dc1352010-06-16 09:52:26 -04009432static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9433 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009434 .init_caps = NFS_CAP_READDIRPLUS
9435 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009436 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009437 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009438 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009439 .init_client = nfs41_init_client,
9440 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009441 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009442 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009443 .free_lock_state = nfs41_free_lock_state,
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,
Chuck Lever9915ea72013-08-09 12:48:27 -04009447 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009448 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9449 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9450 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009451 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009452};
9453#endif
9454
Steve Dickson42c2c422013-05-22 12:50:38 -04009455#if defined(CONFIG_NFS_V4_2)
9456static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9457 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009458 .init_caps = NFS_CAP_READDIRPLUS
9459 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009460 | NFS_CAP_POSIX_LOCK
9461 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009462 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009463 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009464 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009465 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009466 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009467 | NFS_CAP_LAYOUTSTATS
9468 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009469 .init_client = nfs41_init_client,
9470 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009471 .match_stateid = nfs41_match_stateid,
9472 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009473 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009474 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009475 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009476 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009477 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009478 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9479 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9480 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009481 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009482};
9483#endif
9484
Trond Myklebust97dc1352010-06-16 09:52:26 -04009485const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9486 [0] = &nfs_v4_0_minor_ops,
9487#if defined(CONFIG_NFS_V4_1)
9488 [1] = &nfs_v4_1_minor_ops,
9489#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009490#if defined(CONFIG_NFS_V4_2)
9491 [2] = &nfs_v4_2_minor_ops,
9492#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009493};
9494
Trond Myklebust13997822016-07-24 17:10:52 -04009495static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009496{
9497 ssize_t error, error2;
9498
9499 error = generic_listxattr(dentry, list, size);
9500 if (error < 0)
9501 return error;
9502 if (list) {
9503 list += error;
9504 size -= error;
9505 }
9506
9507 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9508 if (error2 < 0)
9509 return error2;
9510 return error + error2;
9511}
9512
Trond Myklebust17f26b12013-08-21 15:48:42 -04009513static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009514 .create = nfs_create,
9515 .lookup = nfs_lookup,
9516 .atomic_open = nfs_atomic_open,
9517 .link = nfs_link,
9518 .unlink = nfs_unlink,
9519 .symlink = nfs_symlink,
9520 .mkdir = nfs_mkdir,
9521 .rmdir = nfs_rmdir,
9522 .mknod = nfs_mknod,
9523 .rename = nfs_rename,
9524 .permission = nfs_permission,
9525 .getattr = nfs_getattr,
9526 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009527 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009528};
9529
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009530static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009531 .permission = nfs_permission,
9532 .getattr = nfs_getattr,
9533 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009534 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009535};
9536
David Howells509de812006-08-22 20:06:11 -04009537const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538 .version = 4, /* protocol version */
9539 .dentry_ops = &nfs4_dentry_operations,
9540 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009541 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009542 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009543 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009544 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009545 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 .getattr = nfs4_proc_getattr,
9547 .setattr = nfs4_proc_setattr,
9548 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009549 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550 .access = nfs4_proc_access,
9551 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 .create = nfs4_proc_create,
9553 .remove = nfs4_proc_remove,
9554 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009555 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009557 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009558 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009559 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560 .link = nfs4_proc_link,
9561 .symlink = nfs4_proc_symlink,
9562 .mkdir = nfs4_proc_mkdir,
9563 .rmdir = nfs4_proc_remove,
9564 .readdir = nfs4_proc_readdir,
9565 .mknod = nfs4_proc_mknod,
9566 .statfs = nfs4_proc_statfs,
9567 .fsinfo = nfs4_proc_fsinfo,
9568 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009569 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009571 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009572 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009573 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009574 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009575 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009577 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009578 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009580 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009581 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009582 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009583 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009584 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009585 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009586 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009587 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009588 .create_server = nfs4_create_server,
9589 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590};
9591
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009592static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009593 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009594 .list = nfs4_xattr_list_nfs4_acl,
9595 .get = nfs4_xattr_get_nfs4_acl,
9596 .set = nfs4_xattr_set_nfs4_acl,
9597};
9598
9599const struct xattr_handler *nfs4_xattr_handlers[] = {
9600 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009601#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9602 &nfs4_xattr_nfs4_label_handler,
9603#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009604 NULL
9605};
9606
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607/*
9608 * Local variables:
9609 * c-basic-offset: 8
9610 * End:
9611 */