blob: 2c9c225796106ac4eeb8e32f25bf1974fed60e96 [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 Schumaker9dd91072017-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 Schumaker9dd91072017-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
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001544 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1545 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001546 nfs4_stateid_copy(freeme, &state->open_stateid);
1547 nfs_test_and_clear_all_open_stateid(state);
1548 }
1549
Trond Myklebuste999e802014-02-10 18:20:47 -05001550 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1551 nfs4_stateid_copy(&state->stateid, stateid);
1552 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001553 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001555}
1556
Trond Myklebustc9399f22017-11-06 15:28:01 -05001557static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001558 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 fmode_t fmode,
1560 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001562 /*
1563 * Protect the call to nfs4_state_set_mode_locked and
1564 * serialise the stateid update
1565 */
1566 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1568 switch (fmode) {
1569 case FMODE_READ:
1570 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_WRITE:
1573 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1574 break;
1575 case FMODE_READ|FMODE_WRITE:
1576 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001577 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001579 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001580}
1581
1582static void nfs_state_set_delegation(struct nfs4_state *state,
1583 const nfs4_stateid *deleg_stateid,
1584 fmode_t fmode)
1585{
1586 /*
1587 * Protect the call to nfs4_state_set_mode_locked and
1588 * serialise the stateid update
1589 */
1590 write_seqlock(&state->seqlock);
1591 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1592 set_bit(NFS_DELEGATED_STATE, &state->flags);
1593 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Trond Myklebust1393d962016-09-22 13:39:13 -04001596static int update_open_stateid(struct nfs4_state *state,
1597 const nfs4_stateid *open_stateid,
1598 const nfs4_stateid *delegation,
1599 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001600{
Trond Myklebust1393d962016-09-22 13:39:13 -04001601 struct nfs_server *server = NFS_SERVER(state->inode);
1602 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001603 struct nfs_inode *nfsi = NFS_I(state->inode);
1604 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001605 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001606 int ret = 0;
1607
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001609
1610 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001611 spin_lock(&state->owner->so_lock);
1612 if (open_stateid != NULL) {
1613 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1614 ret = 1;
1615 }
1616
Trond Myklebust34310432008-12-23 15:21:38 -05001617 deleg_cur = rcu_dereference(nfsi->delegation);
1618 if (deleg_cur == NULL)
1619 goto no_delegation;
1620
1621 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001622 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001623 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001624 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001625 goto no_delegation_unlock;
1626
1627 if (delegation == NULL)
1628 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001629 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001630 goto no_delegation_unlock;
1631
Trond Myklebustb7391f42008-12-23 15:21:52 -05001632 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001633 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001634 ret = 1;
1635no_delegation_unlock:
1636 spin_unlock(&deleg_cur->lock);
1637no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001638 if (ret)
1639 update_open_stateflags(state, fmode);
1640 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001641 rcu_read_unlock();
1642
Trond Myklebust4f14c192014-02-12 19:15:06 -05001643 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001644 nfs4_schedule_state_manager(clp);
1645 if (freeme.type != 0)
1646 nfs4_test_and_free_stateid(server, &freeme,
1647 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001648
1649 return ret;
1650}
1651
Trond Myklebust39071e62015-01-24 15:07:56 -05001652static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1653 const nfs4_stateid *stateid)
1654{
1655 struct nfs4_state *state = lsp->ls_state;
1656 bool ret = false;
1657
1658 spin_lock(&state->state_lock);
1659 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1660 goto out_noupdate;
1661 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1662 goto out_noupdate;
1663 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1664 ret = true;
1665out_noupdate:
1666 spin_unlock(&state->state_lock);
1667 return ret;
1668}
Trond Myklebust34310432008-12-23 15:21:38 -05001669
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001670static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671{
1672 struct nfs_delegation *delegation;
1673
1674 rcu_read_lock();
1675 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001676 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001677 rcu_read_unlock();
1678 return;
1679 }
1680 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001681 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682}
1683
Trond Myklebust6ee41262007-07-08 14:11:36 -04001684static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001685{
1686 struct nfs4_state *state = opendata->state;
1687 struct nfs_inode *nfsi = NFS_I(state->inode);
1688 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001689 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001690 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001691 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 nfs4_stateid stateid;
1693 int ret = -EAGAIN;
1694
Trond Myklebustaac00a82007-07-05 19:02:21 -04001695 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001696 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001697 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001698 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001699 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001700 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001701 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001702 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001703 rcu_read_lock();
1704 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001705 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001706 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001707 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001708 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001709 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001710 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001711 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001712 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001713 if (!opendata->is_recover) {
1714 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1715 if (ret != 0)
1716 goto out;
1717 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001718 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001719
1720 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001721 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001722 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001724out:
1725 return ERR_PTR(ret);
1726out_return_state:
1727 atomic_inc(&state->count);
1728 return state;
1729}
1730
Andy Adamsone23008e2012-10-02 21:07:32 -04001731static void
1732nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1733{
1734 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1735 struct nfs_delegation *delegation;
1736 int delegation_flags = 0;
1737
1738 rcu_read_lock();
1739 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1740 if (delegation)
1741 delegation_flags = delegation->flags;
1742 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001743 switch (data->o_arg.claim) {
1744 default:
1745 break;
1746 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1747 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001748 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1749 "returning a delegation for "
1750 "OPEN(CLAIM_DELEGATE_CUR)\n",
1751 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001752 return;
1753 }
1754 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001755 nfs_inode_set_delegation(state->inode,
1756 data->owner->so_cred,
1757 &data->o_res);
1758 else
1759 nfs_inode_reclaim_delegation(state->inode,
1760 data->owner->so_cred,
1761 &data->o_res);
1762}
1763
1764/*
1765 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1766 * and update the nfs4_state.
1767 */
1768static struct nfs4_state *
1769_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1770{
1771 struct inode *inode = data->state->inode;
1772 struct nfs4_state *state = data->state;
1773 int ret;
1774
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001775 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001776 if (data->rpc_status)
1777 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001778 /* cached opens have already been processed */
1779 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001780 }
1781
Andy Adamsone23008e2012-10-02 21:07:32 -04001782 ret = nfs_refresh_inode(inode, &data->f_attr);
1783 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001784 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001785
1786 if (data->o_res.delegation_type != 0)
1787 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001788update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001789 update_open_stateid(state, &data->o_res.stateid, NULL,
1790 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001791 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001792
1793 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001794}
1795
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001796static struct inode *
1797nfs4_opendata_get_inode(struct nfs4_opendata *data)
1798{
1799 struct inode *inode;
1800
1801 switch (data->o_arg.claim) {
1802 case NFS4_OPEN_CLAIM_NULL:
1803 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1804 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1805 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1806 return ERR_PTR(-EAGAIN);
1807 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1808 &data->f_attr, data->f_label);
1809 break;
1810 default:
1811 inode = d_inode(data->dentry);
1812 ihold(inode);
1813 nfs_refresh_inode(inode, &data->f_attr);
1814 }
1815 return inode;
1816}
1817
Andy Adamsone23008e2012-10-02 21:07:32 -04001818static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001819nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1820{
1821 struct nfs4_state *state;
1822 struct inode *inode;
1823
1824 inode = nfs4_opendata_get_inode(data);
1825 if (IS_ERR(inode))
1826 return ERR_CAST(inode);
1827 if (data->state != NULL && data->state->inode == inode) {
1828 state = data->state;
1829 atomic_inc(&state->count);
1830 } else
1831 state = nfs4_get_open_state(inode, data->owner);
1832 iput(inode);
1833 if (state == NULL)
1834 state = ERR_PTR(-ENOMEM);
1835 return state;
1836}
1837
Andy Adamsone23008e2012-10-02 21:07:32 -04001838static struct nfs4_state *
1839_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001840{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001841 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001842
Trond Myklebustaac00a82007-07-05 19:02:21 -04001843 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001844 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001845 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001846 goto out;
1847 }
1848
Trond Myklebust75e8c482017-08-08 10:38:07 -04001849 state = nfs4_opendata_find_nfs4_state(data);
1850 if (IS_ERR(state))
1851 goto out;
1852
Andy Adamsone23008e2012-10-02 21:07:32 -04001853 if (data->o_res.delegation_type != 0)
1854 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001855 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001856 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001857out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001858 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001859 return state;
1860}
1861
Andy Adamsone23008e2012-10-02 21:07:32 -04001862static struct nfs4_state *
1863nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1864{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001865 struct nfs4_state *ret;
1866
Andy Adamsone23008e2012-10-02 21:07:32 -04001867 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001868 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1869 else
1870 ret = _nfs4_opendata_to_nfs4_state(data);
1871 nfs4_sequence_free_slot(&data->o_res.seq_res);
1872 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001873}
1874
Trond Myklebust864472e2006-01-03 09:55:15 +01001875static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1876{
1877 struct nfs_inode *nfsi = NFS_I(state->inode);
1878 struct nfs_open_context *ctx;
1879
1880 spin_lock(&state->inode->i_lock);
1881 list_for_each_entry(ctx, &nfsi->open_files, list) {
1882 if (ctx->state != state)
1883 continue;
1884 get_nfs_open_context(ctx);
1885 spin_unlock(&state->inode->i_lock);
1886 return ctx;
1887 }
1888 spin_unlock(&state->inode->i_lock);
1889 return ERR_PTR(-ENOENT);
1890}
1891
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001892static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1893 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001894{
1895 struct nfs4_opendata *opendata;
1896
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001897 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001898 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001899 if (opendata == NULL)
1900 return ERR_PTR(-ENOMEM);
1901 opendata->state = state;
1902 atomic_inc(&state->count);
1903 return opendata;
1904}
1905
Trond Myklebust24311f82015-09-20 10:50:17 -04001906static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1907 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001908{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001909 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001910 int ret;
1911
Trond Myklebust24311f82015-09-20 10:50:17 -04001912 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001913 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001914 opendata->o_arg.open_flags = 0;
1915 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001916 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1917 NFS_SB(opendata->dentry->d_sb),
1918 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001919 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1920 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1921 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001922 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001923 if (ret != 0)
1924 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001925 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001926 if (IS_ERR(newstate))
1927 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001928 if (newstate != opendata->state)
1929 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001930 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001931 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001932}
1933
1934static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1935{
Trond Myklebust864472e2006-01-03 09:55:15 +01001936 int ret;
1937
Trond Myklebust4f14c192014-02-12 19:15:06 -05001938 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1939 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1940 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1941 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001943 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001944 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001945 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001946 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1947 if (ret != 0)
1948 return ret;
1949 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1950 if (ret != 0)
1951 return ret;
1952 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1953 if (ret != 0)
1954 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001955 /*
1956 * We may have performed cached opens for all three recoveries.
1957 * Check if we need to update the current stateid.
1958 */
1959 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001960 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001961 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001962 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001963 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001964 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001965 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001966 return 0;
1967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/*
1970 * OPEN_RECLAIM:
1971 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001973static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001975 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001976 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001977 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int status;
1979
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001980 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1981 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001982 if (IS_ERR(opendata))
1983 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001984 rcu_read_lock();
1985 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001986 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001987 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001988 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001989 opendata->o_arg.u.delegation_type = delegation_type;
1990 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001991 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return status;
1993}
1994
Trond Myklebust539cd032007-06-05 11:46:42 -04001995static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 struct nfs_server *server = NFS_SERVER(state->inode);
1998 struct nfs4_exception exception = { };
1999 int err;
2000 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002001 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002002 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002003 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2004 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002005 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002006 break;
2007 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 } while (exception.retry);
2009 return err;
2010}
2011
Trond Myklebust864472e2006-01-03 09:55:15 +01002012static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2013{
2014 struct nfs_open_context *ctx;
2015 int ret;
2016
2017 ctx = nfs4_state_find_open_context(state);
2018 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002019 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002020 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002021 put_nfs_open_context(ctx);
2022 return ret;
2023}
2024
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002025static 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 -07002026{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002027 switch (err) {
2028 default:
2029 printk(KERN_ERR "NFS: %s: unhandled error "
2030 "%d.\n", __func__, err);
2031 case 0:
2032 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002033 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002034 case -ESTALE:
2035 break;
2036 case -NFS4ERR_BADSESSION:
2037 case -NFS4ERR_BADSLOT:
2038 case -NFS4ERR_BAD_HIGH_SLOT:
2039 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2040 case -NFS4ERR_DEADSESSION:
2041 set_bit(NFS_DELEGATED_STATE, &state->flags);
2042 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2043 return -EAGAIN;
2044 case -NFS4ERR_STALE_CLIENTID:
2045 case -NFS4ERR_STALE_STATEID:
2046 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002047 /* Don't recall a delegation if it was lost */
2048 nfs4_schedule_lease_recovery(server->nfs_client);
2049 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002050 case -NFS4ERR_MOVED:
2051 nfs4_schedule_migration_recovery(server);
2052 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002053 case -NFS4ERR_LEASE_MOVED:
2054 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2055 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002056 case -NFS4ERR_DELEG_REVOKED:
2057 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002058 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002059 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002060 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002061 nfs_inode_find_state_and_recover(state->inode,
2062 stateid);
2063 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002064 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002065 case -NFS4ERR_DELAY:
2066 case -NFS4ERR_GRACE:
2067 set_bit(NFS_DELEGATED_STATE, &state->flags);
2068 ssleep(1);
2069 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002070 case -ENOMEM:
2071 case -NFS4ERR_DENIED:
2072 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2073 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return err;
2076}
2077
Trond Myklebust24311f82015-09-20 10:50:17 -04002078int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2079 struct nfs4_state *state, const nfs4_stateid *stateid,
2080 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002081{
2082 struct nfs_server *server = NFS_SERVER(state->inode);
2083 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002084 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002085
2086 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2087 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2088 if (IS_ERR(opendata))
2089 return PTR_ERR(opendata);
2090 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002091 write_seqlock(&state->seqlock);
2092 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2093 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002094 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2095 switch (type & (FMODE_READ|FMODE_WRITE)) {
2096 case FMODE_READ|FMODE_WRITE:
2097 case FMODE_WRITE:
2098 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2099 if (err)
2100 break;
2101 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2102 if (err)
2103 break;
2104 case FMODE_READ:
2105 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2106 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002107 nfs4_opendata_put(opendata);
2108 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2109}
2110
Chuck Leverbe05c862013-08-09 12:49:47 -04002111static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2112{
2113 struct nfs4_opendata *data = calldata;
2114
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002115 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2116 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002117}
2118
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002119static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2120{
2121 struct nfs4_opendata *data = calldata;
2122
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002123 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002124
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002125 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002126 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002127 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002128 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002129 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002130 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002131 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002132}
2133
2134static void nfs4_open_confirm_release(void *calldata)
2135{
2136 struct nfs4_opendata *data = calldata;
2137 struct nfs4_state *state = NULL;
2138
2139 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002140 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002141 goto out_free;
2142 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002143 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002144 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002145 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002146 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002147 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002148out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002149 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002150}
2151
2152static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002153 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002154 .rpc_call_done = nfs4_open_confirm_done,
2155 .rpc_release = nfs4_open_confirm_release,
2156};
2157
2158/*
2159 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2160 */
2161static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2162{
David Howells2b0143b2015-03-17 22:25:59 +00002163 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002164 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002165 struct rpc_message msg = {
2166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2167 .rpc_argp = &data->c_arg,
2168 .rpc_resp = &data->c_res,
2169 .rpc_cred = data->owner->so_cred,
2170 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002171 struct rpc_task_setup task_setup_data = {
2172 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002173 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002174 .callback_ops = &nfs4_open_confirm_ops,
2175 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002176 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002177 .flags = RPC_TASK_ASYNC,
2178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int status;
2180
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002181 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002182 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002183 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002184 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002185 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002186 if (data->is_recover)
2187 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002188 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002189 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002190 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002191 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002192 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002193 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002194 smp_wmb();
2195 } else
2196 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002197 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return status;
2199}
2200
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002203 struct nfs4_opendata *data = calldata;
2204 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002205 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002206 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002207
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002209 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002210 /*
2211 * Check if we still need to send an OPEN call, or if we can use
2212 * a delegation instead.
2213 */
2214 if (data->state != NULL) {
2215 struct nfs_delegation *delegation;
2216
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002217 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002218 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002219 rcu_read_lock();
2220 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002221 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002222 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002223 rcu_read_unlock();
2224 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002225 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002226 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002227 switch (claim) {
2228 default:
2229 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002230 case NFS4_OPEN_CLAIM_PREVIOUS:
2231 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2232 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002233 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002234 case NFS4_OPEN_CLAIM_FH:
2235 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002236 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002237 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002238 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002239 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002240 &data->o_res.seq_res,
2241 task) != 0)
2242 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002243
2244 /* Set the create mode (note dependency on the session type) */
2245 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2246 if (data->o_arg.open_flags & O_EXCL) {
2247 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2248 if (nfs4_has_persistent_session(clp))
2249 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2250 else if (clp->cl_mvops->minor_version > 0)
2251 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2252 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002253 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002254unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002255 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002256 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002257out_no_action:
2258 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002259out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002260 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002261}
2262
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002263static void nfs4_open_done(struct rpc_task *task, void *calldata)
2264{
2265 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002267 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002268
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002269 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002270 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002271
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002272 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002273 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2274 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002275 case S_IFREG:
2276 break;
2277 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002279 break;
2280 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002282 break;
2283 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002284 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002285 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002286 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002287 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002288 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2289 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002290 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002291 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002292}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002293
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002294static void nfs4_open_release(void *calldata)
2295{
2296 struct nfs4_opendata *data = calldata;
2297 struct nfs4_state *state = NULL;
2298
2299 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002300 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301 goto out_free;
2302 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002303 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002304 goto out_free;
2305 /* In case we need an open_confirm, no cleanup! */
2306 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2307 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002308 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002309 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002310 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002312 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002313}
2314
2315static const struct rpc_call_ops nfs4_open_ops = {
2316 .rpc_call_prepare = nfs4_open_prepare,
2317 .rpc_call_done = nfs4_open_done,
2318 .rpc_release = nfs4_open_release,
2319};
2320
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002321static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002322{
David Howells2b0143b2015-03-17 22:25:59 +00002323 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002324 struct nfs_server *server = NFS_SERVER(dir);
2325 struct nfs_openargs *o_arg = &data->o_arg;
2326 struct nfs_openres *o_res = &data->o_res;
2327 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002328 struct rpc_message msg = {
2329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2330 .rpc_argp = o_arg,
2331 .rpc_resp = o_res,
2332 .rpc_cred = data->owner->so_cred,
2333 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002334 struct rpc_task_setup task_setup_data = {
2335 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002336 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 .callback_ops = &nfs4_open_ops,
2338 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002339 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002340 .flags = RPC_TASK_ASYNC,
2341 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002342 int status;
2343
Chuck Levera9c92d62013-08-09 12:48:18 -04002344 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002345 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002346 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002347 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002348 data->cancelled = false;
2349 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002350 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002351 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002352 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002353 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002354 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002355 if (IS_ERR(task))
2356 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002357 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002358 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002359 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002360 smp_wmb();
2361 } else
2362 status = data->rpc_status;
2363 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002364
2365 return status;
2366}
2367
2368static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2369{
David Howells2b0143b2015-03-17 22:25:59 +00002370 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002371 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002372 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002373
2374 status = nfs4_run_open_task(data, 1);
2375 if (status != 0 || !data->rpc_done)
2376 return status;
2377
Trond Myklebust6926afd2012-01-07 13:22:46 -05002378 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2379
Anna Schumakerd7e98252017-01-11 16:13:29 -05002380 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002381 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002382
2383 return status;
2384}
2385
Trond Myklebustf3792d62014-07-10 08:54:32 -04002386/*
2387 * Additional permission checks in order to distinguish between an
2388 * open for read, and an open for execute. This works around the
2389 * fact that NFSv4 OPEN treats read and execute permissions as being
2390 * the same.
2391 * Note that in the non-execute case, we want to turn off permission
2392 * checking if we just created a new file (POSIX open() semantics).
2393 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002394static int nfs4_opendata_access(struct rpc_cred *cred,
2395 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002396 struct nfs4_state *state, fmode_t fmode,
2397 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002398{
2399 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002400 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002401
2402 /* access call failed or for some reason the server doesn't
2403 * support any access modes -- defer access call until later */
2404 if (opendata->o_res.access_supported == 0)
2405 return 0;
2406
2407 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002408 /*
2409 * Use openflags to check for exec, because fmode won't
2410 * always have FMODE_EXEC set when file open for exec.
2411 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002412 if (openflags & __FMODE_EXEC) {
2413 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002414 if (S_ISDIR(state->inode->i_mode))
2415 mask = NFS4_ACCESS_LOOKUP;
2416 else
2417 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002418 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002419 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002420
2421 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002422 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2423 nfs_access_add_cache(state->inode, &cache);
2424
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002425 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2426 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002427 return 0;
2428
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002429 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002430}
2431
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002432/*
2433 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2434 */
2435static int _nfs4_proc_open(struct nfs4_opendata *data)
2436{
David Howells2b0143b2015-03-17 22:25:59 +00002437 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002438 struct nfs_server *server = NFS_SERVER(dir);
2439 struct nfs_openargs *o_arg = &data->o_arg;
2440 struct nfs_openres *o_res = &data->o_res;
2441 int status;
2442
2443 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002444 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002445 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002446 if (status != 0) {
2447 if (status == -NFS4ERR_BADNAME &&
2448 !(o_arg->open_flags & O_CREAT))
2449 return -ENOENT;
2450 return status;
2451 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002452
Trond Myklebust6926afd2012-01-07 13:22:46 -05002453 nfs_fattr_map_and_free_names(server, &data->f_attr);
2454
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002455 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002456 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002457 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002458 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002459 data->file_created = true;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002460 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002461 update_changeattr(dir, &o_res->cinfo,
2462 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002463 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002464 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2465 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002467 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002469 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002471 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2472 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002473 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002474 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/*
2479 * OPEN_EXPIRED:
2480 * reclaim state on the server after a network partition.
2481 * Assumes caller holds the appropriate lock
2482 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002483static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002485 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002486 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002488 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002489 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002490 if (IS_ERR(opendata))
2491 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002492 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002493 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002494 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002495 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002499static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002500{
Trond Myklebust539cd032007-06-05 11:46:42 -04002501 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002502 struct nfs4_exception exception = { };
2503 int err;
2504
2505 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002506 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002507 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002508 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2509 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002510 switch (err) {
2511 default:
2512 goto out;
2513 case -NFS4ERR_GRACE:
2514 case -NFS4ERR_DELAY:
2515 nfs4_handle_exception(server, err, &exception);
2516 err = 0;
2517 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002518 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002519out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002520 return err;
2521}
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002526 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Trond Myklebust864472e2006-01-03 09:55:15 +01002528 ctx = nfs4_state_find_open_context(state);
2529 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002530 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002531 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002532 put_nfs_open_context(ctx);
2533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Trond Myklebust41020b62016-09-22 13:38:58 -04002536static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2537 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002538{
Trond Myklebust41020b62016-09-22 13:38:58 -04002539 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002540 write_seqlock(&state->seqlock);
2541 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2542 write_sequnlock(&state->seqlock);
2543 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2544}
2545
2546static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2547{
2548 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002549 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002550}
2551
2552static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2553{
2554 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2555 nfs40_clear_delegation_stateid(state);
2556 return nfs4_open_expired(sp, state);
2557}
2558
Trond Myklebust45870d62016-09-22 13:38:59 -04002559static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2560 nfs4_stateid *stateid,
2561 struct rpc_cred *cred)
2562{
2563 return -NFS4ERR_BAD_STATEID;
2564}
2565
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002566#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002567static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2568 nfs4_stateid *stateid,
2569 struct rpc_cred *cred)
2570{
2571 int status;
2572
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002573 switch (stateid->type) {
2574 default:
2575 break;
2576 case NFS4_INVALID_STATEID_TYPE:
2577 case NFS4_SPECIAL_STATEID_TYPE:
2578 return -NFS4ERR_BAD_STATEID;
2579 case NFS4_REVOKED_STATEID_TYPE:
2580 goto out_free;
2581 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002582
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002583 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002584 switch (status) {
2585 case -NFS4ERR_EXPIRED:
2586 case -NFS4ERR_ADMIN_REVOKED:
2587 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002588 break;
2589 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002590 return status;
2591 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002592out_free:
2593 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002594 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002595 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002596}
2597
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002598static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002599{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002600 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002601 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002602 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002603 struct rpc_cred *cred;
2604 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002605
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002606 /* Get the delegation credential for use by test/free_stateid */
2607 rcu_read_lock();
2608 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002609 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002610 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002611 return;
2612 }
2613
2614 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002615 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2616 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2617 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002618 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002619 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002620 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002621 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002622
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002623 cred = get_rpccred(delegation->cred);
2624 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002625 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002626 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002627 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002628 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002629
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002630 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002631}
2632
2633/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002634 * nfs41_check_expired_locks - possibly free a lock stateid
2635 *
2636 * @state: NFSv4 state for an inode
2637 *
2638 * Returns NFS_OK if recovery for this stateid is now finished.
2639 * Otherwise a negative NFS4ERR value is returned.
2640 */
2641static int nfs41_check_expired_locks(struct nfs4_state *state)
2642{
2643 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002644 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002645 struct nfs_server *server = NFS_SERVER(state->inode);
2646
2647 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2648 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002649
2650 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002651 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2652 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2653 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2654
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002655 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002656 spin_unlock(&state->state_lock);
2657
2658 nfs4_put_lock_state(prev);
2659 prev = lsp;
2660
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002661 status = nfs41_test_and_free_expired_stateid(server,
2662 &lsp->ls_stateid,
2663 cred);
2664 trace_nfs4_test_lock_stateid(state, lsp, status);
2665 if (status == -NFS4ERR_EXPIRED ||
2666 status == -NFS4ERR_BAD_STATEID) {
2667 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002668 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002669 if (!recover_lost_locks)
2670 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2671 } else if (status != NFS_OK) {
2672 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002673 nfs4_put_lock_state(prev);
2674 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002675 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002676 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002677 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002678 }
2679 spin_unlock(&state->state_lock);
2680 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002681out:
2682 return ret;
2683}
2684
2685/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002686 * nfs41_check_open_stateid - possibly free an open stateid
2687 *
2688 * @state: NFSv4 state for an inode
2689 *
2690 * Returns NFS_OK if recovery for this stateid is now finished.
2691 * Otherwise a negative NFS4ERR value is returned.
2692 */
2693static int nfs41_check_open_stateid(struct nfs4_state *state)
2694{
2695 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002696 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002697 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002698 int status;
2699
Trond Myklebustb134fc42016-09-22 13:39:16 -04002700 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002701 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2702 if (nfs4_have_delegation(state->inode, state->state))
2703 return NFS_OK;
2704 return -NFS4ERR_OPENMODE;
2705 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002706 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002707 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002708 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002709 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002710 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002711 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2712 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2713 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002714 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002715 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002716 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002717 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002718 if (nfs_open_stateid_recover_openmode(state))
2719 return -NFS4ERR_OPENMODE;
2720 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002721}
2722
2723static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2724{
Chuck Levereb64cf92012-07-11 16:30:05 -04002725 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002726
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002727 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002728 status = nfs41_check_expired_locks(state);
2729 if (status != NFS_OK)
2730 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002731 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002732 if (status != NFS_OK)
2733 status = nfs4_open_expired(sp, state);
2734 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002735}
2736#endif
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002739 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2740 * fields corresponding to attributes that were used to store the verifier.
2741 * Make sure we clobber those fields in the later setattr call
2742 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002743static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2744 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002745{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002746 const u32 *attrset = opendata->o_res.attrset;
2747
2748 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002749 !(sattr->ia_valid & ATTR_ATIME_SET))
2750 sattr->ia_valid |= ATTR_ATIME;
2751
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002752 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002753 !(sattr->ia_valid & ATTR_MTIME_SET))
2754 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002755
2756 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002757 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002758 (attrset[1] & FATTR4_WORD1_MODE ||
2759 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002760 sattr->ia_valid &= ~ATTR_MODE;
2761
2762 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2763 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002764}
2765
Trond Myklebustc21443c2013-02-07 14:26:21 -05002766static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2767 fmode_t fmode,
2768 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002769 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002770{
2771 struct nfs4_state_owner *sp = opendata->owner;
2772 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002773 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002774 struct nfs4_state *state;
2775 unsigned int seq;
2776 int ret;
2777
2778 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2779
2780 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002781 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002782 goto out;
2783
2784 state = nfs4_opendata_to_nfs4_state(opendata);
2785 ret = PTR_ERR(state);
2786 if (IS_ERR(state))
2787 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002788 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002789 if (server->caps & NFS_CAP_POSIX_LOCK)
2790 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002791 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2792 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002793
Trond Myklebust275bb302013-05-29 13:11:28 -04002794 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002795 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002796 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002797 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002798 alias = d_exact_alias(dentry, state->inode);
2799 if (!alias)
2800 alias = d_splice_alias(igrab(state->inode), dentry);
2801 /* d_splice_alias() can't fail here - it's a non-directory */
2802 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002803 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002804 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002805 }
2806 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002807 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002808 }
2809
Trond Myklebustc21443c2013-02-07 14:26:21 -05002810 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2811 if (ret != 0)
2812 goto out;
2813
David Howells2b0143b2015-03-17 22:25:59 +00002814 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002815 nfs_inode_attach_open_context(ctx);
2816 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2817 nfs4_schedule_stateid_recovery(server, state);
2818 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002819out:
2820 return ret;
2821}
2822
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002823/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002824 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
Andy Adamson82be4172012-05-23 05:02:35 -04002826static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002827 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002828 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002829 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002830 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 struct nfs4_state_owner *sp;
2833 struct nfs4_state *state = NULL;
2834 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002835 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002836 struct dentry *dentry = ctx->dentry;
2837 struct rpc_cred *cred = ctx->cred;
2838 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2839 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002840 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002841 struct iattr *sattr = c->sattr;
2842 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002843 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002844 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002848 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2849 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2851 goto out_err;
2852 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002853 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002854 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002855 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002856 if (d_really_is_positive(dentry))
2857 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002858 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002859 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002860 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002861 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2862 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002863 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002864 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
David Quigley14c43f72013-05-22 12:50:43 -04002866 if (label) {
2867 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2868 if (IS_ERR(olabel)) {
2869 status = PTR_ERR(olabel);
2870 goto err_opendata_put;
2871 }
2872 }
2873
Trond Myklebuste911b812014-03-26 13:24:37 -07002874 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2875 if (!opendata->f_attr.mdsthreshold) {
2876 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2877 if (!opendata->f_attr.mdsthreshold)
2878 goto err_free_label;
2879 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002880 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002881 }
David Howells2b0143b2015-03-17 22:25:59 +00002882 if (d_really_is_positive(dentry))
2883 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002884
Trond Myklebust3efb9722013-05-29 13:17:04 -04002885 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002886 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002887 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002888 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002889
NeilBrownefcbc042015-07-30 13:00:56 +10002890 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002891 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002892 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002893 /*
2894 * send create attributes which was not set by open
2895 * with an extra setattr.
2896 */
2897 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2898 nfs_fattr_init(opendata->o_res.f_attr);
2899 status = nfs4_do_setattr(state->inode, cred,
2900 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002901 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002902 if (status == 0) {
2903 nfs_setattr_update_inode(state->inode, sattr,
2904 opendata->o_res.f_attr);
2905 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2906 }
David Quigley1775fd32013-05-22 12:50:42 -04002907 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002908 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002909 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002910 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002911
Trond Myklebuste911b812014-03-26 13:24:37 -07002912 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002913 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002914 opendata->f_attr.mdsthreshold = NULL;
2915 }
Andy Adamson82be4172012-05-23 05:02:35 -04002916
David Quigley14c43f72013-05-22 12:50:43 -04002917 nfs4_label_free(olabel);
2918
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002919 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002922err_free_label:
2923 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002924err_opendata_put:
2925 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002926err_put_state_owner:
2927 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return status;
2930}
2931
2932
Andy Adamson82be4172012-05-23 05:02:35 -04002933static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002934 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002935 int flags,
2936 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002937 struct nfs4_label *label,
2938 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002940 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 struct nfs4_exception exception = { };
2942 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002943 struct nfs4_open_createattrs c = {
2944 .label = label,
2945 .sattr = sattr,
2946 .verf = {
2947 [0] = (__u32)jiffies,
2948 [1] = (__u32)current->pid,
2949 },
2950 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 int status;
2952
2953 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002954 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002955 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002956 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (status == 0)
2958 break;
2959 /* NOTE: BAD_SEQID means the server and client disagree about the
2960 * book-keeping w.r.t. state-changing operations
2961 * (OPEN/CLOSE/LOCK/LOCKU...)
2962 * It is actually a sign of a bug on the client or on the server.
2963 *
2964 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002965 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 * have unhashed the old state_owner for us, and that we can
2967 * therefore safely retry using a new one. We should still warn
2968 * the user though...
2969 */
2970 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002971 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002972 " returned a bad sequence-id error!\n",
2973 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 exception.retry = 1;
2975 continue;
2976 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002977 /*
2978 * BAD_STATEID on OPEN means that the server cancelled our
2979 * state before it received the OPEN_CONFIRM.
2980 * Recover by retrying the request as per the discussion
2981 * on Page 181 of RFC3530.
2982 */
2983 if (status == -NFS4ERR_BAD_STATEID) {
2984 exception.retry = 1;
2985 continue;
2986 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002987 if (status == -EAGAIN) {
2988 /* We must have found a delegation */
2989 exception.retry = 1;
2990 continue;
2991 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002992 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2993 continue;
2994 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 status, &exception));
2996 } while (exception.retry);
2997 return res;
2998}
2999
Trond Myklebust8487c472016-06-26 08:44:35 -04003000static int _nfs4_do_setattr(struct inode *inode,
3001 struct nfs_setattrargs *arg,
3002 struct nfs_setattrres *res,
3003 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003004 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003005{
3006 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003007 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3009 .rpc_argp = arg,
3010 .rpc_resp = res,
3011 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003012 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003013 struct rpc_cred *delegation_cred = NULL;
3014 unsigned long timestamp = jiffies;
3015 fmode_t fmode;
3016 bool truncate;
3017 int status;
3018
3019 nfs_fattr_init(res->fattr);
3020
3021 /* Servers should only apply open mode checks for file size changes */
3022 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3023 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3024
3025 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3026 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003027 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003028 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003029 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003030 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003031 l_ctx = nfs_get_lock_context(ctx);
3032 if (IS_ERR(l_ctx))
3033 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003034 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3035 &arg->stateid, &delegation_cred);
3036 nfs_put_lock_context(l_ctx);
3037 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003038 return -EBADF;
3039 } else
3040 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3041 if (delegation_cred)
3042 msg.rpc_cred = delegation_cred;
3043
3044 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3045
3046 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003047 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003048 renew_lease(server, timestamp);
3049 trace_nfs4_setattr(inode, &arg->stateid, status);
3050 return status;
3051}
3052
3053static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3054 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003055 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003056 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003058 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003059 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003060 struct nfs_setattrargs arg = {
3061 .fh = NFS_FH(inode),
3062 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 .server = server,
3064 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003065 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003066 };
3067 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003069 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003071 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003072 struct nfs4_exception exception = {
3073 .state = state,
3074 .inode = inode,
3075 .stateid = &arg.stateid,
3076 };
3077 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
David Quigleyaa9c2662013-05-22 12:50:44 -04003079 arg.bitmask = nfs4_bitmask(server, ilabel);
3080 if (ilabel)
3081 arg.bitmask = nfs4_bitmask(server, olabel);
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003084 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003085 switch (err) {
3086 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003087 if (!(sattr->ia_valid & ATTR_SIZE)) {
3088 pr_warn_once("NFSv4: server %s is incorrectly "
3089 "applying open mode checks to "
3090 "a SETATTR that is not "
3091 "changing file size.\n",
3092 server->nfs_client->cl_hostname);
3093 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003094 if (state && !(state->state & FMODE_WRITE)) {
3095 err = -EBADF;
3096 if (sattr->ia_valid & ATTR_OPEN)
3097 err = -EACCES;
3098 goto out;
3099 }
3100 }
3101 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003103out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return err;
3105}
3106
Peng Tao500d7012015-09-22 11:35:22 +08003107static bool
3108nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3109{
3110 if (inode == NULL || !nfs_have_layout(inode))
3111 return false;
3112
3113 return pnfs_wait_on_layoutreturn(inode, task);
3114}
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116struct nfs4_closedata {
3117 struct inode *inode;
3118 struct nfs4_state *state;
3119 struct nfs_closeargs arg;
3120 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003121 struct {
3122 struct nfs4_layoutreturn_args arg;
3123 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003124 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003125 u32 roc_barrier;
3126 bool roc;
3127 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003128 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003129 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130};
3131
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003132static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003133{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003134 struct nfs4_closedata *calldata = data;
3135 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003136 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003137
Trond Myklebustcf805162016-11-15 14:56:07 -05003138 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003139 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3140 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003141 nfs4_put_open_state(calldata->state);
3142 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003143 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003144 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003145 kfree(calldata);
3146}
3147
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003148static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003150 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003153 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Chuck Levera3ca5652012-03-01 17:00:40 -05003155 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003156 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3157 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003158 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003159
3160 /* Handle Layoutreturn errors */
3161 if (calldata->arg.lr_args && task->tk_status != 0) {
3162 switch (calldata->res.lr_ret) {
3163 default:
3164 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3165 break;
3166 case 0:
3167 calldata->arg.lr_args = NULL;
3168 calldata->res.lr_res = NULL;
3169 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003170 case -NFS4ERR_OLD_STATEID:
3171 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003172 calldata->inode))
3173 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003174 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003175 case -NFS4ERR_ADMIN_REVOKED:
3176 case -NFS4ERR_DELEG_REVOKED:
3177 case -NFS4ERR_EXPIRED:
3178 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003179 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3180 case -NFS4ERR_WRONG_CRED:
3181 calldata->arg.lr_args = NULL;
3182 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003183 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003184 }
3185 }
3186
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003187 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 * the state_owner. we keep this around to process errors
3189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 switch (task->tk_status) {
3191 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003192 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003193 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003194 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003195 case -NFS4ERR_ACCESS:
3196 if (calldata->arg.bitmask != NULL) {
3197 calldata->arg.bitmask = NULL;
3198 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003199 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003200
3201 }
3202 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003203 case -NFS4ERR_OLD_STATEID:
3204 /* Did we race with OPEN? */
3205 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003206 state))
3207 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003208 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003209 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003211 case -NFS4ERR_EXPIRED:
3212 nfs4_free_revoked_stateid(server,
3213 &calldata->arg.stateid,
3214 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003215 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003216 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 default:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003219 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
3220 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003222 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3223 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003224out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003225 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003226 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003227 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003228 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003229 return;
3230lr_restart:
3231 calldata->res.lr_ret = 0;
3232out_restart:
3233 task->tk_status = 0;
3234 rpc_restart_call_prepare(task);
3235 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003238static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003240 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003241 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003242 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003243 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003244 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003245
Chuck Levera3ca5652012-03-01 17:00:40 -05003246 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003247 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003248 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003249
Trond Myklebust88069f72009-12-08 08:33:16 -05003250 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003251 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003252 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3253 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3254 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003255 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003256 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003257 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003258 if (state->n_rdonly == 0)
3259 call_close |= is_rdonly;
3260 else if (is_rdonly)
3261 calldata->arg.fmode |= FMODE_READ;
3262 if (state->n_wronly == 0)
3263 call_close |= is_wronly;
3264 else if (is_wronly)
3265 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003266 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3267 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003268 } else if (is_rdwr)
3269 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3270
Trond Myklebust5cc78612016-11-14 11:19:56 -05003271 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003272 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003273 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003274 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003275
3276 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003277 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003278 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003279 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003280
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003281 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003282 nfs_release_seqid(calldata->arg.seqid);
3283 goto out_wait;
3284 }
3285
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003286 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003287 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003288
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003289 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003290 /* Close-to-open cache consistency revalidation */
3291 if (!nfs4_have_delegation(inode, FMODE_READ))
3292 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3293 else
3294 calldata->arg.bitmask = NULL;
3295 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003296
Trond Myklebust6ae37332015-01-30 14:21:14 -05003297 calldata->arg.share_access =
3298 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3299 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003300
Trond Myklebustd8d84982016-12-19 12:14:44 -05003301 if (calldata->res.fattr == NULL)
3302 calldata->arg.bitmask = NULL;
3303 else if (calldata->arg.bitmask == NULL)
3304 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003305 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003306 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003307 &calldata->arg.seq_args,
3308 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003309 task) != 0)
3310 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003311 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003312 return;
3313out_no_action:
3314 task->tk_action = NULL;
3315out_wait:
3316 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003319static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003320 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003321 .rpc_call_done = nfs4_close_done,
3322 .rpc_release = nfs4_free_closedata,
3323};
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325/*
3326 * It is possible for data to be read/written from a mem-mapped file
3327 * after the sys_close call (which hits the vfs layer as a flush).
3328 * This means that we can't safely call nfsv4 close on a file until
3329 * the inode is cleared. This in turn means that we are not good
3330 * NFSv4 citizens - we do not indicate to the server to update the file's
3331 * share state even when we are done with one of the three share
3332 * stateid's in the inode.
3333 *
3334 * NOTE: Caller must be holding the sp->so_owner semaphore!
3335 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003336int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003338 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003339 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003341 struct nfs4_state_owner *sp = state->owner;
3342 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003343 struct rpc_message msg = {
3344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3345 .rpc_cred = state->owner->so_cred,
3346 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003347 struct rpc_task_setup task_setup_data = {
3348 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003349 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003350 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003351 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003352 .flags = RPC_TASK_ASYNC,
3353 };
Trond Myklebust95121352005-10-18 14:20:12 -07003354 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003356 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3357 &task_setup_data.rpc_client, &msg);
3358
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003359 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003361 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003362 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003363 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003365 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003366 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3367 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003369 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3370 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003371 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003372 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003373 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003374 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003375 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003376 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003377 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003378 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003379 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003380 calldata->lr.roc = pnfs_roc(state->inode,
3381 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3382 if (calldata->lr.roc) {
3383 calldata->arg.lr_args = &calldata->lr.arg;
3384 calldata->res.lr_res = &calldata->lr.res;
3385 }
Al Viro643168c2011-06-22 18:20:23 -04003386 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003387
Trond Myklebust1174dd12010-12-21 10:52:24 -05003388 msg.rpc_argp = &calldata->arg;
3389 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003390 task_setup_data.callback_data = calldata;
3391 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003392 if (IS_ERR(task))
3393 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003394 status = 0;
3395 if (wait)
3396 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003397 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003398 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003399out_free_calldata:
3400 kfree(calldata);
3401out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003402 nfs4_put_open_state(state);
3403 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003404 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
Trond Myklebust2b484292010-09-17 10:56:51 -04003407static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003408nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3409 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003412 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3413
3414 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Trond Myklebust565277f2007-10-15 18:17:53 -04003416 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003417 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003418
3419 nfs4_label_release_security(label);
3420
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003421 if (IS_ERR(state))
3422 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003423 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424}
3425
Trond Myklebust1185a552009-12-03 15:54:02 -05003426static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003427{
3428 if (ctx->state == NULL)
3429 return;
3430 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003431 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003432 else
Al Viro643168c2011-06-22 18:20:23 -04003433 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Trond Myklebustb944dba2013-11-04 15:20:20 -05003436#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3437#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003438#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3441{
Kinglong Mee8c612822015-08-26 21:12:58 +08003442 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003443 struct nfs4_server_caps_arg args = {
3444 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003445 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 struct nfs4_server_caps_res res = {};
3448 struct rpc_message msg = {
3449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003450 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 .rpc_resp = &res,
3452 };
3453 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003454 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Kinglong Mee8c612822015-08-26 21:12:58 +08003456 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3457 FATTR4_WORD0_FH_EXPIRE_TYPE |
3458 FATTR4_WORD0_LINK_SUPPORT |
3459 FATTR4_WORD0_SYMLINK_SUPPORT |
3460 FATTR4_WORD0_ACLSUPPORT;
3461 if (minorversion)
3462 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3463
Bryan Schumaker7c513052011-03-24 17:12:24 +00003464 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003466 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003467 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003468 case 0:
3469 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3470 res.attr_bitmask[2] = 0;
3471 break;
3472 case 1:
3473 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3474 break;
3475 case 2:
3476 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003479 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3480 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3481 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3482 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003483 NFS_CAP_CTIME|NFS_CAP_MTIME|
3484 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003485 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3486 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 server->caps |= NFS_CAP_ACLS;
3488 if (res.has_links != 0)
3489 server->caps |= NFS_CAP_HARDLINKS;
3490 if (res.has_symlinks != 0)
3491 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003492 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3493 server->caps |= NFS_CAP_FILEID;
3494 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3495 server->caps |= NFS_CAP_MODE;
3496 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3497 server->caps |= NFS_CAP_NLINK;
3498 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3499 server->caps |= NFS_CAP_OWNER;
3500 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3501 server->caps |= NFS_CAP_OWNER_GROUP;
3502 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3503 server->caps |= NFS_CAP_ATIME;
3504 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3505 server->caps |= NFS_CAP_CTIME;
3506 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3507 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003508#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3509 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3510 server->caps |= NFS_CAP_SECURITY_LABEL;
3511#endif
3512 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3513 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003514 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003515
Trond Myklebusta65318b2009-03-11 14:10:28 -04003516 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3517 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3518 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003519 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003520
3521 /* Avoid a regression due to buggy server */
3522 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3523 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003524 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3525 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003528 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return status;
3532}
3533
Trond Myklebust55a97592006-06-09 09:34:19 -04003534int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
3536 struct nfs4_exception exception = { };
3537 int err;
3538 do {
3539 err = nfs4_handle_exception(server,
3540 _nfs4_server_capabilities(server, fhandle),
3541 &exception);
3542 } while (exception.retry);
3543 return err;
3544}
3545
3546static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3547 struct nfs_fsinfo *info)
3548{
David Quigleyaa9c2662013-05-22 12:50:44 -04003549 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003551 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 };
3553 struct nfs4_lookup_res res = {
3554 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003555 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 .fh = fhandle,
3557 };
3558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3560 .rpc_argp = &args,
3561 .rpc_resp = &res,
3562 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003563
David Quigleyaa9c2662013-05-22 12:50:44 -04003564 bitmask[0] = nfs4_fattr_bitmap[0];
3565 bitmask[1] = nfs4_fattr_bitmap[1];
3566 /*
3567 * Process the label in the upcoming getfattr
3568 */
3569 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3570
Trond Myklebust0e574af2005-10-27 22:12:38 -04003571 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003572 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
3575static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3576 struct nfs_fsinfo *info)
3577{
3578 struct nfs4_exception exception = { };
3579 int err;
3580 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003581 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003582 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003583 switch (err) {
3584 case 0:
3585 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003586 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003587 default:
3588 err = nfs4_handle_exception(server, err, &exception);
3589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003591out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return err;
3593}
3594
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003595static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3596 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3597{
Trond Myklebustc2190662013-08-26 19:23:04 -04003598 struct rpc_auth_create_args auth_args = {
3599 .pseudoflavor = flavor,
3600 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003601 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003602
Trond Myklebustc2190662013-08-26 19:23:04 -04003603 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003604 if (IS_ERR(auth))
3605 return -EACCES;
3606 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003607}
3608
Chuck Lever9a744ba2013-03-16 15:56:02 -04003609/*
3610 * Retry pseudoroot lookup with various security flavors. We do this when:
3611 *
3612 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3613 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3614 *
3615 * Returns zero on success, or a negative NFS4ERR value, or a
3616 * negative errno value.
3617 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003618static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003619 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003621 /* Per 3530bis 15.33.5 */
3622 static const rpc_authflavor_t flav_array[] = {
3623 RPC_AUTH_GSS_KRB5P,
3624 RPC_AUTH_GSS_KRB5I,
3625 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003626 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003627 RPC_AUTH_NULL,
3628 };
3629 int status = -EPERM;
3630 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003632 if (server->auth_info.flavor_len > 0) {
3633 /* try each flavor specified by user */
3634 for (i = 0; i < server->auth_info.flavor_len; i++) {
3635 status = nfs4_lookup_root_sec(server, fhandle, info,
3636 server->auth_info.flavors[i]);
3637 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3638 continue;
3639 break;
3640 }
3641 } else {
3642 /* no flavors specified by user, try default list */
3643 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3644 status = nfs4_lookup_root_sec(server, fhandle, info,
3645 flav_array[i]);
3646 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3647 continue;
3648 break;
3649 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003650 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003651
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003652 /*
3653 * -EACCESS could mean that the user doesn't have correct permissions
3654 * to access the mount. It could also mean that we tried to mount
3655 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3656 * existing mount programs don't handle -EACCES very well so it should
3657 * be mapped to -EPERM instead.
3658 */
3659 if (status == -EACCES)
3660 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003661 return status;
3662}
3663
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003664/**
3665 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3666 * @server: initialized nfs_server handle
3667 * @fhandle: we fill in the pseudo-fs root file handle
3668 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003669 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003670 *
3671 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003672 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003673int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003674 struct nfs_fsinfo *info,
3675 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003676{
Andre Przywarac7757072015-04-23 17:17:40 +01003677 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003678
Andre Przywarac7757072015-04-23 17:17:40 +01003679 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003680 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003681
3682 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003683 status = server->nfs_client->cl_mvops->find_root_sec(server,
3684 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (status == 0)
3687 status = nfs4_server_capabilities(server, fhandle);
3688 if (status == 0)
3689 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003690
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003691 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
Bryan Schumakerbae36242012-05-10 15:07:31 -04003694static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3695 struct nfs_fsinfo *info)
3696{
3697 int error;
3698 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003699 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003700
3701 error = nfs4_server_capabilities(server, mntfh);
3702 if (error < 0) {
3703 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3704 return error;
3705 }
3706
David Quigley14c43f72013-05-22 12:50:43 -04003707 label = nfs4_label_alloc(server, GFP_KERNEL);
3708 if (IS_ERR(label))
3709 return PTR_ERR(label);
3710
David Quigley1775fd32013-05-22 12:50:42 -04003711 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003712 if (error < 0) {
3713 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003714 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003715 }
3716
3717 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3718 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3719 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3720
David Quigley14c43f72013-05-22 12:50:43 -04003721err_free_label:
3722 nfs4_label_free(label);
3723
Bryan Schumakerbae36242012-05-10 15:07:31 -04003724 return error;
3725}
3726
Manoj Naik6b97fd32006-06-09 09:34:29 -04003727/*
3728 * Get locations and (maybe) other attributes of a referral.
3729 * Note that we'll actually follow the referral later when
3730 * we detect fsid mismatch in inode revalidation
3731 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003732static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3733 const struct qstr *name, struct nfs_fattr *fattr,
3734 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003735{
3736 int status = -ENOMEM;
3737 struct page *page = NULL;
3738 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003739
3740 page = alloc_page(GFP_KERNEL);
3741 if (page == NULL)
3742 goto out;
3743 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3744 if (locations == NULL)
3745 goto out;
3746
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003747 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003748 if (status != 0)
3749 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003750
3751 /*
3752 * If the fsid didn't change, this is a migration event, not a
3753 * referral. Cause us to drop into the exception handler, which
3754 * will kick off migration recovery.
3755 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003756 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003757 dprintk("%s: server did not return a different fsid for"
3758 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003759 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003760 goto out;
3761 }
Andy Adamson533eb462011-06-13 18:25:56 -04003762 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3763 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003764
Andy Adamson533eb462011-06-13 18:25:56 -04003765 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003766 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003767 memset(fhandle, 0, sizeof(struct nfs_fh));
3768out:
3769 if (page)
3770 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003771 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003772 return status;
3773}
3774
David Quigley1775fd32013-05-22 12:50:42 -04003775static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3776 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
3778 struct nfs4_getattr_arg args = {
3779 .fh = fhandle,
3780 .bitmask = server->attr_bitmask,
3781 };
3782 struct nfs4_getattr_res res = {
3783 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003784 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 .server = server,
3786 };
3787 struct rpc_message msg = {
3788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3789 .rpc_argp = &args,
3790 .rpc_resp = &res,
3791 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003792
3793 args.bitmask = nfs4_bitmask(server, label);
3794
Trond Myklebust0e574af2005-10-27 22:12:38 -04003795 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003796 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
David Quigley1775fd32013-05-22 12:50:42 -04003799static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3800 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801{
3802 struct nfs4_exception exception = { };
3803 int err;
3804 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003805 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3806 trace_nfs4_getattr(server, fhandle, fattr, err);
3807 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 &exception);
3809 } while (exception.retry);
3810 return err;
3811}
3812
3813/*
3814 * The file is not closed if it is opened due to the a request to change
3815 * the size of the file. The open call will not be needed once the
3816 * VFS layer lookup-intents are implemented.
3817 *
3818 * Close is called when the inode is destroyed.
3819 * If we haven't opened the file for O_WRONLY, we
3820 * need to in the size_change case to obtain a stateid.
3821 *
3822 * Got race?
3823 * Because OPEN is always done by name in nfsv4, it is
3824 * possible that we opened a different file by the same
3825 * name. We can recognize this race condition, but we
3826 * can't do anything about it besides returning an error.
3827 *
3828 * This will be fixed with VFS changes (lookup-intent).
3829 */
3830static int
3831nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3832 struct iattr *sattr)
3833{
David Howells2b0143b2015-03-17 22:25:59 +00003834 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003835 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003836 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003837 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 int status;
3839
Peng Tao88ac8152014-09-12 11:04:10 +08003840 if (pnfs_ld_layoutret_on_setattr(inode) &&
3841 sattr->ia_valid & ATTR_SIZE &&
3842 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003843 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003844
Trond Myklebust0e574af2005-10-27 22:12:38 -04003845 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Andy Adamson26699402012-05-30 16:12:24 -04003847 /* Deal with open(O_TRUNC) */
3848 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003849 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003850
3851 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003852 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003853 return 0;
3854
Trond Myklebustd5308382005-11-04 15:33:38 -05003855 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003856 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003857
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003858 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003859 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003860 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003861 }
3862
David Quigley14c43f72013-05-22 12:50:43 -04003863 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3864 if (IS_ERR(label))
3865 return PTR_ERR(label);
3866
NeilBrown29b59f92016-10-13 15:26:47 +11003867 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003868 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003869 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003870 nfs_setsecurity(inode, fattr, label);
3871 }
David Quigley14c43f72013-05-22 12:50:43 -04003872 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return status;
3874}
3875
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003876static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3877 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003878 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003879{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003880 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003881 int status;
3882 struct nfs4_lookup_arg args = {
3883 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003884 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003885 .name = name,
3886 };
3887 struct nfs4_lookup_res res = {
3888 .server = server,
3889 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003890 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003891 .fh = fhandle,
3892 };
3893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3895 .rpc_argp = &args,
3896 .rpc_resp = &res,
3897 };
3898
David Quigleyaa9c2662013-05-22 12:50:44 -04003899 args.bitmask = nfs4_bitmask(server, label);
3900
David Howells2b3de442006-08-22 20:06:09 -04003901 nfs_fattr_init(fattr);
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003904 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 dprintk("NFS reply lookup: %d\n", status);
3906 return status;
3907}
3908
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003909static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003910{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003911 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003912 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003913 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3914 fattr->nlink = 2;
3915}
3916
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003917static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003918 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003919 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003920{
3921 struct nfs4_exception exception = { };
3922 struct rpc_clnt *client = *clnt;
3923 int err;
3924 do {
David Quigley1775fd32013-05-22 12:50:42 -04003925 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003926 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003927 switch (err) {
3928 case -NFS4ERR_BADNAME:
3929 err = -ENOENT;
3930 goto out;
3931 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003932 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003933 if (err == -NFS4ERR_MOVED)
3934 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003935 goto out;
3936 case -NFS4ERR_WRONGSEC:
3937 err = -EPERM;
3938 if (client != *clnt)
3939 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003940 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003941 if (IS_ERR(client))
3942 return PTR_ERR(client);
3943
3944 exception.retry = 1;
3945 break;
3946 default:
3947 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3948 }
3949 } while (exception.retry);
3950
3951out:
3952 if (err == 0)
3953 *clnt = client;
3954 else if (client != *clnt)
3955 rpc_shutdown_client(client);
3956
3957 return err;
3958}
3959
Al Virobeffb8f2016-07-20 16:34:42 -04003960static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003961 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3962 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003964 int status;
3965 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003966
David Quigley1775fd32013-05-22 12:50:42 -04003967 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003968 if (client != NFS_CLIENT(dir)) {
3969 rpc_shutdown_client(client);
3970 nfs_fixup_secinfo_attributes(fattr);
3971 }
3972 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003975struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003976nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003977 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3978{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003979 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003980 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003981
David Quigley1775fd32013-05-22 12:50:42 -04003982 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003983 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003984 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003985 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003986}
3987
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003988static int _nfs4_proc_lookupp(struct inode *inode,
3989 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3990 struct nfs4_label *label)
3991{
3992 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3993 struct nfs_server *server = NFS_SERVER(inode);
3994 int status;
3995 struct nfs4_lookupp_arg args = {
3996 .bitmask = server->attr_bitmask,
3997 .fh = NFS_FH(inode),
3998 };
3999 struct nfs4_lookupp_res res = {
4000 .server = server,
4001 .fattr = fattr,
4002 .label = label,
4003 .fh = fhandle,
4004 };
4005 struct rpc_message msg = {
4006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4007 .rpc_argp = &args,
4008 .rpc_resp = &res,
4009 };
4010
4011 args.bitmask = nfs4_bitmask(server, label);
4012
4013 nfs_fattr_init(fattr);
4014
4015 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4016 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4017 &res.seq_res, 0);
4018 dprintk("NFS reply lookupp: %d\n", status);
4019 return status;
4020}
4021
4022static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4023 struct nfs_fattr *fattr, struct nfs4_label *label)
4024{
4025 struct nfs4_exception exception = { };
4026 int err;
4027 do {
4028 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4029 trace_nfs4_lookupp(inode, err);
4030 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4031 &exception);
4032 } while (exception.retry);
4033 return err;
4034}
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4037{
Trond Myklebust76b32992007-08-10 17:45:11 -04004038 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 struct nfs4_accessargs args = {
4040 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004041 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004042 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004044 struct nfs4_accessres res = {
4045 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004046 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 struct rpc_message msg = {
4048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4049 .rpc_argp = &args,
4050 .rpc_resp = &res,
4051 .rpc_cred = entry->cred,
4052 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004053 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Trond Myklebustc407d412010-04-16 16:22:48 -04004055 res.fattr = nfs_alloc_fattr();
4056 if (res.fattr == NULL)
4057 return -ENOMEM;
4058
Bryan Schumaker7c513052011-03-24 17:12:24 +00004059 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004061 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004062 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004064 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return status;
4066}
4067
4068static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4069{
4070 struct nfs4_exception exception = { };
4071 int err;
4072 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004073 err = _nfs4_proc_access(inode, entry);
4074 trace_nfs4_access(inode, err);
4075 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 &exception);
4077 } while (exception.retry);
4078 return err;
4079}
4080
4081/*
4082 * TODO: For the time being, we don't try to get any attributes
4083 * along with any of the zero-copy operations READ, READDIR,
4084 * READLINK, WRITE.
4085 *
4086 * In the case of the first three, we want to put the GETATTR
4087 * after the read-type operation -- this is because it is hard
4088 * to predict the length of a GETATTR response in v4, and thus
4089 * align the READ data correctly. This means that the GETATTR
4090 * may end up partially falling into the page cache, and we should
4091 * shift it into the 'tail' of the xdr_buf before processing.
4092 * To do this efficiently, we need to know the total length
4093 * of data received, which doesn't seem to be available outside
4094 * of the RPC layer.
4095 *
4096 * In the case of WRITE, we also want to put the GETATTR after
4097 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004098 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 *
4100 * Both of these changes to the XDR layer would in fact be quite
4101 * minor, but I decided to leave them for a subsequent patch.
4102 */
4103static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4104 unsigned int pgbase, unsigned int pglen)
4105{
4106 struct nfs4_readlink args = {
4107 .fh = NFS_FH(inode),
4108 .pgbase = pgbase,
4109 .pglen = pglen,
4110 .pages = &page,
4111 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004112 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 struct rpc_message msg = {
4114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4115 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004116 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 };
4118
Bryan Schumaker7c513052011-03-24 17:12:24 +00004119 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 -07004120}
4121
4122static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4123 unsigned int pgbase, unsigned int pglen)
4124{
4125 struct nfs4_exception exception = { };
4126 int err;
4127 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004128 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4129 trace_nfs4_readlink(inode, err);
4130 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 &exception);
4132 } while (exception.retry);
4133 return err;
4134}
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004137 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139static int
4140nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004141 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004143 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004144 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004145 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 int status = 0;
4148
NeilBrown532d4de2016-10-13 15:26:47 +11004149 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004150 if (IS_ERR(ctx))
4151 return PTR_ERR(ctx);
4152
David Quigleyaa9c2662013-05-22 12:50:44 -04004153 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4154
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004155 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4156 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004157 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 if (IS_ERR(state)) {
4159 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004160 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004163 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004164 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 return status;
4166}
4167
Al Virobeffb8f2016-07-20 16:34:42 -04004168static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
Trond Myklebust16e42952005-10-27 22:12:44 -04004170 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004171 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004173 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004175 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004176 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4180 .rpc_argp = &args,
4181 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004183 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004184 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004185
Bryan Schumaker7c513052011-03-24 17:12:24 +00004186 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004187 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004188 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return status;
4190}
4191
Al Virobeffb8f2016-07-20 16:34:42 -04004192static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 struct nfs4_exception exception = { };
4195 int err;
4196 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004197 err = _nfs4_proc_remove(dir, name);
4198 trace_nfs4_remove(dir, name, err);
4199 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 &exception);
4201 } while (exception.retry);
4202 return err;
4203}
4204
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004205static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004207 struct nfs_server *server = NFS_SERVER(dir);
4208 struct nfs_removeargs *args = msg->rpc_argp;
4209 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004211 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004213 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004214
4215 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004218static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4219{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004220 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004221 &data->args.seq_args,
4222 &data->res.seq_res,
4223 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004226static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004228 struct nfs_unlinkdata *data = task->tk_calldata;
4229 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004230
Trond Myklebust14516c32010-07-31 14:29:06 -04004231 if (!nfs4_sequence_done(task, &res->seq_res))
4232 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004233 if (nfs4_async_handle_error(task, res->server, NULL,
4234 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004235 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004236 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004237 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004238 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
Jeff Laytond3d41522010-09-17 17:31:57 -04004241static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4242{
4243 struct nfs_server *server = NFS_SERVER(dir);
4244 struct nfs_renameargs *arg = msg->rpc_argp;
4245 struct nfs_renameres *res = msg->rpc_resp;
4246
4247 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004248 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004249 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004250}
4251
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004252static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4253{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004254 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004255 &data->args.seq_args,
4256 &data->res.seq_res,
4257 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004258}
4259
4260static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4261 struct inode *new_dir)
4262{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004263 struct nfs_renamedata *data = task->tk_calldata;
4264 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004265
4266 if (!nfs4_sequence_done(task, &res->seq_res))
4267 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004268 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004269 return 0;
4270
Trond Myklebustc733c492017-01-11 12:32:26 -05004271 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004272 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004273 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004274 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004275 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004276 return 1;
4277}
4278
Al Virobeffb8f2016-07-20 16:34:42 -04004279static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004281 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 struct nfs4_link_arg arg = {
4283 .fh = NFS_FH(inode),
4284 .dir_fh = NFS_FH(dir),
4285 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004286 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004288 struct nfs4_link_res res = {
4289 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004290 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004291 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 struct rpc_message msg = {
4293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4294 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004295 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004297 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Trond Myklebust136f2622010-04-16 16:22:49 -04004299 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004300 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004301 goto out;
4302
David Quigley14c43f72013-05-22 12:50:43 -04004303 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4304 if (IS_ERR(res.label)) {
4305 status = PTR_ERR(res.label);
4306 goto out;
4307 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004308 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004309
Bryan Schumaker7c513052011-03-24 17:12:24 +00004310 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004311 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004312 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004313 status = nfs_post_op_update_inode(inode, res.fattr);
4314 if (!status)
4315 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004316 }
David Quigley14c43f72013-05-22 12:50:43 -04004317
4318
4319 nfs4_label_free(res.label);
4320
Trond Myklebust136f2622010-04-16 16:22:49 -04004321out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004322 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return status;
4324}
4325
Al Virobeffb8f2016-07-20 16:34:42 -04004326static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
4328 struct nfs4_exception exception = { };
4329 int err;
4330 do {
4331 err = nfs4_handle_exception(NFS_SERVER(inode),
4332 _nfs4_proc_link(inode, dir, name),
4333 &exception);
4334 } while (exception.retry);
4335 return err;
4336}
4337
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004338struct nfs4_createdata {
4339 struct rpc_message msg;
4340 struct nfs4_create_arg arg;
4341 struct nfs4_create_res res;
4342 struct nfs_fh fh;
4343 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004344 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004345};
4346
4347static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004348 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349{
4350 struct nfs4_createdata *data;
4351
4352 data = kzalloc(sizeof(*data), GFP_KERNEL);
4353 if (data != NULL) {
4354 struct nfs_server *server = NFS_SERVER(dir);
4355
David Quigley14c43f72013-05-22 12:50:43 -04004356 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4357 if (IS_ERR(data->label))
4358 goto out_free;
4359
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004360 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4361 data->msg.rpc_argp = &data->arg;
4362 data->msg.rpc_resp = &data->res;
4363 data->arg.dir_fh = NFS_FH(dir);
4364 data->arg.server = server;
4365 data->arg.name = name;
4366 data->arg.attrs = sattr;
4367 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004368 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004369 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004370 data->res.server = server;
4371 data->res.fh = &data->fh;
4372 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004373 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004374 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004375 }
4376 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004377out_free:
4378 kfree(data);
4379 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004380}
4381
4382static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4383{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004384 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004385 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004387 update_changeattr(dir, &data->res.dir_cinfo,
4388 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004389 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004390 }
4391 return status;
4392}
4393
4394static void nfs4_free_createdata(struct nfs4_createdata *data)
4395{
David Quigley14c43f72013-05-22 12:50:43 -04004396 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004397 kfree(data);
4398}
4399
Chuck Lever4f390c12006-08-22 20:06:22 -04004400static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004401 struct page *page, unsigned int len, struct iattr *sattr,
4402 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004404 struct nfs4_createdata *data;
4405 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
Chuck Lever94a6d752006-08-22 20:06:23 -04004407 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004408 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004409
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004410 status = -ENOMEM;
4411 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4412 if (data == NULL)
4413 goto out;
4414
4415 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4416 data->arg.u.symlink.pages = &page;
4417 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004418 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004420 status = nfs4_do_create(dir, dentry, data);
4421
4422 nfs4_free_createdata(data);
4423out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 return status;
4425}
4426
Chuck Lever4f390c12006-08-22 20:06:22 -04004427static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004428 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
4430 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004431 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004433
4434 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004437 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4438 trace_nfs4_symlink(dir, &dentry->d_name, err);
4439 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 &exception);
4441 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004442
4443 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 return err;
4445}
4446
4447static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004448 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 struct nfs4_createdata *data;
4451 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004453 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4454 if (data == NULL)
4455 goto out;
4456
David Quigley1775fd32013-05-22 12:50:42 -04004457 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004458 status = nfs4_do_create(dir, dentry, data);
4459
4460 nfs4_free_createdata(data);
4461out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 return status;
4463}
4464
4465static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4466 struct iattr *sattr)
4467{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004468 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004470 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004472
David Quigleyaa9c2662013-05-22 12:50:44 -04004473 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4474
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004475 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4476 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004478 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4479 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4480 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 &exception);
4482 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004483 nfs4_label_release_security(label);
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 return err;
4486}
4487
4488static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004489 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
David Howells2b0143b2015-03-17 22:25:59 +00004491 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 struct nfs4_readdir_arg args = {
4493 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004494 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 .pgbase = 0,
4496 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004497 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004498 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 };
4500 struct nfs4_readdir_res res;
4501 struct rpc_message msg = {
4502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4503 .rpc_argp = &args,
4504 .rpc_resp = &res,
4505 .rpc_cred = cred,
4506 };
4507 int status;
4508
Al Viro6de14722013-09-16 10:53:17 -04004509 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4510 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004511 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004512 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004514 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 -05004515 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004516 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004517 status += args.pgbase;
4518 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004519
4520 nfs_invalidate_atime(dir);
4521
Harvey Harrison3110ff82008-05-02 13:42:44 -07004522 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 return status;
4524}
4525
4526static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004527 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
4529 struct nfs4_exception exception = { };
4530 int err;
4531 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004532 err = _nfs4_proc_readdir(dentry, cred, cookie,
4533 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004534 trace_nfs4_readdir(d_inode(dentry), err);
4535 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 &exception);
4537 } while (exception.retry);
4538 return err;
4539}
4540
4541static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004542 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004544 struct nfs4_createdata *data;
4545 int mode = sattr->ia_mode;
4546 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004548 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4549 if (data == NULL)
4550 goto out;
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004553 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004555 data->arg.ftype = NF4BLK;
4556 data->arg.u.device.specdata1 = MAJOR(rdev);
4557 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 }
4559 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004560 data->arg.ftype = NF4CHR;
4561 data->arg.u.device.specdata1 = MAJOR(rdev);
4562 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004563 } else if (!S_ISSOCK(mode)) {
4564 status = -EINVAL;
4565 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
David Quigley1775fd32013-05-22 12:50:42 -04004567
David Quigleyaa9c2662013-05-22 12:50:44 -04004568 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004569 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004570out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004571 nfs4_free_createdata(data);
4572out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 return status;
4574}
4575
4576static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4577 struct iattr *sattr, dev_t rdev)
4578{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004579 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004581 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004583
David Quigleyaa9c2662013-05-22 12:50:44 -04004584 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4585
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004586 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4587 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004589 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4590 trace_nfs4_mknod(dir, &dentry->d_name, err);
4591 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 &exception);
4593 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004594
4595 nfs4_label_release_security(label);
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return err;
4598}
4599
4600static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4601 struct nfs_fsstat *fsstat)
4602{
4603 struct nfs4_statfs_arg args = {
4604 .fh = fhandle,
4605 .bitmask = server->attr_bitmask,
4606 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004607 struct nfs4_statfs_res res = {
4608 .fsstat = fsstat,
4609 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 struct rpc_message msg = {
4611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4612 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004613 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 };
4615
Trond Myklebust0e574af2005-10-27 22:12:38 -04004616 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004617 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618}
4619
4620static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4621{
4622 struct nfs4_exception exception = { };
4623 int err;
4624 do {
4625 err = nfs4_handle_exception(server,
4626 _nfs4_proc_statfs(server, fhandle, fsstat),
4627 &exception);
4628 } while (exception.retry);
4629 return err;
4630}
4631
4632static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4633 struct nfs_fsinfo *fsinfo)
4634{
4635 struct nfs4_fsinfo_arg args = {
4636 .fh = fhandle,
4637 .bitmask = server->attr_bitmask,
4638 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004639 struct nfs4_fsinfo_res res = {
4640 .fsinfo = fsinfo,
4641 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 struct rpc_message msg = {
4643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4644 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004645 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 };
4647
Bryan Schumaker7c513052011-03-24 17:12:24 +00004648 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649}
4650
4651static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4652{
4653 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004654 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 int err;
4656
4657 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004658 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004659 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004660 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004661 nfs4_set_lease_period(server->nfs_client,
4662 fsinfo->lease_time * HZ,
4663 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004664 break;
4665 }
4666 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 } while (exception.retry);
4668 return err;
4669}
4670
4671static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4672{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004673 int error;
4674
Trond Myklebust0e574af2005-10-27 22:12:38 -04004675 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004676 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004677 if (error == 0) {
4678 /* block layout checks this! */
4679 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004680 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004681 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004682
4683 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
4686static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4687 struct nfs_pathconf *pathconf)
4688{
4689 struct nfs4_pathconf_arg args = {
4690 .fh = fhandle,
4691 .bitmask = server->attr_bitmask,
4692 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004693 struct nfs4_pathconf_res res = {
4694 .pathconf = pathconf,
4695 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4698 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004699 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 };
4701
4702 /* None of the pathconf attributes are mandatory to implement */
4703 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4704 memset(pathconf, 0, sizeof(*pathconf));
4705 return 0;
4706 }
4707
Trond Myklebust0e574af2005-10-27 22:12:38 -04004708 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004709 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
4712static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4713 struct nfs_pathconf *pathconf)
4714{
4715 struct nfs4_exception exception = { };
4716 int err;
4717
4718 do {
4719 err = nfs4_handle_exception(server,
4720 _nfs4_proc_pathconf(server, fhandle, pathconf),
4721 &exception);
4722 } while (exception.retry);
4723 return err;
4724}
4725
Trond Myklebust5521abf2013-03-16 20:54:34 -04004726int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004727 const struct nfs_open_context *ctx,
4728 const struct nfs_lock_context *l_ctx,
4729 fmode_t fmode)
4730{
NeilBrown17393472016-10-13 15:26:47 +11004731 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004732}
4733EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4734
Trond Myklebust5521abf2013-03-16 20:54:34 -04004735static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4736 const struct nfs_open_context *ctx,
4737 const struct nfs_lock_context *l_ctx,
4738 fmode_t fmode)
4739{
4740 nfs4_stateid current_stateid;
4741
Trond Myklebuste1253be2014-03-05 08:44:23 -05004742 /* If the current stateid represents a lost lock, then exit */
4743 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4744 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004745 return nfs4_stateid_match(stateid, &current_stateid);
4746}
4747
4748static bool nfs4_error_stateid_expired(int err)
4749{
4750 switch (err) {
4751 case -NFS4ERR_DELEG_REVOKED:
4752 case -NFS4ERR_ADMIN_REVOKED:
4753 case -NFS4ERR_BAD_STATEID:
4754 case -NFS4ERR_STALE_STATEID:
4755 case -NFS4ERR_OLD_STATEID:
4756 case -NFS4ERR_OPENMODE:
4757 case -NFS4ERR_EXPIRED:
4758 return true;
4759 }
4760 return false;
4761}
4762
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004763static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004765 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004767 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004768 if (task->tk_status < 0) {
4769 struct nfs4_exception exception = {
4770 .inode = hdr->inode,
4771 .state = hdr->args.context->state,
4772 .stateid = &hdr->args.stateid,
4773 };
4774 task->tk_status = nfs4_async_handle_exception(task,
4775 server, task->tk_status, &exception);
4776 if (exception.retry) {
4777 rpc_restart_call_prepare(task);
4778 return -EAGAIN;
4779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004783 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
Trond Myklebust5521abf2013-03-16 20:54:34 -04004787static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004788 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004789{
4790
4791 if (!nfs4_error_stateid_expired(task->tk_status) ||
4792 nfs4_stateid_is_current(&args->stateid,
4793 args->context,
4794 args->lock_context,
4795 FMODE_READ))
4796 return false;
4797 rpc_restart_call_prepare(task);
4798 return true;
4799}
4800
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004801static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004802{
4803
4804 dprintk("--> %s\n", __func__);
4805
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004806 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004807 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004808 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004809 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004810 if (task->tk_status > 0)
4811 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004812 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4813 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004814}
4815
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004816static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4817 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004819 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004820 if (!hdr->pgio_done_cb)
4821 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004822 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004823 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824}
4825
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004826static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4827 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004828{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004829 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004830 &hdr->args.seq_args,
4831 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004832 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004833 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004834 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4835 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004836 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004837 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004838 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004839 return -EIO;
4840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841}
4842
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004843static int nfs4_write_done_cb(struct rpc_task *task,
4844 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004846 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004847
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004848 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004849 if (task->tk_status < 0) {
4850 struct nfs4_exception exception = {
4851 .inode = hdr->inode,
4852 .state = hdr->args.context->state,
4853 .stateid = &hdr->args.stateid,
4854 };
4855 task->tk_status = nfs4_async_handle_exception(task,
4856 NFS_SERVER(inode), task->tk_status,
4857 &exception);
4858 if (exception.retry) {
4859 rpc_restart_call_prepare(task);
4860 return -EAGAIN;
4861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004863 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004864 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004865 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004866 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
Trond Myklebust5521abf2013-03-16 20:54:34 -04004870static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004871 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004872{
4873
4874 if (!nfs4_error_stateid_expired(task->tk_status) ||
4875 nfs4_stateid_is_current(&args->stateid,
4876 args->context,
4877 args->lock_context,
4878 FMODE_WRITE))
4879 return false;
4880 rpc_restart_call_prepare(task);
4881 return true;
4882}
4883
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004884static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004885{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004886 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004887 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004888 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004889 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004890 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4891 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004892}
4893
Trond Myklebust5a37f852012-04-28 14:55:16 -04004894static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004895bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004896{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004897 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004898 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004899 return false;
4900 /* Otherwise, request attributes if and only if we don't hold
4901 * a delegation
4902 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004903 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004904}
Fred Isamana69aef12011-03-03 15:13:47 +00004905
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004906static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4907 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004909 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004910
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004911 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4912 hdr->args.bitmask = NULL;
4913 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004914 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004915 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004916
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004917 if (!hdr->pgio_done_cb)
4918 hdr->pgio_done_cb = nfs4_write_done_cb;
4919 hdr->res.server = server;
4920 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004922 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004923 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
Fred Isaman0b7c0152012-04-20 14:47:39 -04004926static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4927{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004928 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004929 &data->args.seq_args,
4930 &data->res.seq_res,
4931 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932}
4933
Fred Isaman0b7c0152012-04-20 14:47:39 -04004934static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004937
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004938 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004939 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4940 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004941 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004942 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
4946
Fred Isaman0b7c0152012-04-20 14:47:39 -04004947static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004948{
4949 if (!nfs4_sequence_done(task, &data->res.seq_res))
4950 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004951 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004952}
4953
Fred Isaman0b7c0152012-04-20 14:47:39 -04004954static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004956 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004957
Fred Isaman0b7c0152012-04-20 14:47:39 -04004958 if (data->commit_done_cb == NULL)
4959 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004960 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004961 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004962 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963}
4964
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004965struct nfs4_renewdata {
4966 struct nfs_client *client;
4967 unsigned long timestamp;
4968};
4969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970/*
4971 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4972 * standalone procedure for queueing an asynchronous RENEW.
4973 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004974static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004975{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004976 struct nfs4_renewdata *data = calldata;
4977 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004978
Elena Reshetova212bf412017-10-20 12:53:38 +03004979 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004980 nfs4_schedule_state_renewal(clp);
4981 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004982 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004983}
4984
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004985static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004987 struct nfs4_renewdata *data = calldata;
4988 struct nfs_client *clp = data->client;
4989 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004991 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004992 switch (task->tk_status) {
4993 case 0:
4994 break;
4995 case -NFS4ERR_LEASE_MOVED:
4996 nfs4_schedule_lease_moved_recovery(clp);
4997 break;
4998 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004999 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005000 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5001 return;
5002 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005003 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005004 return;
5005 }
5006 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005008 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005011static const struct rpc_call_ops nfs4_renew_ops = {
5012 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005013 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005014};
5015
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005016static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017{
5018 struct rpc_message msg = {
5019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5020 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005021 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005023 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005025 if (renew_flags == 0)
5026 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005027 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005028 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005029 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005030 if (data == NULL) {
5031 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005032 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005033 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005034 data->client = clp;
5035 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005036 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005037 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005040static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
5042 struct rpc_message msg = {
5043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5044 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005045 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 };
5047 unsigned long now = jiffies;
5048 int status;
5049
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005050 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 if (status < 0)
5052 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005053 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 return 0;
5055}
5056
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005057static inline int nfs4_server_supports_acls(struct nfs_server *server)
5058{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005059 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005060}
5061
Trond Myklebust21f498c2012-08-24 10:59:25 -04005062/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5063 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005064 * the stack.
5065 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005066#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005067
Neil Hormane9e3d722011-03-04 19:26:03 -05005068static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005069 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005070{
5071 struct page *newpage, **spages;
5072 int rc = 0;
5073 size_t len;
5074 spages = pages;
5075
5076 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005077 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005078 newpage = alloc_page(GFP_KERNEL);
5079
5080 if (newpage == NULL)
5081 goto unwind;
5082 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005083 buf += len;
5084 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005085 *pages++ = newpage;
5086 rc++;
5087 } while (buflen != 0);
5088
5089 return rc;
5090
5091unwind:
5092 for(; rc > 0; rc--)
5093 __free_page(spages[rc-1]);
5094 return -ENOMEM;
5095}
5096
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005097struct nfs4_cached_acl {
5098 int cached;
5099 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005100 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005101};
5102
5103static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005104{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005105 struct nfs_inode *nfsi = NFS_I(inode);
5106
5107 spin_lock(&inode->i_lock);
5108 kfree(nfsi->nfs4_acl);
5109 nfsi->nfs4_acl = acl;
5110 spin_unlock(&inode->i_lock);
5111}
5112
5113static void nfs4_zap_acl_attr(struct inode *inode)
5114{
5115 nfs4_set_cached_acl(inode, NULL);
5116}
5117
5118static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5119{
5120 struct nfs_inode *nfsi = NFS_I(inode);
5121 struct nfs4_cached_acl *acl;
5122 int ret = -ENOENT;
5123
5124 spin_lock(&inode->i_lock);
5125 acl = nfsi->nfs4_acl;
5126 if (acl == NULL)
5127 goto out;
5128 if (buf == NULL) /* user is just asking for length */
5129 goto out_len;
5130 if (acl->cached == 0)
5131 goto out;
5132 ret = -ERANGE; /* see getxattr(2) man page */
5133 if (acl->len > buflen)
5134 goto out;
5135 memcpy(buf, acl->data, acl->len);
5136out_len:
5137 ret = acl->len;
5138out:
5139 spin_unlock(&inode->i_lock);
5140 return ret;
5141}
5142
Sachin Prabhu5794d212012-04-17 14:36:40 +01005143static 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 +00005144{
5145 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005146 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005147
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005148 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005149 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005150 if (acl == NULL)
5151 goto out;
5152 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005153 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005154 } else {
5155 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5156 if (acl == NULL)
5157 goto out;
5158 acl->cached = 0;
5159 }
5160 acl->len = acl_len;
5161out:
5162 nfs4_set_cached_acl(inode, acl);
5163}
5164
Andy Adamsonbf118a32011-12-07 11:55:27 -05005165/*
5166 * The getxattr API returns the required buffer length when called with a
5167 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5168 * the required buf. On a NULL buf, we send a page of data to the server
5169 * guessing that the ACL request can be serviced by a page. If so, we cache
5170 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5171 * the cache. If not so, we throw away the page, and cache the required
5172 * length. The next getxattr call will then produce another round trip to
5173 * the server, this time with the input buf of the required size.
5174 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005175static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005176{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005177 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005178 struct nfs_getaclargs args = {
5179 .fh = NFS_FH(inode),
5180 .acl_pages = pages,
5181 .acl_len = buflen,
5182 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005183 struct nfs_getaclres res = {
5184 .acl_len = buflen,
5185 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005186 struct rpc_message msg = {
5187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5188 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005189 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005190 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005191 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005192 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005193
Trond Myklebust21f498c2012-08-24 10:59:25 -04005194 if (npages > ARRAY_SIZE(pages))
5195 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005196
Andy Adamsonbf118a32011-12-07 11:55:27 -05005197 for (i = 0; i < npages; i++) {
5198 pages[i] = alloc_page(GFP_KERNEL);
5199 if (!pages[i])
5200 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005201 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005202
5203 /* for decoding across pages */
5204 res.acl_scratch = alloc_page(GFP_KERNEL);
5205 if (!res.acl_scratch)
5206 goto out_free;
5207
Andy Adamsonbf118a32011-12-07 11:55:27 -05005208 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005209
Peng Taode040be2012-01-10 22:42:47 +08005210 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005211 __func__, buf, buflen, npages, args.acl_len);
5212 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5213 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005214 if (ret)
5215 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005216
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005217 /* Handle the case where the passed-in buffer is too short */
5218 if (res.acl_flags & NFS4_ACL_TRUNC) {
5219 /* Did the user only issue a request for the acl length? */
5220 if (buf == NULL)
5221 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005222 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005223 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005224 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005225 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005226 if (buf) {
5227 if (res.acl_len > buflen) {
5228 ret = -ERANGE;
5229 goto out_free;
5230 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005231 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005232 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005233out_ok:
5234 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005235out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005236 for (i = 0; i < npages; i++)
5237 if (pages[i])
5238 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005239 if (res.acl_scratch)
5240 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005241 return ret;
5242}
5243
Trond Myklebust16b42892006-08-24 12:27:15 -04005244static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5245{
5246 struct nfs4_exception exception = { };
5247 ssize_t ret;
5248 do {
5249 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005250 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005251 if (ret >= 0)
5252 break;
5253 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5254 } while (exception.retry);
5255 return ret;
5256}
5257
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005258static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5259{
5260 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005261 int ret;
5262
5263 if (!nfs4_server_supports_acls(server))
5264 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005265 ret = nfs_revalidate_inode(server, inode);
5266 if (ret < 0)
5267 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005268 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5269 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005270 ret = nfs4_read_cached_acl(inode, buf, buflen);
5271 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005272 /* -ENOENT is returned if there is no ACL or if there is an ACL
5273 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005274 return ret;
5275 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005276}
5277
Trond Myklebust16b42892006-08-24 12:27:15 -04005278static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005279{
5280 struct nfs_server *server = NFS_SERVER(inode);
5281 struct page *pages[NFS4ACL_MAXPAGES];
5282 struct nfs_setaclargs arg = {
5283 .fh = NFS_FH(inode),
5284 .acl_pages = pages,
5285 .acl_len = buflen,
5286 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005287 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005288 struct rpc_message msg = {
5289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5290 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005291 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005292 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005293 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005294 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005295
5296 if (!nfs4_server_supports_acls(server))
5297 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005298 if (npages > ARRAY_SIZE(pages))
5299 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005300 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005301 if (i < 0)
5302 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005303 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005304 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005305
5306 /*
5307 * Free each page after tx, so the only ref left is
5308 * held by the network stack
5309 */
5310 for (; i > 0; i--)
5311 put_page(pages[i-1]);
5312
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005313 /*
5314 * Acl update can result in inode attribute update.
5315 * so mark the attribute cache invalid.
5316 */
5317 spin_lock(&inode->i_lock);
5318 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5319 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005320 nfs_access_zap_cache(inode);
5321 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005322 return ret;
5323}
5324
Trond Myklebust16b42892006-08-24 12:27:15 -04005325static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5326{
5327 struct nfs4_exception exception = { };
5328 int err;
5329 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005330 err = __nfs4_proc_set_acl(inode, buf, buflen);
5331 trace_nfs4_set_acl(inode, err);
5332 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005333 &exception);
5334 } while (exception.retry);
5335 return err;
5336}
5337
David Quigleyaa9c2662013-05-22 12:50:44 -04005338#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5339static int _nfs4_get_security_label(struct inode *inode, void *buf,
5340 size_t buflen)
5341{
5342 struct nfs_server *server = NFS_SERVER(inode);
5343 struct nfs_fattr fattr;
5344 struct nfs4_label label = {0, 0, buflen, buf};
5345
5346 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005347 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005348 .fh = NFS_FH(inode),
5349 .bitmask = bitmask,
5350 };
5351 struct nfs4_getattr_res res = {
5352 .fattr = &fattr,
5353 .label = &label,
5354 .server = server,
5355 };
5356 struct rpc_message msg = {
5357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005358 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005359 .rpc_resp = &res,
5360 };
5361 int ret;
5362
5363 nfs_fattr_init(&fattr);
5364
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005365 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005366 if (ret)
5367 return ret;
5368 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5369 return -ENOENT;
5370 if (buflen < label.len)
5371 return -ERANGE;
5372 return 0;
5373}
5374
5375static int nfs4_get_security_label(struct inode *inode, void *buf,
5376 size_t buflen)
5377{
5378 struct nfs4_exception exception = { };
5379 int err;
5380
5381 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5382 return -EOPNOTSUPP;
5383
5384 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005385 err = _nfs4_get_security_label(inode, buf, buflen);
5386 trace_nfs4_get_security_label(inode, err);
5387 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005388 &exception);
5389 } while (exception.retry);
5390 return err;
5391}
5392
5393static int _nfs4_do_set_security_label(struct inode *inode,
5394 struct nfs4_label *ilabel,
5395 struct nfs_fattr *fattr,
5396 struct nfs4_label *olabel)
5397{
5398
5399 struct iattr sattr = {0};
5400 struct nfs_server *server = NFS_SERVER(inode);
5401 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005402 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005403 .fh = NFS_FH(inode),
5404 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005405 .server = server,
5406 .bitmask = bitmask,
5407 .label = ilabel,
5408 };
5409 struct nfs_setattrres res = {
5410 .fattr = fattr,
5411 .label = olabel,
5412 .server = server,
5413 };
5414 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5416 .rpc_argp = &arg,
5417 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005418 };
5419 int status;
5420
Jeff Layton12207f62013-11-01 10:49:32 -04005421 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005422
Jeff Layton12207f62013-11-01 10:49:32 -04005423 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005424 if (status)
5425 dprintk("%s failed: %d\n", __func__, status);
5426
5427 return status;
5428}
5429
5430static int nfs4_do_set_security_label(struct inode *inode,
5431 struct nfs4_label *ilabel,
5432 struct nfs_fattr *fattr,
5433 struct nfs4_label *olabel)
5434{
5435 struct nfs4_exception exception = { };
5436 int err;
5437
5438 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005439 err = _nfs4_do_set_security_label(inode, ilabel,
5440 fattr, olabel);
5441 trace_nfs4_set_security_label(inode, err);
5442 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005443 &exception);
5444 } while (exception.retry);
5445 return err;
5446}
5447
5448static int
Al Viro59301222016-05-27 10:19:30 -04005449nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005450{
5451 struct nfs4_label ilabel, *olabel = NULL;
5452 struct nfs_fattr fattr;
5453 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005454 int status;
5455
5456 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5457 return -EOPNOTSUPP;
5458
5459 nfs_fattr_init(&fattr);
5460
5461 ilabel.pi = 0;
5462 ilabel.lfs = 0;
5463 ilabel.label = (char *)buf;
5464 ilabel.len = buflen;
5465
5466 cred = rpc_lookup_cred();
5467 if (IS_ERR(cred))
5468 return PTR_ERR(cred);
5469
5470 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5471 if (IS_ERR(olabel)) {
5472 status = -PTR_ERR(olabel);
5473 goto out;
5474 }
5475
5476 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5477 if (status == 0)
5478 nfs_setsecurity(inode, &fattr, olabel);
5479
5480 nfs4_label_free(olabel);
5481out:
5482 put_rpccred(cred);
5483 return status;
5484}
5485#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5486
5487
Chuck Leverf0920752012-05-21 22:45:41 -04005488static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5489 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005490{
5491 __be32 verf[2];
5492
Chuck Lever2c820d92012-05-21 22:45:33 -04005493 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5494 /* An impossible timestamp guarantees this value
5495 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005496 verf[0] = cpu_to_be32(U32_MAX);
5497 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005498 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005499 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005500 u64 ns = ktime_to_ns(nn->boot_time);
5501
5502 verf[0] = cpu_to_be32(ns >> 32);
5503 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005504 }
Chuck Levercd937102012-03-02 17:14:31 -05005505 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5506}
5507
Jeff Laytona3192682015-06-09 19:43:59 -04005508static int
5509nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005510{
Jeff Laytona3192682015-06-09 19:43:59 -04005511 size_t len;
5512 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005513
Trond Myklebustceb3a162015-01-03 15:16:04 -05005514 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005515 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005516
Jeff Laytona3192682015-06-09 19:43:59 -04005517 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005518 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005519 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5520 1 +
5521 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5522 1;
5523 rcu_read_unlock();
5524
5525 if (len > NFS4_OPAQUE_LIMIT + 1)
5526 return -EINVAL;
5527
5528 /*
5529 * Since this string is allocated at mount time, and held until the
5530 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5531 * about a memory-reclaim deadlock.
5532 */
5533 str = kmalloc(len, GFP_KERNEL);
5534 if (!str)
5535 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005536
Chuck Levere984a552012-09-14 17:24:21 -04005537 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005538 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005539 clp->cl_ipaddr,
5540 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5541 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005542 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005543
Jeff Laytona3192682015-06-09 19:43:59 -04005544 clp->cl_owner_id = str;
5545 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005546}
5547
Jeff Layton873e3852015-06-09 19:44:00 -04005548static int
5549nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005550{
Jeff Layton873e3852015-06-09 19:44:00 -04005551 size_t len;
5552 char *str;
5553
5554 len = 10 + 10 + 1 + 10 + 1 +
5555 strlen(nfs4_client_id_uniquifier) + 1 +
5556 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5557
5558 if (len > NFS4_OPAQUE_LIMIT + 1)
5559 return -EINVAL;
5560
5561 /*
5562 * Since this string is allocated at mount time, and held until the
5563 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5564 * about a memory-reclaim deadlock.
5565 */
5566 str = kmalloc(len, GFP_KERNEL);
5567 if (!str)
5568 return -ENOMEM;
5569
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005570 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005571 clp->rpc_ops->version, clp->cl_minorversion,
5572 nfs4_client_id_uniquifier,
5573 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005574 clp->cl_owner_id = str;
5575 return 0;
5576}
5577
5578static int
5579nfs4_init_uniform_client_string(struct nfs_client *clp)
5580{
Jeff Layton873e3852015-06-09 19:44:00 -04005581 size_t len;
5582 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005583
5584 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005585 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005586
5587 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005588 return nfs4_init_uniquifier_client_string(clp);
5589
5590 len = 10 + 10 + 1 + 10 + 1 +
5591 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5592
5593 if (len > NFS4_OPAQUE_LIMIT + 1)
5594 return -EINVAL;
5595
5596 /*
5597 * Since this string is allocated at mount time, and held until the
5598 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5599 * about a memory-reclaim deadlock.
5600 */
5601 str = kmalloc(len, GFP_KERNEL);
5602 if (!str)
5603 return -ENOMEM;
5604
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005605 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005606 clp->rpc_ops->version, clp->cl_minorversion,
5607 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005608 clp->cl_owner_id = str;
5609 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005610}
5611
Chuck Lever706cb8d2014-03-12 12:51:47 -04005612/*
5613 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5614 * services. Advertise one based on the address family of the
5615 * clientaddr.
5616 */
5617static unsigned int
5618nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5619{
5620 if (strchr(clp->cl_ipaddr, ':') != NULL)
5621 return scnprintf(buf, len, "tcp6");
5622 else
5623 return scnprintf(buf, len, "tcp");
5624}
5625
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005626static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5627{
5628 struct nfs4_setclientid *sc = calldata;
5629
5630 if (task->tk_status == 0)
5631 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5632}
5633
5634static const struct rpc_call_ops nfs4_setclientid_ops = {
5635 .rpc_call_done = nfs4_setclientid_done,
5636};
5637
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005638/**
5639 * nfs4_proc_setclientid - Negotiate client ID
5640 * @clp: state data structure
5641 * @program: RPC program for NFSv4 callback service
5642 * @port: IP port number for NFS4 callback service
5643 * @cred: RPC credential to use for this call
5644 * @res: where to place the result
5645 *
5646 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5647 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005648int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5649 unsigned short port, struct rpc_cred *cred,
5650 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651{
5652 nfs4_verifier sc_verifier;
5653 struct nfs4_setclientid setclientid = {
5654 .sc_verifier = &sc_verifier,
5655 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005656 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 };
5658 struct rpc_message msg = {
5659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5660 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005661 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005662 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005664 struct rpc_task *task;
5665 struct rpc_task_setup task_setup_data = {
5666 .rpc_client = clp->cl_rpcclient,
5667 .rpc_message = &msg,
5668 .callback_ops = &nfs4_setclientid_ops,
5669 .callback_data = &setclientid,
5670 .flags = RPC_TASK_TIMEOUT,
5671 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005672 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Chuck Leverde734832012-07-11 16:30:50 -04005674 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005675 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005676
5677 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5678 status = nfs4_init_uniform_client_string(clp);
5679 else
Jeff Laytona3192682015-06-09 19:43:59 -04005680 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005681
5682 if (status)
5683 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005684
Chuck Leverde734832012-07-11 16:30:50 -04005685 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005686 setclientid.sc_netid_len =
5687 nfs4_init_callback_netid(clp,
5688 setclientid.sc_netid,
5689 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005690 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005691 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 clp->cl_ipaddr, port >> 8, port & 255);
5693
Jeff Layton3a6bb732015-06-09 19:43:57 -04005694 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005695 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005696 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005697 task = rpc_run_task(&task_setup_data);
5698 if (IS_ERR(task)) {
5699 status = PTR_ERR(task);
5700 goto out;
5701 }
5702 status = task->tk_status;
5703 if (setclientid.sc_cred) {
5704 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5705 put_rpccred(setclientid.sc_cred);
5706 }
5707 rpc_put_task(task);
5708out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005709 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005710 dprintk("NFS reply setclientid: %d\n", status);
5711 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712}
5713
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005714/**
5715 * nfs4_proc_setclientid_confirm - Confirm client ID
5716 * @clp: state data structure
5717 * @res: result of a previous SETCLIENTID
5718 * @cred: RPC credential to use for this call
5719 *
5720 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5721 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005722int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005723 struct nfs4_setclientid_res *arg,
5724 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 struct rpc_message msg = {
5727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005728 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005729 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 int status;
5732
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005733 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5734 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5735 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005736 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005737 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005738 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 return status;
5740}
5741
Trond Myklebustfe650402006-01-03 09:55:18 +01005742struct nfs4_delegreturndata {
5743 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005744 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005745 struct nfs_fh fh;
5746 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005747 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005748 struct {
5749 struct nfs4_layoutreturn_args arg;
5750 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005751 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005752 u32 roc_barrier;
5753 bool roc;
5754 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005755 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005756 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005757 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005758};
5759
Trond Myklebustfe650402006-01-03 09:55:18 +01005760static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5761{
5762 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005763
Trond Myklebust14516c32010-07-31 14:29:06 -04005764 if (!nfs4_sequence_done(task, &data->res.seq_res))
5765 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005766
Trond Myklebustca8acf82013-08-13 10:36:56 -04005767 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005768
5769 /* Handle Layoutreturn errors */
5770 if (data->args.lr_args && task->tk_status != 0) {
5771 switch(data->res.lr_ret) {
5772 default:
5773 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5774 break;
5775 case 0:
5776 data->args.lr_args = NULL;
5777 data->res.lr_res = NULL;
5778 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005779 case -NFS4ERR_OLD_STATEID:
5780 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005781 data->inode))
5782 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005783 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005784 case -NFS4ERR_ADMIN_REVOKED:
5785 case -NFS4ERR_DELEG_REVOKED:
5786 case -NFS4ERR_EXPIRED:
5787 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005788 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5789 case -NFS4ERR_WRONG_CRED:
5790 data->args.lr_args = NULL;
5791 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005792 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005793 }
5794 }
5795
Ricardo Labiaga79708862009-12-07 09:23:21 -05005796 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005797 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005798 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005799 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005800 case -NFS4ERR_ADMIN_REVOKED:
5801 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005802 case -NFS4ERR_EXPIRED:
5803 nfs4_free_revoked_stateid(data->res.server,
5804 data->args.stateid,
5805 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005806 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005807 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005808 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005809 task->tk_status = 0;
5810 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005811 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005812 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5813 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005814 task->tk_status = 0;
5815 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005816 case -NFS4ERR_ACCESS:
5817 if (data->args.bitmask) {
5818 data->args.bitmask = NULL;
5819 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005820 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005821 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005822 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005823 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005824 if (nfs4_async_handle_error(task, data->res.server,
5825 NULL, NULL) == -EAGAIN) {
Trond Myklebust140087fd2017-11-06 15:28:10 -05005826 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005827 }
5828 }
5829 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005830 return;
5831lr_restart:
5832 data->res.lr_ret = 0;
5833out_restart:
5834 task->tk_status = 0;
5835 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005836}
5837
5838static void nfs4_delegreturn_release(void *calldata)
5839{
Peng Tao039b7562014-07-03 13:05:02 +08005840 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005841 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005842
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005843 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005844 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005845 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5846 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005847 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005848 nfs_iput_and_deactive(inode);
5849 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005850 kfree(calldata);
5851}
5852
Andy Adamson938e1012009-04-01 09:22:28 -04005853static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5854{
5855 struct nfs4_delegreturndata *d_data;
5856
5857 d_data = (struct nfs4_delegreturndata *)data;
5858
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005859 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005860 return;
5861
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005862 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005863 &d_data->args.seq_args,
5864 &d_data->res.seq_res,
5865 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005866}
Andy Adamson938e1012009-04-01 09:22:28 -04005867
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005868static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005869 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005870 .rpc_call_done = nfs4_delegreturn_done,
5871 .rpc_release = nfs4_delegreturn_release,
5872};
5873
Trond Myklebuste6f81072008-01-24 18:14:34 -05005874static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005875{
5876 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005877 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005878 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005879 struct rpc_message msg = {
5880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5881 .rpc_cred = cred,
5882 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005883 struct rpc_task_setup task_setup_data = {
5884 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005885 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005886 .callback_ops = &nfs4_delegreturn_ops,
5887 .flags = RPC_TASK_ASYNC,
5888 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005889 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005890
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005891 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005892 if (data == NULL)
5893 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005894 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005895
5896 nfs4_state_protect(server->nfs_client,
5897 NFS_SP4_MACH_CRED_CLEANUP,
5898 &task_setup_data.rpc_client, &msg);
5899
Trond Myklebustfe650402006-01-03 09:55:18 +01005900 data->args.fhandle = &data->fh;
5901 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005902 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005903 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005904 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005905 data->res.fattr = &data->fattr;
5906 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005907 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005908 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005909 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005910 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005911 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005912 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005913 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005914 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005915 if (data->lr.roc) {
5916 data->args.lr_args = &data->lr.arg;
5917 data->res.lr_res = &data->lr.res;
5918 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005919 } else if (data->lr.roc) {
5920 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5921 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005922 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005923
Trond Myklebustc970aa82007-07-14 15:39:59 -04005924 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005925 msg.rpc_argp = &data->args;
5926 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005927 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005928 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005929 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005930 if (!issync)
5931 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005932 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005933 if (status != 0)
5934 goto out;
5935 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005936out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005937 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005938 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939}
5940
Trond Myklebuste6f81072008-01-24 18:14:34 -05005941int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942{
5943 struct nfs_server *server = NFS_SERVER(inode);
5944 struct nfs4_exception exception = { };
5945 int err;
5946 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005947 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005948 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 switch (err) {
5950 case -NFS4ERR_STALE_STATEID:
5951 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 case 0:
5953 return 0;
5954 }
5955 err = nfs4_handle_exception(server, err, &exception);
5956 } while (exception.retry);
5957 return err;
5958}
5959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5961{
5962 struct inode *inode = state->inode;
5963 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005964 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005965 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005967 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005969 struct nfs_lockt_res res = {
5970 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 };
5972 struct rpc_message msg = {
5973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005974 .rpc_argp = &arg,
5975 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 .rpc_cred = state->owner->so_cred,
5977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 struct nfs4_lock_state *lsp;
5979 int status;
5980
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005981 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005982 status = nfs4_set_lock_state(state, request);
5983 if (status != 0)
5984 goto out;
5985 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005986 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005987 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005988 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005989 switch (status) {
5990 case 0:
5991 request->fl_type = F_UNLCK;
5992 break;
5993 case -NFS4ERR_DENIED:
5994 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005996 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005997 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005998out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 return status;
6000}
6001
6002static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6003{
6004 struct nfs4_exception exception = { };
6005 int err;
6006
6007 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006008 err = _nfs4_proc_getlk(state, cmd, request);
6009 trace_nfs4_get_lock(request, state, cmd, err);
6010 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 &exception);
6012 } while (exception.retry);
6013 return err;
6014}
6015
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006016struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006017 struct nfs_locku_args arg;
6018 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006019 struct nfs4_lock_state *lsp;
6020 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006021 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006022 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006023 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006024 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006025};
6026
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006027static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6028 struct nfs_open_context *ctx,
6029 struct nfs4_lock_state *lsp,
6030 struct nfs_seqid *seqid)
6031{
6032 struct nfs4_unlockdata *p;
6033 struct inode *inode = lsp->ls_state->inode;
6034
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006035 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006036 if (p == NULL)
6037 return NULL;
6038 p->arg.fh = NFS_FH(inode);
6039 p->arg.fl = &p->fl;
6040 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006041 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006042 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006043 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006044 /* Ensure we don't close file until we're done freeing locks! */
6045 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006046 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006047 memcpy(&p->fl, fl, sizeof(p->fl));
6048 p->server = NFS_SERVER(inode);
6049 return p;
6050}
6051
Trond Myklebust06f814a2006-01-03 09:55:07 +01006052static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006053{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006054 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006055 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006056 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006057 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006058 put_nfs_open_context(calldata->ctx);
6059 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006060}
6061
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006062static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006063{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006064 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006065
Trond Myklebust14516c32010-07-31 14:29:06 -04006066 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6067 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006068 switch (task->tk_status) {
6069 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006070 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006071 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006072 if (nfs4_update_lock_stateid(calldata->lsp,
6073 &calldata->res.stateid))
6074 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006075 case -NFS4ERR_ADMIN_REVOKED:
6076 case -NFS4ERR_EXPIRED:
6077 nfs4_free_revoked_stateid(calldata->server,
6078 &calldata->arg.stateid,
6079 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006080 case -NFS4ERR_BAD_STATEID:
6081 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006082 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006083 if (!nfs4_stateid_match(&calldata->arg.stateid,
6084 &calldata->lsp->ls_stateid))
6085 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006086 break;
6087 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10006088 if (nfs4_async_handle_error(task, calldata->server,
6089 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006090 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006091 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006092 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006093}
6094
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006095static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006096{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006097 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006099 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6100 nfs_async_iocounter_wait(task, calldata->l_ctx))
6101 return;
6102
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006103 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006104 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006105 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006106 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006107 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006108 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006109 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006110 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006111 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006112 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006113 &calldata->res.seq_res,
6114 task) != 0)
6115 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006116 return;
6117out_no_action:
6118 task->tk_action = NULL;
6119out_wait:
6120 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121}
6122
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006123static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006124 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006125 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006126 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006127};
6128
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006129static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6130 struct nfs_open_context *ctx,
6131 struct nfs4_lock_state *lsp,
6132 struct nfs_seqid *seqid)
6133{
6134 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006135 struct rpc_message msg = {
6136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6137 .rpc_cred = ctx->cred,
6138 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006139 struct rpc_task_setup task_setup_data = {
6140 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006141 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006142 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006143 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006144 .flags = RPC_TASK_ASYNC,
6145 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006146
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006147 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6148 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6149
Frank Filz137d6ac2007-07-09 15:32:29 -07006150 /* Ensure this is an unlock - when canceling a lock, the
6151 * canceled lock is passed in, and it won't be an unlock.
6152 */
6153 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006154 if (fl->fl_flags & FL_CLOSE)
6155 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006156
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006157 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6158 if (data == NULL) {
6159 nfs_free_seqid(seqid);
6160 return ERR_PTR(-ENOMEM);
6161 }
6162
Chuck Levera9c92d62013-08-09 12:48:18 -04006163 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006164 msg.rpc_argp = &data->arg;
6165 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006166 task_setup_data.callback_data = data;
6167 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006168}
6169
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6171{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006172 struct inode *inode = state->inode;
6173 struct nfs4_state_owner *sp = state->owner;
6174 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006175 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006176 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006177 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006178 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006179 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006180 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Trond Myklebust9b073572006-06-29 16:38:34 -04006182 status = nfs4_set_lock_state(state, request);
6183 /* Unlock _before_ we do the RPC call */
6184 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006185 /* Exclude nfs_delegation_claim_locks() */
6186 mutex_lock(&sp->so_delegreturn_mutex);
6187 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006188 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006189 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006190 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006191 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006192 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006193 }
6194 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006195 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006196 if (status != 0)
6197 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006198 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006199 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006200 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6201 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006202 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6203 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006204 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006205 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006206 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006207 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006208 status = PTR_ERR(task);
6209 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006210 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006211 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006212 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006213out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006214 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006215 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006216 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217}
6218
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219struct nfs4_lockdata {
6220 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006221 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 struct nfs4_lock_state *lsp;
6223 struct nfs_open_context *ctx;
6224 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006225 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006226 int rpc_status;
6227 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006228 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006229};
6230
6231static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006232 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6233 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006234{
6235 struct nfs4_lockdata *p;
6236 struct inode *inode = lsp->ls_state->inode;
6237 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006238 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006239
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006240 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241 if (p == NULL)
6242 return NULL;
6243
6244 p->arg.fh = NFS_FH(inode);
6245 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006246 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006247 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006248 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006249 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6250 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006251 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006252 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006253 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006254 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006255 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006256 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006257 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006258 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006259 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006260 p->ctx = get_nfs_open_context(ctx);
6261 memcpy(&p->fl, fl, sizeof(p->fl));
6262 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006263out_free_seqid:
6264 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006265out_free:
6266 kfree(p);
6267 return NULL;
6268}
6269
6270static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6271{
6272 struct nfs4_lockdata *data = calldata;
6273 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
Harvey Harrison3110ff82008-05-02 13:42:44 -07006275 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006276 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006277 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006278 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006279 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006280 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006281 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006282 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006283 nfs4_stateid_copy(&data->arg.open_stateid,
6284 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006285 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006286 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006287 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006288 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006289 nfs4_stateid_copy(&data->arg.lock_stateid,
6290 &data->lsp->ls_stateid);
6291 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006292 if (!nfs4_valid_open_stateid(state)) {
6293 data->rpc_status = -EBADF;
6294 task->tk_action = NULL;
6295 goto out_release_open_seqid;
6296 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006297 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006298 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006299 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006300 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006301 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006302 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006303out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006304 nfs_release_seqid(data->arg.open_seqid);
6305out_release_lock_seqid:
6306 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006307out_wait:
6308 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006309 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006310}
6311
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006312static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6313{
6314 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006315 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006316
Harvey Harrison3110ff82008-05-02 13:42:44 -07006317 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006318
Trond Myklebust14516c32010-07-31 14:29:06 -04006319 if (!nfs4_sequence_done(task, &data->res.seq_res))
6320 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006321
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006322 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006323 switch (task->tk_status) {
6324 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006325 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006326 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006327 if (data->arg.new_lock) {
6328 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006329 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006330 rpc_restart_call_prepare(task);
6331 break;
6332 }
6333 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006334 if (data->arg.new_lock_owner != 0) {
6335 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6336 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6337 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6338 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6339 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006340 break;
6341 case -NFS4ERR_BAD_STATEID:
6342 case -NFS4ERR_OLD_STATEID:
6343 case -NFS4ERR_STALE_STATEID:
6344 case -NFS4ERR_EXPIRED:
6345 if (data->arg.new_lock_owner != 0) {
6346 if (!nfs4_stateid_match(&data->arg.open_stateid,
6347 &lsp->ls_state->open_stateid))
6348 rpc_restart_call_prepare(task);
6349 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6350 &lsp->ls_stateid))
6351 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006352 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006353 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006354}
6355
6356static void nfs4_lock_release(void *calldata)
6357{
6358 struct nfs4_lockdata *data = calldata;
6359
Harvey Harrison3110ff82008-05-02 13:42:44 -07006360 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006361 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006362 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006363 struct rpc_task *task;
6364 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6365 data->arg.lock_seqid);
6366 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006367 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006368 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006369 } else
6370 nfs_free_seqid(data->arg.lock_seqid);
6371 nfs4_put_lock_state(data->lsp);
6372 put_nfs_open_context(data->ctx);
6373 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006374 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006375}
6376
6377static const struct rpc_call_ops nfs4_lock_ops = {
6378 .rpc_call_prepare = nfs4_lock_prepare,
6379 .rpc_call_done = nfs4_lock_done,
6380 .rpc_release = nfs4_lock_release,
6381};
6382
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006383static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6384{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006385 switch (error) {
6386 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006387 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006388 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006389 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006390 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006391 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006392 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006393 break;
6394 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006395 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006396 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006397 };
6398}
6399
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006400static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006401{
6402 struct nfs4_lockdata *data;
6403 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006404 struct rpc_message msg = {
6405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6406 .rpc_cred = state->owner->so_cred,
6407 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006408 struct rpc_task_setup task_setup_data = {
6409 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006410 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006411 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006412 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006413 .flags = RPC_TASK_ASYNC,
6414 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006415 int ret;
6416
Harvey Harrison3110ff82008-05-02 13:42:44 -07006417 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006418 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006419 fl->fl_u.nfs4_fl.owner,
6420 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006421 if (data == NULL)
6422 return -ENOMEM;
6423 if (IS_SETLKW(cmd))
6424 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006425 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006426 msg.rpc_argp = &data->arg;
6427 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006428 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006429 if (recovery_type > NFS_LOCK_NEW) {
6430 if (recovery_type == NFS_LOCK_RECLAIM)
6431 data->arg.reclaim = NFS_LOCK_RECLAIM;
6432 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006433 } else
6434 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006435 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006436 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006437 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006438 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006439 if (ret == 0) {
6440 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006441 if (ret)
6442 nfs4_handle_setlk_error(data->server, data->lsp,
6443 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006444 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006445 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006446 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006447 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006448 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006449 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450}
6451
6452static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6453{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006454 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006455 struct nfs4_exception exception = {
6456 .inode = state->inode,
6457 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006458 int err;
6459
6460 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006461 /* Cache the lock if possible... */
6462 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6463 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006464 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006465 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006466 break;
6467 nfs4_handle_exception(server, err, &exception);
6468 } while (exception.retry);
6469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470}
6471
6472static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6473{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006474 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006475 struct nfs4_exception exception = {
6476 .inode = state->inode,
6477 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006478 int err;
6479
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006480 err = nfs4_set_lock_state(state, request);
6481 if (err != 0)
6482 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006483 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006484 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6485 return 0;
6486 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006487 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6489 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006490 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006491 switch (err) {
6492 default:
6493 goto out;
6494 case -NFS4ERR_GRACE:
6495 case -NFS4ERR_DELAY:
6496 nfs4_handle_exception(server, err, &exception);
6497 err = 0;
6498 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006499 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006500out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502}
6503
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006504#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006505static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6506{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006507 struct nfs4_lock_state *lsp;
6508 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006509
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006510 status = nfs4_set_lock_state(state, request);
6511 if (status != 0)
6512 return status;
6513 lsp = request->fl_u.nfs4_fl.owner;
6514 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6515 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6516 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006517 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006518}
6519#endif
6520
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6522{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006523 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006524 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006525 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006526 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
Trond Myklebust01c3b862006-06-29 16:38:39 -04006528 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006529 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006530 if (status < 0)
6531 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006532 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006533 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006534 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006535 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006536 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006537 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006538 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006539 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006540 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006541 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006542 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006543 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006544 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006545 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006546out:
6547 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 return status;
6549}
6550
6551static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6552{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006553 struct nfs4_exception exception = {
6554 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006555 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006556 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 int err;
6558
6559 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006560 err = _nfs4_proc_setlk(state, cmd, request);
6561 if (err == -NFS4ERR_DENIED)
6562 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006564 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 } while (exception.retry);
6566 return err;
6567}
6568
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006569#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6570#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6571
6572static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006573nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6574 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006575{
6576 int status = -ERESTARTSYS;
6577 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6578
6579 while(!signalled()) {
6580 status = nfs4_proc_setlk(state, cmd, request);
6581 if ((status != -EAGAIN) || IS_SETLK(cmd))
6582 break;
6583 freezable_schedule_timeout_interruptible(timeout);
6584 timeout *= 2;
6585 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6586 status = -ERESTARTSYS;
6587 }
6588 return status;
6589}
6590
Jeff Laytona1d617d82016-09-17 18:17:39 -04006591#ifdef CONFIG_NFS_V4_1
6592struct nfs4_lock_waiter {
6593 struct task_struct *task;
6594 struct inode *inode;
6595 struct nfs_lowner *owner;
6596 bool notified;
6597};
6598
6599static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006600nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006601{
6602 int ret;
6603 struct cb_notify_lock_args *cbnl = key;
6604 struct nfs4_lock_waiter *waiter = wait->private;
6605 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6606 *wowner = waiter->owner;
6607
6608 /* Only wake if the callback was for the same owner */
6609 if (lowner->clientid != wowner->clientid ||
6610 lowner->id != wowner->id ||
6611 lowner->s_dev != wowner->s_dev)
6612 return 0;
6613
6614 /* Make sure it's for the right inode */
6615 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6616 return 0;
6617
6618 waiter->notified = true;
6619
6620 /* override "private" so we can use default_wake_function */
6621 wait->private = waiter->task;
6622 ret = autoremove_wake_function(wait, mode, flags, key);
6623 wait->private = waiter;
6624 return ret;
6625}
6626
6627static int
6628nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6629{
6630 int status = -ERESTARTSYS;
6631 unsigned long flags;
6632 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6633 struct nfs_server *server = NFS_SERVER(state->inode);
6634 struct nfs_client *clp = server->nfs_client;
6635 wait_queue_head_t *q = &clp->cl_lock_waitq;
6636 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6637 .id = lsp->ls_seqid.owner_id,
6638 .s_dev = server->s_dev };
6639 struct nfs4_lock_waiter waiter = { .task = current,
6640 .inode = state->inode,
6641 .owner = &owner,
6642 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006643 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006644
6645 /* Don't bother with waitqueue if we don't expect a callback */
6646 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6647 return nfs4_retry_setlk_simple(state, cmd, request);
6648
6649 init_wait(&wait);
6650 wait.private = &waiter;
6651 wait.func = nfs4_wake_lock_waiter;
6652 add_wait_queue(q, &wait);
6653
6654 while(!signalled()) {
6655 status = nfs4_proc_setlk(state, cmd, request);
6656 if ((status != -EAGAIN) || IS_SETLK(cmd))
6657 break;
6658
6659 status = -ERESTARTSYS;
6660 spin_lock_irqsave(&q->lock, flags);
6661 if (waiter.notified) {
6662 spin_unlock_irqrestore(&q->lock, flags);
6663 continue;
6664 }
6665 set_current_state(TASK_INTERRUPTIBLE);
6666 spin_unlock_irqrestore(&q->lock, flags);
6667
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006668 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006669 }
6670
6671 finish_wait(q, &wait);
6672 return status;
6673}
6674#else /* !CONFIG_NFS_V4_1 */
6675static inline int
6676nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6677{
6678 return nfs4_retry_setlk_simple(state, cmd, request);
6679}
6680#endif
6681
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682static int
6683nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6684{
6685 struct nfs_open_context *ctx;
6686 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 int status;
6688
6689 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006690 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 state = ctx->state;
6692
Trond Myklebustd9531262009-07-21 19:22:38 -04006693 if (IS_GETLK(cmd)) {
6694 if (state != NULL)
6695 return nfs4_proc_getlk(state, F_GETLK, request);
6696 return 0;
6697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698
6699 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6700 return -EINVAL;
6701
Trond Myklebustd9531262009-07-21 19:22:38 -04006702 if (request->fl_type == F_UNLCK) {
6703 if (state != NULL)
6704 return nfs4_proc_unlck(state, cmd, request);
6705 return 0;
6706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
Trond Myklebustd9531262009-07-21 19:22:38 -04006708 if (state == NULL)
6709 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006710
6711 if ((request->fl_flags & FL_POSIX) &&
6712 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6713 return -ENOLCK;
6714
Jeff Layton1ea67db2016-09-17 18:17:37 -04006715 status = nfs4_set_lock_state(state, request);
6716 if (status != 0)
6717 return status;
6718
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006719 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720}
6721
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006722int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006723{
6724 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006725 int err;
6726
6727 err = nfs4_set_lock_state(state, fl);
6728 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006729 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006730 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006731 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006732}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006733
Trond Myklebustcf470c32012-03-07 13:49:12 -05006734struct nfs_release_lockowner_data {
6735 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006736 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006737 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006738 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006739 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006740};
6741
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006742static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6743{
6744 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006745 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006746 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6747 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006748 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006749 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006750}
6751
6752static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6753{
6754 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006755 struct nfs_server *server = data->server;
6756
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006757 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006758
6759 switch (task->tk_status) {
6760 case 0:
6761 renew_lease(server, data->timestamp);
6762 break;
6763 case -NFS4ERR_STALE_CLIENTID:
6764 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006765 nfs4_schedule_lease_recovery(server->nfs_client);
6766 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006767 case -NFS4ERR_LEASE_MOVED:
6768 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006769 if (nfs4_async_handle_error(task, server,
6770 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006771 rpc_restart_call_prepare(task);
6772 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006773}
6774
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006775static void nfs4_release_lockowner_release(void *calldata)
6776{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006777 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006778 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006779 kfree(calldata);
6780}
6781
Trond Myklebust17280172012-03-11 13:11:00 -04006782static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006783 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6784 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006785 .rpc_release = nfs4_release_lockowner_release,
6786};
6787
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006788static void
6789nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006790{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006791 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006792 struct rpc_message msg = {
6793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6794 };
6795
6796 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006797 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006798
Trond Myklebustcf470c32012-03-07 13:49:12 -05006799 data = kmalloc(sizeof(*data), GFP_NOFS);
6800 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006801 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006802 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006803 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006804 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6805 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6806 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006807
Trond Myklebustcf470c32012-03-07 13:49:12 -05006808 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006809 msg.rpc_resp = &data->res;
6810 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006811 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006812}
6813
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006814#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6815
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006816static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006817 struct dentry *unused, struct inode *inode,
6818 const char *key, const void *buf,
6819 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006820{
Al Viro59301222016-05-27 10:19:30 -04006821 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006822}
6823
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006824static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006825 struct dentry *unused, struct inode *inode,
6826 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006827{
Al Virob2968212016-04-10 20:48:24 -04006828 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006829}
6830
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006831static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006832{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006833 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006834}
6835
David Quigleyc9bccef2013-05-22 12:50:45 -04006836#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006837
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006838static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006839 struct dentry *unused, struct inode *inode,
6840 const char *key, const void *buf,
6841 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006842{
6843 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006844 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006845
6846 return -EOPNOTSUPP;
6847}
6848
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006849static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006850 struct dentry *unused, struct inode *inode,
6851 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006852{
6853 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006854 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006855 return -EOPNOTSUPP;
6856}
6857
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006858static ssize_t
6859nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006860{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006861 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006862
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006863 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6864 len = security_inode_listsecurity(inode, list, list_len);
6865 if (list_len && len > list_len)
6866 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006867 }
6868 return len;
6869}
6870
6871static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6872 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006873 .get = nfs4_xattr_get_nfs4_label,
6874 .set = nfs4_xattr_set_nfs4_label,
6875};
David Quigleyc9bccef2013-05-22 12:50:45 -04006876
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006877#else
6878
6879static ssize_t
6880nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6881{
6882 return 0;
6883}
6884
6885#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006886
Andy Adamson533eb462011-06-13 18:25:56 -04006887/*
6888 * nfs_fhget will use either the mounted_on_fileid or the fileid
6889 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006890static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6891{
Andy Adamson533eb462011-06-13 18:25:56 -04006892 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6893 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6894 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006895 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006896 return;
6897
6898 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006899 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006900 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6901 fattr->nlink = 2;
6902}
6903
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006904static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6905 const struct qstr *name,
6906 struct nfs4_fs_locations *fs_locations,
6907 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006908{
6909 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006910 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006911 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006912 };
6913 struct nfs4_fs_locations_arg args = {
6914 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006915 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006916 .page = page,
6917 .bitmask = bitmask,
6918 };
Benny Halevy22958462009-04-01 09:22:02 -04006919 struct nfs4_fs_locations_res res = {
6920 .fs_locations = fs_locations,
6921 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006922 struct rpc_message msg = {
6923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6924 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006925 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006926 };
6927 int status;
6928
Harvey Harrison3110ff82008-05-02 13:42:44 -07006929 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006930
6931 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6932 * is not supported */
6933 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6934 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6935 else
6936 bitmask[0] |= FATTR4_WORD0_FILEID;
6937
Trond Myklebustc228fd32007-01-13 02:28:11 -05006938 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006939 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006940 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006941 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006942 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006943 return status;
6944}
6945
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006946int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6947 const struct qstr *name,
6948 struct nfs4_fs_locations *fs_locations,
6949 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006950{
6951 struct nfs4_exception exception = { };
6952 int err;
6953 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006954 err = _nfs4_proc_fs_locations(client, dir, name,
6955 fs_locations, page);
6956 trace_nfs4_get_fs_locations(dir, name, err);
6957 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006958 &exception);
6959 } while (exception.retry);
6960 return err;
6961}
6962
Chuck Leverb03d7352013-10-17 14:12:50 -04006963/*
6964 * This operation also signals the server that this client is
6965 * performing migration recovery. The server can stop returning
6966 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6967 * appended to this compound to identify the client ID which is
6968 * performing recovery.
6969 */
6970static int _nfs40_proc_get_locations(struct inode *inode,
6971 struct nfs4_fs_locations *locations,
6972 struct page *page, struct rpc_cred *cred)
6973{
6974 struct nfs_server *server = NFS_SERVER(inode);
6975 struct rpc_clnt *clnt = server->client;
6976 u32 bitmask[2] = {
6977 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6978 };
6979 struct nfs4_fs_locations_arg args = {
6980 .clientid = server->nfs_client->cl_clientid,
6981 .fh = NFS_FH(inode),
6982 .page = page,
6983 .bitmask = bitmask,
6984 .migration = 1, /* skip LOOKUP */
6985 .renew = 1, /* append RENEW */
6986 };
6987 struct nfs4_fs_locations_res res = {
6988 .fs_locations = locations,
6989 .migration = 1,
6990 .renew = 1,
6991 };
6992 struct rpc_message msg = {
6993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6994 .rpc_argp = &args,
6995 .rpc_resp = &res,
6996 .rpc_cred = cred,
6997 };
6998 unsigned long now = jiffies;
6999 int status;
7000
7001 nfs_fattr_init(&locations->fattr);
7002 locations->server = server;
7003 locations->nlocations = 0;
7004
7005 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7006 nfs4_set_sequence_privileged(&args.seq_args);
7007 status = nfs4_call_sync_sequence(clnt, server, &msg,
7008 &args.seq_args, &res.seq_res);
7009 if (status)
7010 return status;
7011
7012 renew_lease(server, now);
7013 return 0;
7014}
7015
7016#ifdef CONFIG_NFS_V4_1
7017
7018/*
7019 * This operation also signals the server that this client is
7020 * performing migration recovery. The server can stop asserting
7021 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7022 * performing this operation is identified in the SEQUENCE
7023 * operation in this compound.
7024 *
7025 * When the client supports GETATTR(fs_locations_info), it can
7026 * be plumbed in here.
7027 */
7028static int _nfs41_proc_get_locations(struct inode *inode,
7029 struct nfs4_fs_locations *locations,
7030 struct page *page, struct rpc_cred *cred)
7031{
7032 struct nfs_server *server = NFS_SERVER(inode);
7033 struct rpc_clnt *clnt = server->client;
7034 u32 bitmask[2] = {
7035 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7036 };
7037 struct nfs4_fs_locations_arg args = {
7038 .fh = NFS_FH(inode),
7039 .page = page,
7040 .bitmask = bitmask,
7041 .migration = 1, /* skip LOOKUP */
7042 };
7043 struct nfs4_fs_locations_res res = {
7044 .fs_locations = locations,
7045 .migration = 1,
7046 };
7047 struct rpc_message msg = {
7048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7049 .rpc_argp = &args,
7050 .rpc_resp = &res,
7051 .rpc_cred = cred,
7052 };
7053 int status;
7054
7055 nfs_fattr_init(&locations->fattr);
7056 locations->server = server;
7057 locations->nlocations = 0;
7058
7059 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7060 nfs4_set_sequence_privileged(&args.seq_args);
7061 status = nfs4_call_sync_sequence(clnt, server, &msg,
7062 &args.seq_args, &res.seq_res);
7063 if (status == NFS4_OK &&
7064 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7065 status = -NFS4ERR_LEASE_MOVED;
7066 return status;
7067}
7068
7069#endif /* CONFIG_NFS_V4_1 */
7070
7071/**
7072 * nfs4_proc_get_locations - discover locations for a migrated FSID
7073 * @inode: inode on FSID that is migrating
7074 * @locations: result of query
7075 * @page: buffer
7076 * @cred: credential to use for this operation
7077 *
7078 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7079 * operation failed, or a negative errno if a local error occurred.
7080 *
7081 * On success, "locations" is filled in, but if the server has
7082 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7083 * asserted.
7084 *
7085 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7086 * from this client that require migration recovery.
7087 */
7088int nfs4_proc_get_locations(struct inode *inode,
7089 struct nfs4_fs_locations *locations,
7090 struct page *page, struct rpc_cred *cred)
7091{
7092 struct nfs_server *server = NFS_SERVER(inode);
7093 struct nfs_client *clp = server->nfs_client;
7094 const struct nfs4_mig_recovery_ops *ops =
7095 clp->cl_mvops->mig_recovery_ops;
7096 struct nfs4_exception exception = { };
7097 int status;
7098
7099 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7100 (unsigned long long)server->fsid.major,
7101 (unsigned long long)server->fsid.minor,
7102 clp->cl_hostname);
7103 nfs_display_fhandle(NFS_FH(inode), __func__);
7104
7105 do {
7106 status = ops->get_locations(inode, locations, page, cred);
7107 if (status != -NFS4ERR_DELAY)
7108 break;
7109 nfs4_handle_exception(server, status, &exception);
7110 } while (exception.retry);
7111 return status;
7112}
7113
Chuck Lever44c99932013-10-17 14:13:30 -04007114/*
7115 * This operation also signals the server that this client is
7116 * performing "lease moved" recovery. The server can stop
7117 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7118 * is appended to this compound to identify the client ID which is
7119 * performing recovery.
7120 */
7121static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7122{
7123 struct nfs_server *server = NFS_SERVER(inode);
7124 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7125 struct rpc_clnt *clnt = server->client;
7126 struct nfs4_fsid_present_arg args = {
7127 .fh = NFS_FH(inode),
7128 .clientid = clp->cl_clientid,
7129 .renew = 1, /* append RENEW */
7130 };
7131 struct nfs4_fsid_present_res res = {
7132 .renew = 1,
7133 };
7134 struct rpc_message msg = {
7135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7136 .rpc_argp = &args,
7137 .rpc_resp = &res,
7138 .rpc_cred = cred,
7139 };
7140 unsigned long now = jiffies;
7141 int status;
7142
7143 res.fh = nfs_alloc_fhandle();
7144 if (res.fh == NULL)
7145 return -ENOMEM;
7146
7147 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7148 nfs4_set_sequence_privileged(&args.seq_args);
7149 status = nfs4_call_sync_sequence(clnt, server, &msg,
7150 &args.seq_args, &res.seq_res);
7151 nfs_free_fhandle(res.fh);
7152 if (status)
7153 return status;
7154
7155 do_renew_lease(clp, now);
7156 return 0;
7157}
7158
7159#ifdef CONFIG_NFS_V4_1
7160
7161/*
7162 * This operation also signals the server that this client is
7163 * performing "lease moved" recovery. The server can stop asserting
7164 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7165 * this operation is identified in the SEQUENCE operation in this
7166 * compound.
7167 */
7168static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7169{
7170 struct nfs_server *server = NFS_SERVER(inode);
7171 struct rpc_clnt *clnt = server->client;
7172 struct nfs4_fsid_present_arg args = {
7173 .fh = NFS_FH(inode),
7174 };
7175 struct nfs4_fsid_present_res res = {
7176 };
7177 struct rpc_message msg = {
7178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7179 .rpc_argp = &args,
7180 .rpc_resp = &res,
7181 .rpc_cred = cred,
7182 };
7183 int status;
7184
7185 res.fh = nfs_alloc_fhandle();
7186 if (res.fh == NULL)
7187 return -ENOMEM;
7188
7189 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7190 nfs4_set_sequence_privileged(&args.seq_args);
7191 status = nfs4_call_sync_sequence(clnt, server, &msg,
7192 &args.seq_args, &res.seq_res);
7193 nfs_free_fhandle(res.fh);
7194 if (status == NFS4_OK &&
7195 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7196 status = -NFS4ERR_LEASE_MOVED;
7197 return status;
7198}
7199
7200#endif /* CONFIG_NFS_V4_1 */
7201
7202/**
7203 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7204 * @inode: inode on FSID to check
7205 * @cred: credential to use for this operation
7206 *
7207 * Server indicates whether the FSID is present, moved, or not
7208 * recognized. This operation is necessary to clear a LEASE_MOVED
7209 * condition for this client ID.
7210 *
7211 * Returns NFS4_OK if the FSID is present on this server,
7212 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7213 * NFS4ERR code if some error occurred on the server, or a
7214 * negative errno if a local failure occurred.
7215 */
7216int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7217{
7218 struct nfs_server *server = NFS_SERVER(inode);
7219 struct nfs_client *clp = server->nfs_client;
7220 const struct nfs4_mig_recovery_ops *ops =
7221 clp->cl_mvops->mig_recovery_ops;
7222 struct nfs4_exception exception = { };
7223 int status;
7224
7225 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7226 (unsigned long long)server->fsid.major,
7227 (unsigned long long)server->fsid.minor,
7228 clp->cl_hostname);
7229 nfs_display_fhandle(NFS_FH(inode), __func__);
7230
7231 do {
7232 status = ops->fsid_present(inode, cred);
7233 if (status != -NFS4ERR_DELAY)
7234 break;
7235 nfs4_handle_exception(server, status, &exception);
7236 } while (exception.retry);
7237 return status;
7238}
7239
Andy Adamson5ec16a82013-08-08 10:57:55 -04007240/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007241 * If 'use_integrity' is true and the state managment nfs_client
7242 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7243 * and the machine credential as per RFC3530bis and RFC5661 Security
7244 * Considerations sections. Otherwise, just use the user cred with the
7245 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007246 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007247static 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 +00007248{
7249 int status;
7250 struct nfs4_secinfo_arg args = {
7251 .dir_fh = NFS_FH(dir),
7252 .name = name,
7253 };
7254 struct nfs4_secinfo_res res = {
7255 .flavors = flavors,
7256 };
7257 struct rpc_message msg = {
7258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7259 .rpc_argp = &args,
7260 .rpc_resp = &res,
7261 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007262 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007263 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007264
7265 if (use_integrity) {
7266 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007267 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7268 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007269 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007270
7271 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007272
7273 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7274 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7275
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007276 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7277 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007278 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007279
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007280 if (cred)
7281 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007282
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007283 return status;
7284}
7285
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007286int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7287 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007288{
7289 struct nfs4_exception exception = { };
7290 int err;
7291 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007292 err = -NFS4ERR_WRONGSEC;
7293
7294 /* try to use integrity protection with machine cred */
7295 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7296 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7297
7298 /*
7299 * if unable to use integrity protection, or SECINFO with
7300 * integrity protection returns NFS4ERR_WRONGSEC (which is
7301 * disallowed by spec, but exists in deployed servers) use
7302 * the current filesystem's rpc_client and the user cred.
7303 */
7304 if (err == -NFS4ERR_WRONGSEC)
7305 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7306
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007307 trace_nfs4_secinfo(dir, name, err);
7308 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007309 &exception);
7310 } while (exception.retry);
7311 return err;
7312}
7313
Andy Adamson557134a2009-04-01 09:21:53 -04007314#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007315/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007316 * Check the exchange flags returned by the server for invalid flags, having
7317 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7318 * DS flags set.
7319 */
7320static int nfs4_check_cl_exchange_flags(u32 flags)
7321{
7322 if (flags & ~EXCHGID4_FLAG_MASK_R)
7323 goto out_inval;
7324 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7325 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7326 goto out_inval;
7327 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7328 goto out_inval;
7329 return NFS_OK;
7330out_inval:
7331 return -NFS4ERR_INVAL;
7332}
7333
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007334static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007335nfs41_same_server_scope(struct nfs41_server_scope *a,
7336 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007337{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007338 if (a->server_scope_sz != b->server_scope_sz)
7339 return false;
7340 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007341}
7342
Andy Adamson02a95de2016-02-05 16:08:37 -05007343static void
7344nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7345{
7346}
7347
7348static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7349 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7350};
7351
Andy Adamson357f54d2010-12-14 10:11:57 -05007352/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007353 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007354 *
7355 * The 4.1 client currently uses the same TCP connection for the
7356 * fore and backchannel.
7357 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007358static
7359int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7360 struct rpc_xprt *xprt,
7361 struct nfs_client *clp,
7362 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007363{
7364 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007365 struct nfs41_bind_conn_to_session_args args = {
7366 .client = clp,
7367 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7368 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007369 struct nfs41_bind_conn_to_session_res res;
7370 struct rpc_message msg = {
7371 .rpc_proc =
7372 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007373 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007374 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007375 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007376 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007377 struct rpc_task_setup task_setup_data = {
7378 .rpc_client = clnt,
7379 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007380 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007381 .rpc_message = &msg,
7382 .flags = RPC_TASK_TIMEOUT,
7383 };
7384 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007385
Trond Myklebust71a097c2015-02-18 09:27:18 -08007386 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7387 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7388 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007389
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007390 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7391 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7392 args.dir = NFS4_CDFC4_FORE;
7393
7394 task = rpc_run_task(&task_setup_data);
7395 if (!IS_ERR(task)) {
7396 status = task->tk_status;
7397 rpc_put_task(task);
7398 } else
7399 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007400 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007401 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007402 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007403 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7404 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007405 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007406 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007407 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007408 dprintk("NFS: %s: Unexpected direction from server\n",
7409 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007410 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007411 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007412 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007413 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7414 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007415 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007416 }
7417 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007418
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007419 return status;
7420}
7421
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007422struct rpc_bind_conn_calldata {
7423 struct nfs_client *clp;
7424 struct rpc_cred *cred;
7425};
7426
7427static int
7428nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7429 struct rpc_xprt *xprt,
7430 void *calldata)
7431{
7432 struct rpc_bind_conn_calldata *p = calldata;
7433
7434 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7435}
7436
7437int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7438{
7439 struct rpc_bind_conn_calldata data = {
7440 .clp = clp,
7441 .cred = cred,
7442 };
7443 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7444 nfs4_proc_bind_conn_to_session_callback, &data);
7445}
7446
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007447/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007448 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7449 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007450 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007451static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7452 .how = SP4_MACH_CRED,
7453 .enforce.u.words = {
7454 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7455 1 << (OP_EXCHANGE_ID - 32) |
7456 1 << (OP_CREATE_SESSION - 32) |
7457 1 << (OP_DESTROY_SESSION - 32) |
7458 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007459 },
7460 .allow.u.words = {
7461 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007462 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007463 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007464 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007465 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007466 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007467 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007468 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007469 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007470 1 << (OP_FREE_STATEID - 32) |
7471 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007472 }
7473};
7474
7475/*
7476 * Select the state protection mode for client `clp' given the server results
7477 * from exchange_id in `sp'.
7478 *
7479 * Returns 0 on success, negative errno otherwise.
7480 */
7481static int nfs4_sp4_select_mode(struct nfs_client *clp,
7482 struct nfs41_state_protection *sp)
7483{
7484 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7485 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7486 1 << (OP_EXCHANGE_ID - 32) |
7487 1 << (OP_CREATE_SESSION - 32) |
7488 1 << (OP_DESTROY_SESSION - 32) |
7489 1 << (OP_DESTROY_CLIENTID - 32)
7490 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007491 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007492 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007493 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007494
7495 if (sp->how == SP4_MACH_CRED) {
7496 /* Print state protect result */
7497 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7498 for (i = 0; i <= LAST_NFS4_OP; i++) {
7499 if (test_bit(i, sp->enforce.u.longs))
7500 dfprintk(MOUNT, " enforce op %d\n", i);
7501 if (test_bit(i, sp->allow.u.longs))
7502 dfprintk(MOUNT, " allow op %d\n", i);
7503 }
7504
7505 /* make sure nothing is on enforce list that isn't supported */
7506 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7507 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7508 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007509 ret = -EINVAL;
7510 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007511 }
7512 }
7513
7514 /*
7515 * Minimal mode - state operations are allowed to use machine
7516 * credential. Note this already happens by default, so the
7517 * client doesn't have to do anything more than the negotiation.
7518 *
7519 * NOTE: we don't care if EXCHANGE_ID is in the list -
7520 * we're already using the machine cred for exchange_id
7521 * and will never use a different cred.
7522 */
7523 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7524 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7525 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7526 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7527 dfprintk(MOUNT, "sp4_mach_cred:\n");
7528 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007529 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007530 } else {
7531 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007532 ret = -EINVAL;
7533 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007534 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007535
7536 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007537 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7538 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007539 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7540 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007541 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007542 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007543
Andrew Elble99ade3c2015-12-02 09:39:51 -05007544 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7545 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007546 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007547 }
7548
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007549 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7550 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7551 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007552 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007553 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007554
7555 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7556 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7557 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007558 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007559 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007560
7561 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7562 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007563 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007564 }
7565
7566 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7567 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007568 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007569 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007570 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007571out:
7572 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007573 return 0;
7574}
7575
Andy Adamson8d89bd72016-09-09 09:22:18 -04007576struct nfs41_exchange_id_data {
7577 struct nfs41_exchange_id_res res;
7578 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007579};
7580
Andy Adamson8d89bd72016-09-09 09:22:18 -04007581static void nfs4_exchange_id_release(void *data)
7582{
7583 struct nfs41_exchange_id_data *cdata =
7584 (struct nfs41_exchange_id_data *)data;
7585
Olga Kornievskaia63513232017-03-13 10:36:19 -04007586 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007587 kfree(cdata->res.impl_id);
7588 kfree(cdata->res.server_scope);
7589 kfree(cdata->res.server_owner);
7590 kfree(cdata);
7591}
7592
7593static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007594 .rpc_release = nfs4_exchange_id_release,
7595};
7596
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007597/*
7598 * _nfs4_proc_exchange_id()
7599 *
7600 * Wrapper for EXCHANGE_ID operation.
7601 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007602static struct rpc_task *
7603nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007604 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007605{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007606 struct rpc_message msg = {
7607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007608 .rpc_cred = cred,
7609 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007610 struct rpc_task_setup task_setup_data = {
7611 .rpc_client = clp->cl_rpcclient,
7612 .callback_ops = &nfs4_exchange_id_call_ops,
7613 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007614 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007615 };
7616 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007617 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007618
Elena Reshetova212bf412017-10-20 12:53:38 +03007619 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007620 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007621
Trond Myklebust9c760d12017-07-31 18:38:50 -04007622 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007623 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007624 if (!calldata)
7625 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007626
Trond Myklebustfd405592017-08-01 16:02:47 -04007627 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007628
7629 status = nfs4_init_uniform_client_string(clp);
7630 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007631 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007632
Andy Adamson8d89bd72016-09-09 09:22:18 -04007633 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7634 GFP_NOFS);
7635 status = -ENOMEM;
7636 if (unlikely(calldata->res.server_owner == NULL))
7637 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007638
Andy Adamson8d89bd72016-09-09 09:22:18 -04007639 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007640 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007641 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007642 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007643
Andy Adamson8d89bd72016-09-09 09:22:18 -04007644 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7645 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007646 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007647
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007648 switch (sp4_how) {
7649 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007650 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007651 break;
7652
7653 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007654 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007655 break;
7656
7657 default:
7658 /* unsupported! */
7659 WARN_ON_ONCE(1);
7660 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007661 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007662 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007663 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007664 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007665 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007666 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7667 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007668 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007669 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007670 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7671 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007672#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007673 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007674#endif
7675 msg.rpc_argp = &calldata->args;
7676 msg.rpc_resp = &calldata->res;
7677 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007678
Trond Myklebust9c760d12017-07-31 18:38:50 -04007679 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007680
7681out_impl_id:
7682 kfree(calldata->res.impl_id);
7683out_server_scope:
7684 kfree(calldata->res.server_scope);
7685out_server_owner:
7686 kfree(calldata->res.server_owner);
7687out_calldata:
7688 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007689out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007690 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007691 return ERR_PTR(status);
7692}
7693
7694/*
7695 * _nfs4_proc_exchange_id()
7696 *
7697 * Wrapper for EXCHANGE_ID operation.
7698 */
7699static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7700 u32 sp4_how)
7701{
7702 struct rpc_task *task;
7703 struct nfs41_exchange_id_args *argp;
7704 struct nfs41_exchange_id_res *resp;
7705 int status;
7706
7707 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7708 if (IS_ERR(task))
7709 return PTR_ERR(task);
7710
7711 argp = task->tk_msg.rpc_argp;
7712 resp = task->tk_msg.rpc_resp;
7713 status = task->tk_status;
7714 if (status != 0)
7715 goto out;
7716
7717 status = nfs4_check_cl_exchange_flags(resp->flags);
7718 if (status != 0)
7719 goto out;
7720
7721 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7722 if (status != 0)
7723 goto out;
7724
7725 clp->cl_clientid = resp->clientid;
7726 clp->cl_exchange_flags = resp->flags;
7727 clp->cl_seqid = resp->seqid;
7728 /* Client ID is not confirmed */
7729 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7730 clear_bit(NFS4_SESSION_ESTABLISHED,
7731 &clp->cl_session->session_state);
7732
7733 if (clp->cl_serverscope != NULL &&
7734 !nfs41_same_server_scope(clp->cl_serverscope,
7735 resp->server_scope)) {
7736 dprintk("%s: server_scope mismatch detected\n",
7737 __func__);
7738 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7739 }
7740
7741 swap(clp->cl_serverowner, resp->server_owner);
7742 swap(clp->cl_serverscope, resp->server_scope);
7743 swap(clp->cl_implid, resp->impl_id);
7744
7745 /* Save the EXCHANGE_ID verifier session trunk tests */
7746 memcpy(clp->cl_confirm.data, argp->verifier.data,
7747 sizeof(clp->cl_confirm.data));
7748out:
7749 trace_nfs4_exchange_id(clp, status);
7750 rpc_put_task(task);
7751 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007752}
7753
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007754/*
7755 * nfs4_proc_exchange_id()
7756 *
7757 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7758 *
7759 * Since the clientid has expired, all compounds using sessions
7760 * associated with the stale clientid will be returning
7761 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7762 * be in some phase of session reset.
7763 *
7764 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7765 */
7766int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7767{
7768 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7769 int status;
7770
7771 /* try SP4_MACH_CRED if krb5i/p */
7772 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7773 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007774 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007775 if (!status)
7776 return 0;
7777 }
7778
7779 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007780 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007781}
7782
Andy Adamson04fa2c62016-09-09 09:22:29 -04007783/**
7784 * nfs4_test_session_trunk
7785 *
7786 * This is an add_xprt_test() test function called from
7787 * rpc_clnt_setup_test_and_add_xprt.
7788 *
7789 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7790 * and is dereferrenced in nfs4_exchange_id_release
7791 *
7792 * Upon success, add the new transport to the rpc_clnt
7793 *
7794 * @clnt: struct rpc_clnt to get new transport
7795 * @xprt: the rpc_xprt to test
7796 * @data: call data for _nfs4_proc_exchange_id.
7797 */
7798int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7799 void *data)
7800{
7801 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007802 struct rpc_task *task;
7803 int status;
7804
Andy Adamson04fa2c62016-09-09 09:22:29 -04007805 u32 sp4_how;
7806
7807 dprintk("--> %s try %s\n", __func__,
7808 xprt->address_strings[RPC_DISPLAY_ADDR]);
7809
7810 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7811
7812 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007813 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7814 if (IS_ERR(task))
7815 return PTR_ERR(task);
7816
7817 status = task->tk_status;
7818 if (status == 0)
7819 status = nfs4_detect_session_trunking(adata->clp,
7820 task->tk_msg.rpc_resp, xprt);
7821
7822 rpc_put_task(task);
7823 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007824}
7825EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7826
Trond Myklebust66245532012-05-25 17:18:09 -04007827static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7828 struct rpc_cred *cred)
7829{
7830 struct rpc_message msg = {
7831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7832 .rpc_argp = clp,
7833 .rpc_cred = cred,
7834 };
7835 int status;
7836
7837 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007838 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007839 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007840 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007841 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7842 return status;
7843}
7844
7845static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7846 struct rpc_cred *cred)
7847{
7848 unsigned int loop;
7849 int ret;
7850
7851 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7852 ret = _nfs4_proc_destroy_clientid(clp, cred);
7853 switch (ret) {
7854 case -NFS4ERR_DELAY:
7855 case -NFS4ERR_CLIENTID_BUSY:
7856 ssleep(1);
7857 break;
7858 default:
7859 return ret;
7860 }
7861 }
7862 return 0;
7863}
7864
7865int nfs4_destroy_clientid(struct nfs_client *clp)
7866{
7867 struct rpc_cred *cred;
7868 int ret = 0;
7869
7870 if (clp->cl_mvops->minor_version < 1)
7871 goto out;
7872 if (clp->cl_exchange_flags == 0)
7873 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007874 if (clp->cl_preserve_clid)
7875 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007876 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007877 ret = nfs4_proc_destroy_clientid(clp, cred);
7878 if (cred)
7879 put_rpccred(cred);
7880 switch (ret) {
7881 case 0:
7882 case -NFS4ERR_STALE_CLIENTID:
7883 clp->cl_exchange_flags = 0;
7884 }
7885out:
7886 return ret;
7887}
7888
Andy Adamson2050f0c2009-04-01 09:22:30 -04007889struct nfs4_get_lease_time_data {
7890 struct nfs4_get_lease_time_args *args;
7891 struct nfs4_get_lease_time_res *res;
7892 struct nfs_client *clp;
7893};
7894
7895static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7896 void *calldata)
7897{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007898 struct nfs4_get_lease_time_data *data =
7899 (struct nfs4_get_lease_time_data *)calldata;
7900
7901 dprintk("--> %s\n", __func__);
7902 /* just setup sequence, do not trigger session recovery
7903 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007904 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007905 &data->args->la_seq_args,
7906 &data->res->lr_seq_res,
7907 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007908 dprintk("<-- %s\n", __func__);
7909}
7910
7911/*
7912 * Called from nfs4_state_manager thread for session setup, so don't recover
7913 * from sequence operation or clientid errors.
7914 */
7915static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7916{
7917 struct nfs4_get_lease_time_data *data =
7918 (struct nfs4_get_lease_time_data *)calldata;
7919
7920 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007921 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7922 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007923 switch (task->tk_status) {
7924 case -NFS4ERR_DELAY:
7925 case -NFS4ERR_GRACE:
7926 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7927 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7928 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007929 /* fall through */
7930 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007931 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007932 return;
7933 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007934 dprintk("<-- %s\n", __func__);
7935}
7936
Trond Myklebust17280172012-03-11 13:11:00 -04007937static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007938 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7939 .rpc_call_done = nfs4_get_lease_time_done,
7940};
7941
7942int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7943{
7944 struct rpc_task *task;
7945 struct nfs4_get_lease_time_args args;
7946 struct nfs4_get_lease_time_res res = {
7947 .lr_fsinfo = fsinfo,
7948 };
7949 struct nfs4_get_lease_time_data data = {
7950 .args = &args,
7951 .res = &res,
7952 .clp = clp,
7953 };
7954 struct rpc_message msg = {
7955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7956 .rpc_argp = &args,
7957 .rpc_resp = &res,
7958 };
7959 struct rpc_task_setup task_setup = {
7960 .rpc_client = clp->cl_rpcclient,
7961 .rpc_message = &msg,
7962 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007963 .callback_data = &data,
7964 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007965 };
7966 int status;
7967
Chuck Levera9c92d62013-08-09 12:48:18 -04007968 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007969 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007970 task = rpc_run_task(&task_setup);
7971
7972 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007973 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007974
Anna Schumakerf6148712017-04-07 14:15:23 -04007975 status = task->tk_status;
7976 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007977 return status;
7978}
7979
Andy Adamsonfc931582009-04-01 09:22:31 -04007980/*
7981 * Initialize the values to be used by the client in CREATE_SESSION
7982 * If nfs4_init_session set the fore channel request and response sizes,
7983 * use them.
7984 *
7985 * Set the back channel max_resp_sz_cached to zero to force the client to
7986 * always set csa_cachethis to FALSE because the current implementation
7987 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7988 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007989static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7990 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007991{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007992 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007993 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007994
Andy Adamson18aad3d2013-06-26 12:21:49 -04007995 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7996 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7997
Andy Adamsonfc931582009-04-01 09:22:31 -04007998 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007999 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8000 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008001 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008002 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008003
8004 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008005 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008006 __func__,
8007 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008008 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008009
8010 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008011 args->bc_attrs.max_rqst_sz = max_bc_payload;
8012 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008013 args->bc_attrs.max_resp_sz_cached = 0;
8014 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008015 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008016
8017 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8018 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8019 __func__,
8020 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8021 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8022 args->bc_attrs.max_reqs);
8023}
8024
Trond Myklebust79969dd2015-02-18 11:30:18 -08008025static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8026 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008027{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008028 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008029 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008030
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008031 if (rcvd->max_resp_sz > sent->max_resp_sz)
8032 return -EINVAL;
8033 /*
8034 * Our requested max_ops is the minimum we need; we're not
8035 * prepared to break up compounds into smaller pieces than that.
8036 * So, no point even trying to continue if the server won't
8037 * cooperate:
8038 */
8039 if (rcvd->max_ops < sent->max_ops)
8040 return -EINVAL;
8041 if (rcvd->max_reqs == 0)
8042 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008043 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8044 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008045 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008046}
8047
Trond Myklebust79969dd2015-02-18 11:30:18 -08008048static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8049 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008050{
8051 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008052 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008053
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008054 if (!(res->flags & SESSION4_BACK_CHAN))
8055 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008056 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8057 return -EINVAL;
8058 if (rcvd->max_resp_sz < sent->max_resp_sz)
8059 return -EINVAL;
8060 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8061 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008062 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008063 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008064 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008065 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008066out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008067 return 0;
8068}
Andy Adamson8d353012009-04-01 09:22:32 -04008069
Andy Adamson8d353012009-04-01 09:22:32 -04008070static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008071 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008072{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008073 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008074
Trond Myklebust79969dd2015-02-18 11:30:18 -08008075 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008076 if (ret)
8077 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008078 return nfs4_verify_back_channel_attrs(args, res);
8079}
8080
8081static void nfs4_update_session(struct nfs4_session *session,
8082 struct nfs41_create_session_res *res)
8083{
8084 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008085 /* Mark client id and session as being confirmed */
8086 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8087 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008088 session->flags = res->flags;
8089 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008090 if (res->flags & SESSION4_BACK_CHAN)
8091 memcpy(&session->bc_attrs, &res->bc_attrs,
8092 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008093}
8094
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008095static int _nfs4_proc_create_session(struct nfs_client *clp,
8096 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008097{
8098 struct nfs4_session *session = clp->cl_session;
8099 struct nfs41_create_session_args args = {
8100 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008101 .clientid = clp->cl_clientid,
8102 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008103 .cb_program = NFS4_CALLBACK,
8104 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008105 struct nfs41_create_session_res res;
8106
Andy Adamsonfc931582009-04-01 09:22:31 -04008107 struct rpc_message msg = {
8108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8109 .rpc_argp = &args,
8110 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008111 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008112 };
8113 int status;
8114
Chuck Lever6b26cc82016-05-02 14:40:40 -04008115 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008116 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008117
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008118 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008119 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008120
Trond Myklebustb519d402016-09-11 14:50:01 -04008121 switch (status) {
8122 case -NFS4ERR_STALE_CLIENTID:
8123 case -NFS4ERR_DELAY:
8124 case -ETIMEDOUT:
8125 case -EACCES:
8126 case -EAGAIN:
8127 goto out;
8128 };
8129
8130 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008131 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008132 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008133 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008134 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008135 if (status)
8136 goto out;
8137 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008138 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008139out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008140 return status;
8141}
8142
8143/*
8144 * Issues a CREATE_SESSION operation to the server.
8145 * It is the responsibility of the caller to verify the session is
8146 * expired before calling this routine.
8147 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008148int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008149{
8150 int status;
8151 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008152 struct nfs4_session *session = clp->cl_session;
8153
8154 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8155
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008156 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008157 if (status)
8158 goto out;
8159
Andy Adamsonaacd5532011-11-09 13:58:21 -05008160 /* Init or reset the session slot tables */
8161 status = nfs4_setup_session_slot_tables(session);
8162 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008163 if (status)
8164 goto out;
8165
8166 ptr = (unsigned *)&session->sess_id.data[0];
8167 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8168 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008169out:
8170 dprintk("<-- %s\n", __func__);
8171 return status;
8172}
8173
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008174/*
8175 * Issue the over-the-wire RPC DESTROY_SESSION.
8176 * The caller must serialize access to this routine.
8177 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008178int nfs4_proc_destroy_session(struct nfs4_session *session,
8179 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008180{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008181 struct rpc_message msg = {
8182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8183 .rpc_argp = session,
8184 .rpc_cred = cred,
8185 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008186 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008187
8188 dprintk("--> nfs4_proc_destroy_session\n");
8189
8190 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008191 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8192 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008193
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008194 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008195 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008196
8197 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008198 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008199 "Session has been destroyed regardless...\n", status);
8200
8201 dprintk("<-- nfs4_proc_destroy_session\n");
8202 return status;
8203}
8204
Trond Myklebust7b38c362012-05-23 13:23:31 -04008205/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008206 * Renew the cl_session lease.
8207 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008208struct nfs4_sequence_data {
8209 struct nfs_client *clp;
8210 struct nfs4_sequence_args args;
8211 struct nfs4_sequence_res res;
8212};
8213
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008214static void nfs41_sequence_release(void *data)
8215{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008216 struct nfs4_sequence_data *calldata = data;
8217 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008218
Elena Reshetova212bf412017-10-20 12:53:38 +03008219 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008220 nfs4_schedule_state_renewal(clp);
8221 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008222 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008223}
8224
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008225static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8226{
8227 switch(task->tk_status) {
8228 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8230 return -EAGAIN;
8231 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008232 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008233 }
8234 return 0;
8235}
8236
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008237static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008238{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008239 struct nfs4_sequence_data *calldata = data;
8240 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008241
Trond Myklebust14516c32010-07-31 14:29:06 -04008242 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8243 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008244
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008245 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008246 if (task->tk_status < 0) {
8247 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008248 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008249 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008250
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008251 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8252 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008253 return;
8254 }
8255 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008256 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008257out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008258 dprintk("<-- %s\n", __func__);
8259}
8260
8261static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8262{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008263 struct nfs4_sequence_data *calldata = data;
8264 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008265 struct nfs4_sequence_args *args;
8266 struct nfs4_sequence_res *res;
8267
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008268 args = task->tk_msg.rpc_argp;
8269 res = task->tk_msg.rpc_resp;
8270
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008271 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008272}
8273
8274static const struct rpc_call_ops nfs41_sequence_ops = {
8275 .rpc_call_done = nfs41_sequence_call_done,
8276 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008277 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008278};
8279
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008280static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8281 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008282 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008283 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008284{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008285 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008286 struct rpc_message msg = {
8287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8288 .rpc_cred = cred,
8289 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008290 struct rpc_task_setup task_setup_data = {
8291 .rpc_client = clp->cl_rpcclient,
8292 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008293 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008294 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008295 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008296 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008297
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008298 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008299 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008300 goto out_err;
8301
8302 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008303 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008304 if (calldata == NULL)
8305 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008306 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008307 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008308 if (is_privileged)
8309 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008310 msg.rpc_argp = &calldata->args;
8311 msg.rpc_resp = &calldata->res;
8312 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008313 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008314
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008315 ret = rpc_run_task(&task_setup_data);
8316 if (IS_ERR(ret))
8317 goto out_err;
8318 return ret;
8319out_put_clp:
8320 nfs_put_client(clp);
8321out_err:
8322 nfs41_release_slot(slot);
8323 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008324}
8325
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008326static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008327{
8328 struct rpc_task *task;
8329 int ret = 0;
8330
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008331 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008332 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008333 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008334 if (IS_ERR(task))
8335 ret = PTR_ERR(task);
8336 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008337 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008338 dprintk("<-- %s status=%d\n", __func__, ret);
8339 return ret;
8340}
8341
8342static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8343{
8344 struct rpc_task *task;
8345 int ret;
8346
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008347 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008348 if (IS_ERR(task)) {
8349 ret = PTR_ERR(task);
8350 goto out;
8351 }
8352 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008353 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008354 ret = task->tk_status;
8355 rpc_put_task(task);
8356out:
8357 dprintk("<-- %s status=%d\n", __func__, ret);
8358 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008359}
8360
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008361struct nfs4_reclaim_complete_data {
8362 struct nfs_client *clp;
8363 struct nfs41_reclaim_complete_args arg;
8364 struct nfs41_reclaim_complete_res res;
8365};
8366
8367static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8368{
8369 struct nfs4_reclaim_complete_data *calldata = data;
8370
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008371 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008372 &calldata->arg.seq_args,
8373 &calldata->res.seq_res,
8374 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008375}
8376
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008377static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8378{
8379 switch(task->tk_status) {
8380 case 0:
8381 case -NFS4ERR_COMPLETE_ALREADY:
8382 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8383 break;
8384 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008385 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008386 /* fall through */
8387 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008388 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008389 case -NFS4ERR_BADSESSION:
8390 case -NFS4ERR_DEADSESSION:
8391 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8392 nfs4_schedule_session_recovery(clp->cl_session,
8393 task->tk_status);
8394 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008395 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008396 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008397 }
8398 return 0;
8399}
8400
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008401static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8402{
8403 struct nfs4_reclaim_complete_data *calldata = data;
8404 struct nfs_client *clp = calldata->clp;
8405 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8406
8407 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008408 if (!nfs41_sequence_done(task, res))
8409 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008410
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008411 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008412 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8413 rpc_restart_call_prepare(task);
8414 return;
8415 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008416 dprintk("<-- %s\n", __func__);
8417}
8418
8419static void nfs4_free_reclaim_complete_data(void *data)
8420{
8421 struct nfs4_reclaim_complete_data *calldata = data;
8422
8423 kfree(calldata);
8424}
8425
8426static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8427 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8428 .rpc_call_done = nfs4_reclaim_complete_done,
8429 .rpc_release = nfs4_free_reclaim_complete_data,
8430};
8431
8432/*
8433 * Issue a global reclaim complete.
8434 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008435static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8436 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008437{
8438 struct nfs4_reclaim_complete_data *calldata;
8439 struct rpc_task *task;
8440 struct rpc_message msg = {
8441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008442 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008443 };
8444 struct rpc_task_setup task_setup_data = {
8445 .rpc_client = clp->cl_rpcclient,
8446 .rpc_message = &msg,
8447 .callback_ops = &nfs4_reclaim_complete_call_ops,
8448 .flags = RPC_TASK_ASYNC,
8449 };
8450 int status = -ENOMEM;
8451
8452 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008453 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008454 if (calldata == NULL)
8455 goto out;
8456 calldata->clp = clp;
8457 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008458
Chuck Levera9c92d62013-08-09 12:48:18 -04008459 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008460 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008461 msg.rpc_argp = &calldata->arg;
8462 msg.rpc_resp = &calldata->res;
8463 task_setup_data.callback_data = calldata;
8464 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008465 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008466 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008467 goto out;
8468 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008469 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008470 if (status == 0)
8471 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008472 rpc_put_task(task);
8473out:
8474 dprintk("<-- %s status=%d\n", __func__, status);
8475 return status;
8476}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477
8478static void
8479nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8480{
8481 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008482 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008483
8484 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008485 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008486 &lgp->res.seq_res, task);
8487 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008488}
8489
8490static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8491{
8492 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008493
8494 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008495 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008496 dprintk("<-- %s\n", __func__);
8497}
8498
8499static int
8500nfs4_layoutget_handle_exception(struct rpc_task *task,
8501 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8502{
Trond Myklebustee314c22012-10-01 17:25:48 -07008503 struct inode *inode = lgp->args.inode;
8504 struct nfs_server *server = NFS_SERVER(inode);
8505 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008506 int nfs4err = task->tk_status;
8507 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008508 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008509
Boaz Harroshed7e5422014-01-22 20:34:54 +02008510 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008512 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008513 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008514 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008515
8516 /*
8517 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8518 * on the file. set tk_status to -ENODATA to tell upper layer to
8519 * retry go inband.
8520 */
8521 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008522 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008523 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008524 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008525 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8526 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8527 */
8528 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008529 status = -EOVERFLOW;
8530 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008531 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008532 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008533 * (or clients) writing to the same RAID stripe except when
8534 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008535 *
8536 * Treat it like we would RECALLCONFLICT -- we retry for a little
8537 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008538 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008539 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008540 if (lgp->args.minlength == 0) {
8541 status = -EOVERFLOW;
8542 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008543 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008544 status = -EBUSY;
8545 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008546 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008547 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008548 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008549 case -NFS4ERR_DELEG_REVOKED:
8550 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008551 case -NFS4ERR_EXPIRED:
8552 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008553 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008554 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008555 lo = NFS_I(inode)->layout;
8556 /* If the open stateid was bad, then recover it. */
8557 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008558 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008559 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008560 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008561 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008562 break;
8563 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008564
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008565 /*
8566 * Mark the bad layout state as invalid, then retry
8567 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008568 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008569 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008570 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008571 pnfs_free_lseg_list(&head);
8572 status = -EAGAIN;
8573 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008574 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008575
Trond Myklebust8ac09252017-01-23 22:44:12 -05008576 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008577 err = nfs4_handle_exception(server, nfs4err, exception);
8578 if (!status) {
8579 if (exception->retry)
8580 status = -EAGAIN;
8581 else
8582 status = err;
8583 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008584out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008585 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008586 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008587}
8588
Idan Kedar85541162012-08-02 11:47:10 +03008589static size_t max_response_pages(struct nfs_server *server)
8590{
8591 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8592 return nfs_page_array_len(0, max_resp_sz);
8593}
8594
8595static void nfs4_free_pages(struct page **pages, size_t size)
8596{
8597 int i;
8598
8599 if (!pages)
8600 return;
8601
8602 for (i = 0; i < size; i++) {
8603 if (!pages[i])
8604 break;
8605 __free_page(pages[i]);
8606 }
8607 kfree(pages);
8608}
8609
8610static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8611{
8612 struct page **pages;
8613 int i;
8614
8615 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8616 if (!pages) {
8617 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8618 return NULL;
8619 }
8620
8621 for (i = 0; i < size; i++) {
8622 pages[i] = alloc_page(gfp_flags);
8623 if (!pages[i]) {
8624 dprintk("%s: failed to allocate page\n", __func__);
8625 nfs4_free_pages(pages, size);
8626 return NULL;
8627 }
8628 }
8629
8630 return pages;
8631}
8632
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008633static void nfs4_layoutget_release(void *calldata)
8634{
8635 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008636 struct inode *inode = lgp->args.inode;
8637 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008638 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008639
8640 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008641 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008642 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008643 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008644 put_nfs_open_context(lgp->args.ctx);
8645 kfree(calldata);
8646 dprintk("<-- %s\n", __func__);
8647}
8648
8649static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8650 .rpc_call_prepare = nfs4_layoutget_prepare,
8651 .rpc_call_done = nfs4_layoutget_done,
8652 .rpc_release = nfs4_layoutget_release,
8653};
8654
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008655struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008656nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008657{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008658 struct inode *inode = lgp->args.inode;
8659 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008660 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008661 struct rpc_task *task;
8662 struct rpc_message msg = {
8663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8664 .rpc_argp = &lgp->args,
8665 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008666 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008667 };
8668 struct rpc_task_setup task_setup_data = {
8669 .rpc_client = server->client,
8670 .rpc_message = &msg,
8671 .callback_ops = &nfs4_layoutget_call_ops,
8672 .callback_data = lgp,
8673 .flags = RPC_TASK_ASYNC,
8674 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008675 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008676 struct nfs4_exception exception = {
8677 .inode = inode,
8678 .timeout = *timeout,
8679 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008680 int status = 0;
8681
8682 dprintk("--> %s\n", __func__);
8683
Peng Tao4bd5a982014-11-17 11:05:17 +08008684 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8685 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8686
Idan Kedar85541162012-08-02 11:47:10 +03008687 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8688 if (!lgp->args.layout.pages) {
8689 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008690 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008691 }
8692 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8693
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008694 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008695 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008696 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008697
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008698 task = rpc_run_task(&task_setup_data);
8699 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008700 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008701 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008702 if (status == 0) {
8703 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8704 *timeout = exception.timeout;
8705 }
8706
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008707 trace_nfs4_layoutget(lgp->args.ctx,
8708 &lgp->args.range,
8709 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008710 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008711 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008712
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008713 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8714 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008715 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008716 rpc_put_task(task);
8717 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008718 if (status)
8719 return ERR_PTR(status);
8720 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008721}
8722
Benny Halevycbe82602011-05-22 19:52:37 +03008723static void
8724nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8725{
8726 struct nfs4_layoutreturn *lrp = calldata;
8727
8728 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008729 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008730 &lrp->args.seq_args,
8731 &lrp->res.seq_res,
8732 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008733}
8734
8735static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8736{
8737 struct nfs4_layoutreturn *lrp = calldata;
8738 struct nfs_server *server;
8739
8740 dprintk("--> %s\n", __func__);
8741
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008742 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008743 return;
8744
8745 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008746 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008747 case -NFS4ERR_OLD_STATEID:
8748 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8749 lrp->args.inode))
8750 goto out_restart;
8751 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008752 default:
8753 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008754 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008755 case 0:
8756 break;
8757 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008758 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008759 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008760 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008761 }
Benny Halevycbe82602011-05-22 19:52:37 +03008762 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008763 return;
8764out_restart:
8765 task->tk_status = 0;
8766 nfs4_sequence_free_slot(&lrp->res.seq_res);
8767 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008768}
8769
8770static void nfs4_layoutreturn_release(void *calldata)
8771{
8772 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008773 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008774
8775 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008776 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008777 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008778 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008779 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8780 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008781 pnfs_put_layout_hdr(lrp->args.layout);
8782 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008783 kfree(calldata);
8784 dprintk("<-- %s\n", __func__);
8785}
8786
8787static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8788 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8789 .rpc_call_done = nfs4_layoutreturn_done,
8790 .rpc_release = nfs4_layoutreturn_release,
8791};
8792
Peng Tao6c166052014-11-17 09:30:40 +08008793int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008794{
8795 struct rpc_task *task;
8796 struct rpc_message msg = {
8797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8798 .rpc_argp = &lrp->args,
8799 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008800 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008801 };
8802 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008803 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008804 .rpc_message = &msg,
8805 .callback_ops = &nfs4_layoutreturn_call_ops,
8806 .callback_data = lrp,
8807 };
Peng Tao6c166052014-11-17 09:30:40 +08008808 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008809
Andrew Elble99ade3c2015-12-02 09:39:51 -05008810 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8811 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8812 &task_setup_data.rpc_client, &msg);
8813
Benny Halevycbe82602011-05-22 19:52:37 +03008814 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008815 if (!sync) {
8816 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8817 if (!lrp->inode) {
8818 nfs4_layoutreturn_release(lrp);
8819 return -EAGAIN;
8820 }
8821 task_setup_data.flags |= RPC_TASK_ASYNC;
8822 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008823 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008824 task = rpc_run_task(&task_setup_data);
8825 if (IS_ERR(task))
8826 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008827 if (sync)
8828 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008829 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008830 dprintk("<-- %s status=%d\n", __func__, status);
8831 rpc_put_task(task);
8832 return status;
8833}
8834
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008835static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008836_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8837 struct pnfs_device *pdev,
8838 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008839{
8840 struct nfs4_getdeviceinfo_args args = {
8841 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008842 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8843 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008844 };
8845 struct nfs4_getdeviceinfo_res res = {
8846 .pdev = pdev,
8847 };
8848 struct rpc_message msg = {
8849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8850 .rpc_argp = &args,
8851 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008852 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008853 };
8854 int status;
8855
8856 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008857 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008858 if (res.notification & ~args.notify_types)
8859 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008860 if (res.notification != args.notify_types)
8861 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008862
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008863 dprintk("<-- %s status=%d\n", __func__, status);
8864
8865 return status;
8866}
8867
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008868int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8869 struct pnfs_device *pdev,
8870 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008871{
8872 struct nfs4_exception exception = { };
8873 int err;
8874
8875 do {
8876 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008877 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008878 &exception);
8879 } while (exception.retry);
8880 return err;
8881}
8882EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8883
Andy Adamson863a3c62011-03-23 13:27:54 +00008884static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8885{
8886 struct nfs4_layoutcommit_data *data = calldata;
8887 struct nfs_server *server = NFS_SERVER(data->args.inode);
8888
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008889 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008890 &data->args.seq_args,
8891 &data->res.seq_res,
8892 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008893}
8894
8895static void
8896nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8897{
8898 struct nfs4_layoutcommit_data *data = calldata;
8899 struct nfs_server *server = NFS_SERVER(data->args.inode);
8900
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008901 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008902 return;
8903
8904 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008905 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8906 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8907 case -NFS4ERR_BADLAYOUT: /* no layout */
8908 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008909 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008910 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008911 break;
8912 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008913 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008914 rpc_restart_call_prepare(task);
8915 return;
8916 }
8917 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008918}
8919
8920static void nfs4_layoutcommit_release(void *calldata)
8921{
8922 struct nfs4_layoutcommit_data *data = calldata;
8923
Andy Adamsondb29c082011-07-30 20:52:38 -04008924 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008925 nfs_post_op_update_inode_force_wcc(data->args.inode,
8926 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008927 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008928 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008929 kfree(data);
8930}
8931
8932static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8933 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8934 .rpc_call_done = nfs4_layoutcommit_done,
8935 .rpc_release = nfs4_layoutcommit_release,
8936};
8937
8938int
Andy Adamsonef311532011-03-12 02:58:10 -05008939nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008940{
8941 struct rpc_message msg = {
8942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8943 .rpc_argp = &data->args,
8944 .rpc_resp = &data->res,
8945 .rpc_cred = data->cred,
8946 };
8947 struct rpc_task_setup task_setup_data = {
8948 .task = &data->task,
8949 .rpc_client = NFS_CLIENT(data->args.inode),
8950 .rpc_message = &msg,
8951 .callback_ops = &nfs4_layoutcommit_ops,
8952 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008953 };
8954 struct rpc_task *task;
8955 int status = 0;
8956
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008957 dprintk("NFS: initiating layoutcommit call. sync %d "
8958 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008959 data->args.lastbytewritten,
8960 data->args.inode->i_ino);
8961
Trond Myklebust472e2592015-02-05 16:50:30 -05008962 if (!sync) {
8963 data->inode = nfs_igrab_and_active(data->args.inode);
8964 if (data->inode == NULL) {
8965 nfs4_layoutcommit_release(data);
8966 return -EAGAIN;
8967 }
8968 task_setup_data.flags = RPC_TASK_ASYNC;
8969 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008970 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008971 task = rpc_run_task(&task_setup_data);
8972 if (IS_ERR(task))
8973 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008974 if (sync)
8975 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008976 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008977 dprintk("%s: status %d\n", __func__, status);
8978 rpc_put_task(task);
8979 return status;
8980}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008981
Andy Adamson97431202013-08-08 10:57:56 -04008982/**
8983 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8984 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8985 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008986static int
8987_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008988 struct nfs_fsinfo *info,
8989 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008990{
8991 struct nfs41_secinfo_no_name_args args = {
8992 .style = SECINFO_STYLE_CURRENT_FH,
8993 };
8994 struct nfs4_secinfo_res res = {
8995 .flavors = flavors,
8996 };
8997 struct rpc_message msg = {
8998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8999 .rpc_argp = &args,
9000 .rpc_resp = &res,
9001 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009002 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009003 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009004 int status;
9005
9006 if (use_integrity) {
9007 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009008 cred = nfs4_get_clid_cred(server->nfs_client);
9009 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009010 }
9011
9012 dprintk("--> %s\n", __func__);
9013 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9014 &res.seq_res, 0);
9015 dprintk("<-- %s status=%d\n", __func__, status);
9016
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009017 if (cred)
9018 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009019
9020 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009021}
9022
9023static int
9024nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9025 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9026{
9027 struct nfs4_exception exception = { };
9028 int err;
9029 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009030 /* first try using integrity protection */
9031 err = -NFS4ERR_WRONGSEC;
9032
9033 /* try to use integrity protection with machine cred */
9034 if (_nfs4_is_integrity_protected(server->nfs_client))
9035 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9036 flavors, true);
9037
9038 /*
9039 * if unable to use integrity protection, or SECINFO with
9040 * integrity protection returns NFS4ERR_WRONGSEC (which is
9041 * disallowed by spec, but exists in deployed servers) use
9042 * the current filesystem's rpc_client and the user cred.
9043 */
9044 if (err == -NFS4ERR_WRONGSEC)
9045 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9046 flavors, false);
9047
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009048 switch (err) {
9049 case 0:
9050 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009051 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009052 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009053 default:
9054 err = nfs4_handle_exception(server, err, &exception);
9055 }
9056 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009057out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009058 return err;
9059}
9060
9061static int
9062nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9063 struct nfs_fsinfo *info)
9064{
9065 int err;
9066 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009067 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009068 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009069 struct nfs4_secinfo4 *secinfo;
9070 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009071
9072 page = alloc_page(GFP_KERNEL);
9073 if (!page) {
9074 err = -ENOMEM;
9075 goto out;
9076 }
9077
9078 flavors = page_address(page);
9079 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9080
9081 /*
9082 * Fall back on "guess and check" method if
9083 * the server doesn't support SECINFO_NO_NAME
9084 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009085 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009086 err = nfs4_find_root_sec(server, fhandle, info);
9087 goto out_freepage;
9088 }
9089 if (err)
9090 goto out_freepage;
9091
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009092 for (i = 0; i < flavors->num_flavors; i++) {
9093 secinfo = &flavors->flavors[i];
9094
9095 switch (secinfo->flavor) {
9096 case RPC_AUTH_NULL:
9097 case RPC_AUTH_UNIX:
9098 case RPC_AUTH_GSS:
9099 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9100 &secinfo->flavor_info);
9101 break;
9102 default:
9103 flavor = RPC_AUTH_MAXFLAVOR;
9104 break;
9105 }
9106
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009107 if (!nfs_auth_info_match(&server->auth_info, flavor))
9108 flavor = RPC_AUTH_MAXFLAVOR;
9109
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009110 if (flavor != RPC_AUTH_MAXFLAVOR) {
9111 err = nfs4_lookup_root_sec(server, fhandle,
9112 info, flavor);
9113 if (!err)
9114 break;
9115 }
9116 }
9117
9118 if (flavor == RPC_AUTH_MAXFLAVOR)
9119 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009120
9121out_freepage:
9122 put_page(page);
9123 if (err == -EACCES)
9124 return -EPERM;
9125out:
9126 return err;
9127}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009128
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009129static int _nfs41_test_stateid(struct nfs_server *server,
9130 nfs4_stateid *stateid,
9131 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009132{
9133 int status;
9134 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009135 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009136 };
9137 struct nfs41_test_stateid_res res;
9138 struct rpc_message msg = {
9139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9140 .rpc_argp = &args,
9141 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009142 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009143 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009144 struct rpc_clnt *rpc_client = server->client;
9145
9146 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9147 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009148
Chuck Lever38527b12012-07-11 16:30:23 -04009149 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009150 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009151 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009152 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009153 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009154 if (status != NFS_OK) {
9155 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009156 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009157 }
9158 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009159 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009160}
9161
Trond Myklebust43912bb2016-09-22 13:38:56 -04009162static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9163 int err, struct nfs4_exception *exception)
9164{
9165 exception->retry = 0;
9166 switch(err) {
9167 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009168 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009169 nfs4_handle_exception(server, err, exception);
9170 break;
9171 case -NFS4ERR_BADSESSION:
9172 case -NFS4ERR_BADSLOT:
9173 case -NFS4ERR_BAD_HIGH_SLOT:
9174 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9175 case -NFS4ERR_DEADSESSION:
9176 nfs4_do_handle_exception(server, err, exception);
9177 }
9178}
9179
Chuck Lever38527b12012-07-11 16:30:23 -04009180/**
9181 * nfs41_test_stateid - perform a TEST_STATEID operation
9182 *
9183 * @server: server / transport on which to perform the operation
9184 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009185 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009186 *
9187 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9188 * Otherwise a negative NFS4ERR value is returned if the operation
9189 * failed or the state ID is not currently valid.
9190 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009191static int nfs41_test_stateid(struct nfs_server *server,
9192 nfs4_stateid *stateid,
9193 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009194{
9195 struct nfs4_exception exception = { };
9196 int err;
9197 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009198 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009199 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009200 } while (exception.retry);
9201 return err;
9202}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009203
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009204struct nfs_free_stateid_data {
9205 struct nfs_server *server;
9206 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009207 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009208};
9209
9210static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9211{
9212 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009213 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009214 &data->args.seq_args,
9215 &data->res.seq_res,
9216 task);
9217}
9218
9219static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9220{
9221 struct nfs_free_stateid_data *data = calldata;
9222
9223 nfs41_sequence_done(task, &data->res.seq_res);
9224
9225 switch (task->tk_status) {
9226 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009227 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009228 rpc_restart_call_prepare(task);
9229 }
9230}
9231
9232static void nfs41_free_stateid_release(void *calldata)
9233{
9234 kfree(calldata);
9235}
9236
Trond Myklebust17f26b12013-08-21 15:48:42 -04009237static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009238 .rpc_call_prepare = nfs41_free_stateid_prepare,
9239 .rpc_call_done = nfs41_free_stateid_done,
9240 .rpc_release = nfs41_free_stateid_release,
9241};
9242
9243static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009244 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009245 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009246 bool privileged)
9247{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009248 struct rpc_message msg = {
9249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009250 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009251 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009252 struct rpc_task_setup task_setup = {
9253 .rpc_client = server->client,
9254 .rpc_message = &msg,
9255 .callback_ops = &nfs41_free_stateid_ops,
9256 .flags = RPC_TASK_ASYNC,
9257 };
9258 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009259
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009260 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9261 &task_setup.rpc_client, &msg);
9262
Chuck Lever38527b12012-07-11 16:30:23 -04009263 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009264 data = kmalloc(sizeof(*data), GFP_NOFS);
9265 if (!data)
9266 return ERR_PTR(-ENOMEM);
9267 data->server = server;
9268 nfs4_stateid_copy(&data->args.stateid, stateid);
9269
9270 task_setup.callback_data = data;
9271
9272 msg.rpc_argp = &data->args;
9273 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009274 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009275 if (privileged)
9276 nfs4_set_sequence_privileged(&data->args.seq_args);
9277
9278 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009279}
9280
Chuck Lever38527b12012-07-11 16:30:23 -04009281/**
9282 * nfs41_free_stateid - perform a FREE_STATEID operation
9283 *
9284 * @server: server / transport on which to perform the operation
9285 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009286 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009287 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009288 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009289 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009290 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009291static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009292 const nfs4_stateid *stateid,
9293 struct rpc_cred *cred,
9294 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009295{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009296 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009297
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009298 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009299 if (IS_ERR(task))
9300 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009301 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009302 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009303}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009304
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009305static void
9306nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009307{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009308 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009309
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009310 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009311 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009312}
9313
Trond Myklebust36281ca2012-03-04 18:13:56 -05009314static bool nfs41_match_stateid(const nfs4_stateid *s1,
9315 const nfs4_stateid *s2)
9316{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009317 if (s1->type != s2->type)
9318 return false;
9319
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009320 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009321 return false;
9322
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009323 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009324 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009325
Anna Schumaker045c5512017-01-11 16:59:48 -05009326 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009327}
9328
Andy Adamson557134a2009-04-01 09:21:53 -04009329#endif /* CONFIG_NFS_V4_1 */
9330
Trond Myklebust36281ca2012-03-04 18:13:56 -05009331static bool nfs4_match_stateid(const nfs4_stateid *s1,
9332 const nfs4_stateid *s2)
9333{
Trond Myklebustf597c532012-03-04 18:13:56 -05009334 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009335}
9336
9337
Trond Myklebust17280172012-03-11 13:11:00 -04009338static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009339 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009340 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009341 .recover_open = nfs4_open_reclaim,
9342 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009343 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009344 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345};
9346
Andy Adamson591d71c2009-04-01 09:22:47 -04009347#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009348static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009349 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9350 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9351 .recover_open = nfs4_open_reclaim,
9352 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009353 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009354 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009355 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009356};
9357#endif /* CONFIG_NFS_V4_1 */
9358
Trond Myklebust17280172012-03-11 13:11:00 -04009359static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009360 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009361 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009362 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009364 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365};
9366
Andy Adamson591d71c2009-04-01 09:22:47 -04009367#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009368static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009369 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9370 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009371 .recover_open = nfs41_open_expired,
9372 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009373 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009374};
9375#endif /* CONFIG_NFS_V4_1 */
9376
Trond Myklebust17280172012-03-11 13:11:00 -04009377static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009378 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009379 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009380 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009381};
9382
9383#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009384static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009385 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009386 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009387 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009388};
9389#endif
9390
Chuck Leverec011fe2013-10-17 14:12:39 -04009391static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009392 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009393 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009394};
9395
9396#if defined(CONFIG_NFS_V4_1)
9397static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009398 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009399 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009400};
9401#endif /* CONFIG_NFS_V4_1 */
9402
Trond Myklebust97dc1352010-06-16 09:52:26 -04009403static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9404 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009405 .init_caps = NFS_CAP_READDIRPLUS
9406 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009407 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009408 .init_client = nfs40_init_client,
9409 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009410 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009411 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009412 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009413 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009414 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009415 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009416 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9417 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9418 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009419 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009420};
9421
9422#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009423static struct nfs_seqid *
9424nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9425{
9426 return NULL;
9427}
9428
Trond Myklebust97dc1352010-06-16 09:52:26 -04009429static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9430 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009431 .init_caps = NFS_CAP_READDIRPLUS
9432 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009433 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009434 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009435 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009436 .init_client = nfs41_init_client,
9437 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009438 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009439 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009440 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009441 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009442 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009443 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009444 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009445 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9446 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9447 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009448 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009449};
9450#endif
9451
Steve Dickson42c2c422013-05-22 12:50:38 -04009452#if defined(CONFIG_NFS_V4_2)
9453static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9454 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009455 .init_caps = NFS_CAP_READDIRPLUS
9456 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009457 | NFS_CAP_POSIX_LOCK
9458 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009459 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009460 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009461 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009462 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009463 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009464 | NFS_CAP_LAYOUTSTATS
9465 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009466 .init_client = nfs41_init_client,
9467 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009468 .match_stateid = nfs41_match_stateid,
9469 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009470 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009471 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009472 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009473 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009474 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009475 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9476 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9477 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009478 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009479};
9480#endif
9481
Trond Myklebust97dc1352010-06-16 09:52:26 -04009482const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9483 [0] = &nfs_v4_0_minor_ops,
9484#if defined(CONFIG_NFS_V4_1)
9485 [1] = &nfs_v4_1_minor_ops,
9486#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009487#if defined(CONFIG_NFS_V4_2)
9488 [2] = &nfs_v4_2_minor_ops,
9489#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009490};
9491
Trond Myklebust13997822016-07-24 17:10:52 -04009492static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009493{
9494 ssize_t error, error2;
9495
9496 error = generic_listxattr(dentry, list, size);
9497 if (error < 0)
9498 return error;
9499 if (list) {
9500 list += error;
9501 size -= error;
9502 }
9503
9504 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9505 if (error2 < 0)
9506 return error2;
9507 return error + error2;
9508}
9509
Trond Myklebust17f26b12013-08-21 15:48:42 -04009510static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009511 .create = nfs_create,
9512 .lookup = nfs_lookup,
9513 .atomic_open = nfs_atomic_open,
9514 .link = nfs_link,
9515 .unlink = nfs_unlink,
9516 .symlink = nfs_symlink,
9517 .mkdir = nfs_mkdir,
9518 .rmdir = nfs_rmdir,
9519 .mknod = nfs_mknod,
9520 .rename = nfs_rename,
9521 .permission = nfs_permission,
9522 .getattr = nfs_getattr,
9523 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009524 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009525};
9526
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009527static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009528 .permission = nfs_permission,
9529 .getattr = nfs_getattr,
9530 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009531 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009532};
9533
David Howells509de812006-08-22 20:06:11 -04009534const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 .version = 4, /* protocol version */
9536 .dentry_ops = &nfs4_dentry_operations,
9537 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009538 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009539 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009541 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009542 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009543 .getattr = nfs4_proc_getattr,
9544 .setattr = nfs4_proc_setattr,
9545 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009546 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009547 .access = nfs4_proc_access,
9548 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009549 .create = nfs4_proc_create,
9550 .remove = nfs4_proc_remove,
9551 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009552 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009553 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009554 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009555 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009556 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009557 .link = nfs4_proc_link,
9558 .symlink = nfs4_proc_symlink,
9559 .mkdir = nfs4_proc_mkdir,
9560 .rmdir = nfs4_proc_remove,
9561 .readdir = nfs4_proc_readdir,
9562 .mknod = nfs4_proc_mknod,
9563 .statfs = nfs4_proc_statfs,
9564 .fsinfo = nfs4_proc_fsinfo,
9565 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009566 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009568 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009570 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009572 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009574 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009575 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009577 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009578 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009579 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009580 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009581 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009582 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009583 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009584 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009585 .create_server = nfs4_create_server,
9586 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587};
9588
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009589static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009590 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009591 .list = nfs4_xattr_list_nfs4_acl,
9592 .get = nfs4_xattr_get_nfs4_acl,
9593 .set = nfs4_xattr_set_nfs4_acl,
9594};
9595
9596const struct xattr_handler *nfs4_xattr_handlers[] = {
9597 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009598#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9599 &nfs4_xattr_nfs4_label_handler,
9600#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009601 NULL
9602};
9603
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604/*
9605 * Local variables:
9606 * c-basic-offset: 8
9607 * End:
9608 */