blob: e7ca62a86dabf20ff2a026fa0686a4bbfeacad46 [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,
David Quigley1775fd32013-05-22 12:50:42 -040096 struct nfs4_state *state, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
228 | FATTR4_WORD0_CHANGE
229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500258 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500263 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD1_OWNER_GROUP
265 | FATTR4_WORD1_RAWDEV
266 | FATTR4_WORD1_SPACE_USED
267 | FATTR4_WORD1_TIME_ACCESS
268 | FATTR4_WORD1_TIME_METADATA
269 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400270 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400271};
272
Al Virobc4785c2006-10-19 23:28:51 -0700273static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct nfs4_readdir_arg *readdir)
275{
Al Viro0dbb4c62006-10-19 23:28:49 -0700276 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000279 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
281 return;
282 }
283
284 readdir->cookie = 0;
285 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
286 if (cookie == 2)
287 return;
288
289 /*
290 * NFSv4 servers do not return entries for '.' and '..'
291 * Therefore, we fake these entries here. We let '.'
292 * have cookie 0 and '..' have cookie 1. Note that
293 * when talking to the server, we always send cookie 0
294 * instead of 1 or 2.
295 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800296 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (cookie == 0) {
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_one; /* cookie, second word */
302 *p++ = xdr_one; /* entry len */
303 memcpy(p, ".\0\0\0", 4); /* entry */
304 p++;
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000308 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 *p++ = xdr_one; /* next */
312 *p++ = xdr_zero; /* cookie, first word */
313 *p++ = xdr_two; /* cookie, second word */
314 *p++ = xdr_two; /* entry len */
315 memcpy(p, "..\0\0", 4); /* entry */
316 p++;
317 *p++ = xdr_one; /* bitmap length */
318 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
319 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000320 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 readdir->pgbase = (char *)p - (char *)start;
323 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800324 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Trond Myklebust26d36302016-09-22 13:39:05 -0400327static void nfs4_test_and_free_stateid(struct nfs_server *server,
328 nfs4_stateid *stateid,
329 struct rpc_cred *cred)
330{
331 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
332
333 ops->test_and_free_expired(server, stateid, cred);
334}
335
336static void __nfs4_free_revoked_stateid(struct nfs_server *server,
337 nfs4_stateid *stateid,
338 struct rpc_cred *cred)
339{
340 stateid->type = NFS4_REVOKED_STATEID_TYPE;
341 nfs4_test_and_free_stateid(server, stateid, cred);
342}
343
344static void nfs4_free_revoked_stateid(struct nfs_server *server,
345 const nfs4_stateid *stateid,
346 struct rpc_cred *cred)
347{
348 nfs4_stateid tmp;
349
350 nfs4_stateid_copy(&tmp, stateid);
351 __nfs4_free_revoked_stateid(server, &tmp, cred);
352}
353
NeilBrown8478eaa2014-09-18 16:09:27 +1000354static long nfs4_update_delay(long *timeout)
355{
356 long ret;
357 if (!timeout)
358 return NFS4_POLL_RETRY_MAX;
359 if (*timeout <= 0)
360 *timeout = NFS4_POLL_RETRY_MIN;
361 if (*timeout > NFS4_POLL_RETRY_MAX)
362 *timeout = NFS4_POLL_RETRY_MAX;
363 ret = *timeout;
364 *timeout <<= 1;
365 return ret;
366}
367
Trond Myklebust65de8722008-12-23 15:21:44 -0500368static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
369{
370 int res = 0;
371
372 might_sleep();
373
NeilBrown8478eaa2014-09-18 16:09:27 +1000374 freezable_schedule_timeout_killable_unsafe(
375 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500376 if (fatal_signal_pending(current))
377 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 return res;
379}
380
381/* This is the error handling routine for processes that are allowed
382 * to sleep.
383 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400384static int nfs4_do_handle_exception(struct nfs_server *server,
385 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500386{
387 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500388 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400389 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500390 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500391 int ret = errorcode;
392
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400393 exception->delay = 0;
394 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400396
397 if (stateid == NULL && state != NULL)
398 stateid = &state->stateid;
399
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 switch(errorcode) {
401 case 0:
402 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400403 case -NFS4ERR_DELEG_REVOKED:
404 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400405 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 if (inode != NULL && stateid != NULL) {
408 nfs_inode_find_state_and_recover(inode,
409 stateid);
410 goto wait_on_recovery;
411 }
412 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400413 if (inode) {
414 int err;
415
416 err = nfs_async_inode_return_delegation(inode,
417 stateid);
418 if (err == 0)
419 goto wait_on_recovery;
420 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
421 exception->retry = 1;
422 break;
423 }
424 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 if (state == NULL)
426 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400427 ret = nfs4_schedule_stateid_recovery(server, state);
428 if (ret < 0)
429 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500430 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500431 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500433 nfs4_schedule_lease_recovery(clp);
434 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400435 case -NFS4ERR_MOVED:
436 ret = nfs4_schedule_migration_recovery(server);
437 if (ret < 0)
438 break;
439 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400440 case -NFS4ERR_LEASE_MOVED:
441 nfs4_schedule_lease_moved_recovery(clp);
442 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500443#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400444 case -NFS4ERR_BADSESSION:
445 case -NFS4ERR_BADSLOT:
446 case -NFS4ERR_BAD_HIGH_SLOT:
447 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
448 case -NFS4ERR_DEADSESSION:
449 case -NFS4ERR_SEQ_FALSE_RETRY:
450 case -NFS4ERR_SEQ_MISORDERED:
451 dprintk("%s ERROR: %d Reset session\n", __func__,
452 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400453 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500457 if (exception->timeout > HZ) {
458 /* We have retried a decent amount, time to
459 * fail
460 */
461 ret = -EBUSY;
462 break;
463 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400465 nfs_inc_server_stats(server, NFSIOS_DELAY);
466 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400467 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400468 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400469 exception->delay = 1;
470 return 0;
471
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400472 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_OLD_STATEID:
474 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800475 break;
476 case -NFS4ERR_BADOWNER:
477 /* The following works around a Linux server bug! */
478 case -NFS4ERR_BADNAME:
479 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
480 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
481 exception->retry = 1;
482 printk(KERN_WARNING "NFS: v4 server %s "
483 "does not accept raw "
484 "uid/gids. "
485 "Reenabling the idmapper.\n",
486 server->nfs_client->cl_hostname);
487 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500488 }
489 /* We failed to handle the error */
490 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500491wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400492 exception->recovering = 1;
493 return 0;
494}
495
496/* This is the error handling routine for processes that are allowed
497 * to sleep.
498 */
499int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
500{
501 struct nfs_client *clp = server->nfs_client;
502 int ret;
503
504 ret = nfs4_do_handle_exception(server, errorcode, exception);
505 if (exception->delay) {
506 ret = nfs4_delay(server->client, &exception->timeout);
507 goto out_retry;
508 }
509 if (exception->recovering) {
510 ret = nfs4_wait_clnt_recover(clp);
511 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
512 return -EIO;
513 goto out_retry;
514 }
515 return ret;
516out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500520}
521
Trond Myklebust037fc982015-09-20 15:51:00 -0400522static int
523nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
524 int errorcode, struct nfs4_exception *exception)
525{
526 struct nfs_client *clp = server->nfs_client;
527 int ret;
528
529 ret = nfs4_do_handle_exception(server, errorcode, exception);
530 if (exception->delay) {
531 rpc_delay(task, nfs4_update_delay(&exception->timeout));
532 goto out_retry;
533 }
534 if (exception->recovering) {
535 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
536 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
537 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
538 goto out_retry;
539 }
540 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
541 ret = -EIO;
542 return ret;
543out_retry:
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500544 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400545 exception->retry = 1;
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500546 /*
547 * For NFS4ERR_MOVED, the client transport will need to
548 * be recomputed after migration recovery has completed.
549 */
550 if (errorcode == -NFS4ERR_MOVED)
551 rpc_task_release_transport(task);
552 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
585
586 if (flavor == RPC_AUTH_GSS_KRB5I ||
587 flavor == RPC_AUTH_GSS_KRB5P)
588 return true;
589
590 return false;
591}
Trond Myklebust65de8722008-12-23 15:21:44 -0500592
Trond Myklebust452e9352010-07-31 14:29:06 -0400593static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&clp->cl_lock);
596 if (time_before(clp->cl_last_renewal,timestamp))
597 clp->cl_last_renewal = timestamp;
598 spin_unlock(&clp->cl_lock);
599}
600
Trond Myklebust452e9352010-07-31 14:29:06 -0400601static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
602{
Trond Myklebustbe824162015-07-05 14:50:46 -0400603 struct nfs_client *clp = server->nfs_client;
604
605 if (!nfs4_has_session(clp))
606 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400607}
608
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400609struct nfs4_call_sync_data {
610 const struct nfs_server *seq_server;
611 struct nfs4_sequence_args *seq_args;
612 struct nfs4_sequence_res *seq_res;
613};
614
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800615void nfs4_init_sequence(struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400617{
618 args->sa_slot = NULL;
619 args->sa_cache_this = cache_reply;
620 args->sa_privileged = 0;
621
622 res->sr_slot = NULL;
623}
624
625static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
626{
627 args->sa_privileged = 1;
628}
629
Peng Taocb04ad22014-06-11 05:24:15 +0800630int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
631 struct nfs4_sequence_args *args,
632 struct nfs4_sequence_res *res,
633 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400634{
Chuck Lever3bd23842013-08-09 12:49:19 -0400635 struct nfs4_slot *slot;
636
637 /* slot already allocated? */
638 if (res->sr_slot != NULL)
639 goto out_start;
640
641 spin_lock(&tbl->slot_tbl_lock);
642 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
643 goto out_sleep;
644
645 slot = nfs4_alloc_slot(tbl);
646 if (IS_ERR(slot)) {
647 if (slot == ERR_PTR(-ENOMEM))
648 task->tk_timeout = HZ >> 2;
649 goto out_sleep;
650 }
651 spin_unlock(&tbl->slot_tbl_lock);
652
Trond Myklebust0a014a42016-09-22 13:38:51 -0400653 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400654 args->sa_slot = slot;
655 res->sr_slot = slot;
656
657out_start:
658 rpc_call_start(task);
659 return 0;
660
661out_sleep:
662 if (args->sa_privileged)
663 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
664 NULL, RPC_PRIORITY_PRIVILEGED);
665 else
666 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
667 spin_unlock(&tbl->slot_tbl_lock);
668 return -EAGAIN;
669}
Peng Taocb04ad22014-06-11 05:24:15 +0800670EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400671
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400672static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400673{
674 struct nfs4_slot *slot = res->sr_slot;
675 struct nfs4_slot_table *tbl;
676
Chuck Lever3bd23842013-08-09 12:49:19 -0400677 tbl = slot->table;
678 spin_lock(&tbl->slot_tbl_lock);
679 if (!nfs41_wake_and_assign_slot(tbl, slot))
680 nfs4_free_slot(tbl, slot);
681 spin_unlock(&tbl->slot_tbl_lock);
682
683 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400684}
685
686static int nfs40_sequence_done(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
688{
689 if (res->sr_slot != NULL)
690 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400691 return 1;
692}
693
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694#if defined(CONFIG_NFS_V4_1)
695
Trond Myklebustd185a332010-06-16 09:52:25 -0400696static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400699 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500701 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500705
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400706 /* Bump the slot sequence number */
707 if (slot->seq_done)
708 slot->seq_nr++;
709 slot->seq_done = 0;
710
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500711 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500712 /* Be nice to the server: try to ensure that the last transmitted
713 * value for highest_user_slotid <= target_highest_slotid
714 */
715 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
716 send_new_highest_used_slotid = true;
717
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719 send_new_highest_used_slotid = false;
720 goto out_unlock;
721 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500722 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723
724 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
725 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500726out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500727 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400728 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500729 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400730 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400731 if (waitqueue_active(&tbl->slot_waitq))
732 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400733}
734
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400735static int nfs41_sequence_process(struct rpc_task *task,
736 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400737{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500738 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500741 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500742 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400743
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500744 if (slot == NULL)
745 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400746 /* don't increment the sequence number if the task wasn't sent */
747 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400748 goto out;
749
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500750 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500751
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 if (slot->interrupted) {
753 slot->interrupted = 0;
754 interrupted = true;
755 }
756
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400757 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500758 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400759 switch (res->sr_status) {
760 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400761 /* If previous op on slot was interrupted and we reused
762 * the seq# and got a reply from the cache, then retry
763 */
764 if (task->tk_status == -EREMOTEIO && interrupted) {
765 ++slot->seq_nr;
766 goto retry_nowait;
767 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400768 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400769 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500770 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500771 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500772 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400773 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
774 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500775 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500777 case 1:
778 /*
779 * sr_status remains 1 if an RPC level error occurred.
780 * The server may or may not have processed the sequence
781 * operation..
782 * Mark the slot as having hosted an interrupted RPC call.
783 */
784 slot->interrupted = 1;
785 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400786 case -NFS4ERR_DELAY:
787 /* The server detected a resend of the RPC call and
788 * returned NFS4ERR_DELAY as per Section 2.10.6.2
789 * of RFC5661.
790 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500791 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400792 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500793 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500794 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400795 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500796 case -NFS4ERR_BADSLOT:
797 /*
798 * The slot id we used was probably retired. Try again
799 * using a different slot id.
800 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500801 goto retry_nowait;
802 case -NFS4ERR_SEQ_MISORDERED:
803 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500804 * Was the last operation on this sequence interrupted?
805 * If so, retry after bumping the sequence number.
806 */
807 if (interrupted) {
808 ++slot->seq_nr;
809 goto retry_nowait;
810 }
811 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500812 * Could this slot have been previously retired?
813 * If so, then the server may be expecting seq_nr = 1!
814 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500815 if (slot->seq_nr != 1) {
816 slot->seq_nr = 1;
817 goto retry_nowait;
818 }
819 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500820 case -NFS4ERR_SEQ_FALSE_RETRY:
821 ++slot->seq_nr;
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168a2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168a2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001112 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1115 struct iattr attrs;
1116 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001118 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001119 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int cancelled;
1122};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001124static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1125 int err, struct nfs4_exception *exception)
1126{
1127 if (err != -EINVAL)
1128 return false;
1129 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1130 return false;
1131 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1132 exception->retry = 1;
1133 return true;
1134}
1135
Trond Myklebust6ae37332015-01-30 14:21:14 -05001136static u32
1137nfs4_map_atomic_open_share(struct nfs_server *server,
1138 fmode_t fmode, int openflags)
1139{
1140 u32 res = 0;
1141
1142 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1143 case FMODE_READ:
1144 res = NFS4_SHARE_ACCESS_READ;
1145 break;
1146 case FMODE_WRITE:
1147 res = NFS4_SHARE_ACCESS_WRITE;
1148 break;
1149 case FMODE_READ|FMODE_WRITE:
1150 res = NFS4_SHARE_ACCESS_BOTH;
1151 }
1152 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1153 goto out;
1154 /* Want no delegation if we're using O_DIRECT */
1155 if (openflags & O_DIRECT)
1156 res |= NFS4_SHARE_WANT_NO_DELEG;
1157out:
1158 return res;
1159}
1160
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001161static enum open_claim_type4
1162nfs4_map_atomic_open_claim(struct nfs_server *server,
1163 enum open_claim_type4 claim)
1164{
1165 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1166 return claim;
1167 switch (claim) {
1168 default:
1169 return claim;
1170 case NFS4_OPEN_CLAIM_FH:
1171 return NFS4_OPEN_CLAIM_NULL;
1172 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1173 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1174 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1175 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1176 }
1177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180{
1181 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001182 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183 p->o_res.seqid = p->o_arg.seqid;
1184 p->c_res.seqid = p->c_arg.seqid;
1185 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001186 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001188 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189}
1190
Al Viro82a2c1b2011-06-22 18:30:55 -04001191static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001193 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001194 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001195 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001196 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197{
Al Viro82a2c1b2011-06-22 18:30:55 -04001198 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001199 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001201 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 struct nfs4_opendata *p;
1203
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001204 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 if (p == NULL)
1206 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208 p->f_label = nfs4_label_alloc(server, gfp_mask);
1209 if (IS_ERR(p->f_label))
1210 goto err_free_p;
1211
Kinglong Meea49c2692015-07-27 15:31:38 +08001212 p->a_label = nfs4_label_alloc(server, gfp_mask);
1213 if (IS_ERR(p->a_label))
1214 goto err_free_f;
1215
Trond Myklebust63f5f792015-01-23 19:19:25 -05001216 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1217 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001218 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001219 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 nfs_sb_active(dentry->d_sb);
1221 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 p->dir = parent;
1223 p->owner = sp;
1224 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001225 p->o_arg.open_flags = flags;
1226 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001227 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1228 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001229 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1230 * will return permission denied for all bits until close */
1231 if (!(flags & O_EXCL)) {
1232 /* ask server to check for all possible rights as results
1233 * are cached */
1234 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1235 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001236 }
David Howells7539bba2006-08-22 20:06:09 -04001237 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001238 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1239 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001240 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001241 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001242 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001243 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001244 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001245 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1246 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001247 case NFS4_OPEN_CLAIM_NULL:
1248 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1249 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1250 p->o_arg.fh = NFS_FH(dir);
1251 break;
1252 case NFS4_OPEN_CLAIM_PREVIOUS:
1253 case NFS4_OPEN_CLAIM_FH:
1254 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1255 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001256 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001257 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001258 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001259 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001260
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001261 p->o_arg.u.attrs = &p->attrs;
1262 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001263
1264 verf[0] = jiffies;
1265 verf[1] = current->pid;
1266 memcpy(p->o_arg.u.verifier.data, verf,
1267 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001269 p->c_arg.fh = &p->o_res.fh;
1270 p->c_arg.stateid = &p->o_res.stateid;
1271 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001272 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001275
1276err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001277 nfs4_label_free(p->a_label);
1278err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001279 nfs4_label_free(p->f_label);
1280err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001281 kfree(p);
1282err:
1283 dput(parent);
1284 return NULL;
1285}
1286
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289 struct nfs4_opendata *p = container_of(kref,
1290 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001291 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292
1293 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001294 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001295 if (p->state != NULL)
1296 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001298
Kinglong Meea49c2692015-07-27 15:31:38 +08001299 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001300 nfs4_label_free(p->f_label);
1301
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001302 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001303 dput(p->dentry);
1304 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001305 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001306 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307 kfree(p);
1308}
1309
1310static void nfs4_opendata_put(struct nfs4_opendata *p)
1311{
1312 if (p != NULL)
1313 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001314}
1315
Trond Myklebust06f814a2006-01-03 09:55:07 +01001316static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1317{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318 int ret;
1319
Trond Myklebust06f814a2006-01-03 09:55:07 +01001320 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001321 return ret;
1322}
1323
Trond Myklebust24311f82015-09-20 10:50:17 -04001324static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1325 fmode_t fmode)
1326{
1327 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1328 case FMODE_READ|FMODE_WRITE:
1329 return state->n_rdwr != 0;
1330 case FMODE_WRITE:
1331 return state->n_wronly != 0;
1332 case FMODE_READ:
1333 return state->n_rdonly != 0;
1334 }
1335 WARN_ON_ONCE(1);
1336 return false;
1337}
1338
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001340{
1341 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342
Trond Myklebust536e43d2012-01-17 22:04:26 -05001343 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344 goto out;
1345 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001347 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1348 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 break;
1350 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001351 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1352 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001353 break;
1354 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001355 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1356 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001357 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 return ret;
1360}
1361
Trond Myklebust2a606182015-08-19 22:30:00 -05001362static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1363 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001364{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001365 if (delegation == NULL)
1366 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001369 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1370 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001371 switch (claim) {
1372 case NFS4_OPEN_CLAIM_NULL:
1373 case NFS4_OPEN_CLAIM_FH:
1374 break;
1375 case NFS4_OPEN_CLAIM_PREVIOUS:
1376 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1377 break;
1378 default:
1379 return 0;
1380 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001381 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001382 return 1;
1383}
1384
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001385static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001386{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001388 case FMODE_WRITE:
1389 state->n_wronly++;
1390 break;
1391 case FMODE_READ:
1392 state->n_rdonly++;
1393 break;
1394 case FMODE_READ|FMODE_WRITE:
1395 state->n_rdwr++;
1396 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001397 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001398}
1399
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001400#ifdef CONFIG_NFS_V4_1
1401static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1402{
1403 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1404 return true;
1405 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1406 return true;
1407 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1408 return true;
1409 return false;
1410}
1411#endif /* CONFIG_NFS_V4_1 */
1412
Trond Myklebust4f14c192014-02-12 19:15:06 -05001413static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001414{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001415 struct nfs_client *clp = state->owner->so_server->nfs_client;
1416 bool need_recover = false;
1417
1418 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1419 need_recover = true;
1420 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1421 need_recover = true;
1422 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1423 need_recover = true;
1424 if (need_recover)
1425 nfs4_state_mark_reclaim_nograce(clp, state);
1426}
1427
Trond Myklebuste999e802014-02-10 18:20:47 -05001428static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001429 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001430{
1431 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1432 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001433 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001434 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001436 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001438 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1439 return true;
1440 return false;
1441}
1442
Trond Myklebustf95549c2015-01-23 18:06:09 -05001443static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1444{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001445 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1446 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447 if (state->n_wronly)
1448 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1449 if (state->n_rdonly)
1450 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1451 if (state->n_rdwr)
1452 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001453 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001454}
1455
Trond Myklebust226056c2014-02-11 10:41:07 -05001456static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1457 nfs4_stateid *stateid, fmode_t fmode)
1458{
1459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1460 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1461 case FMODE_WRITE:
1462 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1463 break;
1464 case FMODE_READ:
1465 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 break;
1467 case 0:
1468 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 clear_bit(NFS_OPEN_STATE, &state->flags);
1471 }
1472 if (stateid == NULL)
1473 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001474 /* Handle OPEN+OPEN_DOWNGRADE races */
1475 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1476 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001477 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001478 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001479 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001481 nfs4_stateid_copy(&state->stateid, stateid);
1482 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001483}
1484
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001485static void nfs_clear_open_stateid(struct nfs4_state *state,
1486 nfs4_stateid *arg_stateid,
1487 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001488{
1489 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001490 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1491 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1492 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001493 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001494 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1495 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001496}
1497
Trond Myklebust1393d962016-09-22 13:39:13 -04001498static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1499 const nfs4_stateid *stateid, fmode_t fmode,
1500 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001501{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001503 case FMODE_READ:
1504 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1505 break;
1506 case FMODE_WRITE:
1507 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1508 break;
1509 case FMODE_READ|FMODE_WRITE:
1510 set_bit(NFS_O_RDWR_STATE, &state->flags);
1511 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001512 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001513 return;
1514 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1515 nfs4_stateid_copy(&state->stateid, stateid);
1516 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001517}
1518
Trond Myklebust1393d962016-09-22 13:39:13 -04001519static void __update_open_stateid(struct nfs4_state *state,
1520 const nfs4_stateid *open_stateid,
1521 const nfs4_stateid *deleg_stateid,
1522 fmode_t fmode,
1523 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001525 /*
1526 * Protect the call to nfs4_state_set_mode_locked and
1527 * serialise the stateid update
1528 */
Andrew Elble361cad32015-12-02 09:20:57 -05001529 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001530 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001531 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001532 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001533 set_bit(NFS_DELEGATED_STATE, &state->flags);
1534 }
1535 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001536 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001537 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001539 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Trond Myklebust1393d962016-09-22 13:39:13 -04001542static int update_open_stateid(struct nfs4_state *state,
1543 const nfs4_stateid *open_stateid,
1544 const nfs4_stateid *delegation,
1545 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001546{
Trond Myklebust1393d962016-09-22 13:39:13 -04001547 struct nfs_server *server = NFS_SERVER(state->inode);
1548 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001549 struct nfs_inode *nfsi = NFS_I(state->inode);
1550 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001551 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001552 int ret = 0;
1553
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001554 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001555
1556 rcu_read_lock();
1557 deleg_cur = rcu_dereference(nfsi->delegation);
1558 if (deleg_cur == NULL)
1559 goto no_delegation;
1560
1561 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001562 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001563 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001564 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001565 goto no_delegation_unlock;
1566
1567 if (delegation == NULL)
1568 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001569 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001570 goto no_delegation_unlock;
1571
Trond Myklebustb7391f42008-12-23 15:21:52 -05001572 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001573 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1574 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001575 ret = 1;
1576no_delegation_unlock:
1577 spin_unlock(&deleg_cur->lock);
1578no_delegation:
1579 rcu_read_unlock();
1580
1581 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001582 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001583 ret = 1;
1584 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001585 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001586 nfs4_schedule_state_manager(clp);
1587 if (freeme.type != 0)
1588 nfs4_test_and_free_stateid(server, &freeme,
1589 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001590
1591 return ret;
1592}
1593
Trond Myklebust39071e62015-01-24 15:07:56 -05001594static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1595 const nfs4_stateid *stateid)
1596{
1597 struct nfs4_state *state = lsp->ls_state;
1598 bool ret = false;
1599
1600 spin_lock(&state->state_lock);
1601 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1602 goto out_noupdate;
1603 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1604 goto out_noupdate;
1605 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1606 ret = true;
1607out_noupdate:
1608 spin_unlock(&state->state_lock);
1609 return ret;
1610}
Trond Myklebust34310432008-12-23 15:21:38 -05001611
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001612static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001613{
1614 struct nfs_delegation *delegation;
1615
1616 rcu_read_lock();
1617 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001618 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001619 rcu_read_unlock();
1620 return;
1621 }
1622 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001623 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624}
1625
Trond Myklebust6ee41262007-07-08 14:11:36 -04001626static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627{
1628 struct nfs4_state *state = opendata->state;
1629 struct nfs_inode *nfsi = NFS_I(state->inode);
1630 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001631 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001632 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001633 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001634 nfs4_stateid stateid;
1635 int ret = -EAGAIN;
1636
Trond Myklebustaac00a82007-07-05 19:02:21 -04001637 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001638 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001639 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001640 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001641 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001642 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001643 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001644 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001645 rcu_read_lock();
1646 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001647 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001648 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001649 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001650 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001652 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001654 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001655 if (!opendata->is_recover) {
1656 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1657 if (ret != 0)
1658 goto out;
1659 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001660 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001661
1662 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001663 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001664 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001665 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001666out:
1667 return ERR_PTR(ret);
1668out_return_state:
1669 atomic_inc(&state->count);
1670 return state;
1671}
1672
Andy Adamsone23008e2012-10-02 21:07:32 -04001673static void
1674nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1675{
1676 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1677 struct nfs_delegation *delegation;
1678 int delegation_flags = 0;
1679
1680 rcu_read_lock();
1681 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1682 if (delegation)
1683 delegation_flags = delegation->flags;
1684 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001685 switch (data->o_arg.claim) {
1686 default:
1687 break;
1688 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1689 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001690 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1691 "returning a delegation for "
1692 "OPEN(CLAIM_DELEGATE_CUR)\n",
1693 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001694 return;
1695 }
1696 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001697 nfs_inode_set_delegation(state->inode,
1698 data->owner->so_cred,
1699 &data->o_res);
1700 else
1701 nfs_inode_reclaim_delegation(state->inode,
1702 data->owner->so_cred,
1703 &data->o_res);
1704}
1705
1706/*
1707 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1708 * and update the nfs4_state.
1709 */
1710static struct nfs4_state *
1711_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1712{
1713 struct inode *inode = data->state->inode;
1714 struct nfs4_state *state = data->state;
1715 int ret;
1716
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001717 if (!data->rpc_done) {
1718 if (data->rpc_status) {
1719 ret = data->rpc_status;
1720 goto err;
1721 }
1722 /* cached opens have already been processed */
1723 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001724 }
1725
Andy Adamsone23008e2012-10-02 21:07:32 -04001726 ret = nfs_refresh_inode(inode, &data->f_attr);
1727 if (ret)
1728 goto err;
1729
1730 if (data->o_res.delegation_type != 0)
1731 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001732update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001733 update_open_stateid(state, &data->o_res.stateid, NULL,
1734 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001735 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001736
1737 return state;
1738err:
1739 return ERR_PTR(ret);
1740
1741}
1742
1743static struct nfs4_state *
1744_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001745{
1746 struct inode *inode;
1747 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001748 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001749
Trond Myklebustaac00a82007-07-05 19:02:21 -04001750 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001751 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001752 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001753 goto out;
1754 }
1755
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001756 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001758 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001759 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001761 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001762 goto err;
1763 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764 state = nfs4_get_open_state(inode, data->owner);
1765 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001766 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001767 if (data->o_res.delegation_type != 0)
1768 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001769 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001770 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001772out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001773 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001775err_put_inode:
1776 iput(inode);
1777err:
1778 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779}
1780
Andy Adamsone23008e2012-10-02 21:07:32 -04001781static struct nfs4_state *
1782nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1783{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001784 struct nfs4_state *ret;
1785
Andy Adamsone23008e2012-10-02 21:07:32 -04001786 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001787 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1788 else
1789 ret = _nfs4_opendata_to_nfs4_state(data);
1790 nfs4_sequence_free_slot(&data->o_res.seq_res);
1791 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001792}
1793
Trond Myklebust864472e2006-01-03 09:55:15 +01001794static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1795{
1796 struct nfs_inode *nfsi = NFS_I(state->inode);
1797 struct nfs_open_context *ctx;
1798
1799 spin_lock(&state->inode->i_lock);
1800 list_for_each_entry(ctx, &nfsi->open_files, list) {
1801 if (ctx->state != state)
1802 continue;
1803 get_nfs_open_context(ctx);
1804 spin_unlock(&state->inode->i_lock);
1805 return ctx;
1806 }
1807 spin_unlock(&state->inode->i_lock);
1808 return ERR_PTR(-ENOENT);
1809}
1810
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001811static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1812 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001813{
1814 struct nfs4_opendata *opendata;
1815
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001816 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001817 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001818 if (opendata == NULL)
1819 return ERR_PTR(-ENOMEM);
1820 opendata->state = state;
1821 atomic_inc(&state->count);
1822 return opendata;
1823}
1824
Trond Myklebust24311f82015-09-20 10:50:17 -04001825static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1826 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001827{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001828 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001829 int ret;
1830
Trond Myklebust24311f82015-09-20 10:50:17 -04001831 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001832 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001833 opendata->o_arg.open_flags = 0;
1834 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001835 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1836 NFS_SB(opendata->dentry->d_sb),
1837 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001838 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1839 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1840 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001841 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001842 if (ret != 0)
1843 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001844 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001845 if (IS_ERR(newstate))
1846 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001847 if (newstate != opendata->state)
1848 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001849 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001850 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001851}
1852
1853static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1854{
Trond Myklebust864472e2006-01-03 09:55:15 +01001855 int ret;
1856
Trond Myklebust4f14c192014-02-12 19:15:06 -05001857 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1858 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1859 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1860 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001861 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001862 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001863 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001864 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001865 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1866 if (ret != 0)
1867 return ret;
1868 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1869 if (ret != 0)
1870 return ret;
1871 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1872 if (ret != 0)
1873 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001874 /*
1875 * We may have performed cached opens for all three recoveries.
1876 * Check if we need to update the current stateid.
1877 */
1878 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001879 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001880 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001881 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001882 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001883 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001884 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 return 0;
1886}
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888/*
1889 * OPEN_RECLAIM:
1890 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001892static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001894 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001895 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001896 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int status;
1898
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001899 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1900 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001901 if (IS_ERR(opendata))
1902 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001903 rcu_read_lock();
1904 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001905 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001906 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001907 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001908 opendata->o_arg.u.delegation_type = delegation_type;
1909 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001910 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return status;
1912}
1913
Trond Myklebust539cd032007-06-05 11:46:42 -04001914static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 struct nfs_server *server = NFS_SERVER(state->inode);
1917 struct nfs4_exception exception = { };
1918 int err;
1919 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001920 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001921 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001922 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1923 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001924 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001925 break;
1926 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 } while (exception.retry);
1928 return err;
1929}
1930
Trond Myklebust864472e2006-01-03 09:55:15 +01001931static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1932{
1933 struct nfs_open_context *ctx;
1934 int ret;
1935
1936 ctx = nfs4_state_find_open_context(state);
1937 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001938 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001939 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001940 put_nfs_open_context(ctx);
1941 return ret;
1942}
1943
NeilBrown70f254a2017-12-13 09:57:09 +11001944static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001946 switch (err) {
1947 default:
1948 printk(KERN_ERR "NFS: %s: unhandled error "
1949 "%d.\n", __func__, err);
1950 case 0:
1951 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001952 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001953 case -ESTALE:
1954 break;
1955 case -NFS4ERR_BADSESSION:
1956 case -NFS4ERR_BADSLOT:
1957 case -NFS4ERR_BAD_HIGH_SLOT:
1958 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1959 case -NFS4ERR_DEADSESSION:
1960 set_bit(NFS_DELEGATED_STATE, &state->flags);
1961 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1962 return -EAGAIN;
1963 case -NFS4ERR_STALE_CLIENTID:
1964 case -NFS4ERR_STALE_STATEID:
1965 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001966 /* Don't recall a delegation if it was lost */
1967 nfs4_schedule_lease_recovery(server->nfs_client);
1968 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001969 case -NFS4ERR_MOVED:
1970 nfs4_schedule_migration_recovery(server);
1971 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001972 case -NFS4ERR_LEASE_MOVED:
1973 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1974 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001975 case -NFS4ERR_DELEG_REVOKED:
1976 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001977 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001978 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001979 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001980 nfs_inode_find_state_and_recover(state->inode,
1981 stateid);
1982 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001983 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001984 case -NFS4ERR_DELAY:
1985 case -NFS4ERR_GRACE:
1986 set_bit(NFS_DELEGATED_STATE, &state->flags);
1987 ssleep(1);
1988 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001989 case -ENOMEM:
1990 case -NFS4ERR_DENIED:
NeilBrown70f254a2017-12-13 09:57:09 +11001991 if (fl) {
1992 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
1993 if (lsp)
1994 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1995 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001996 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return err;
1999}
2000
Trond Myklebust24311f82015-09-20 10:50:17 -04002001int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2002 struct nfs4_state *state, const nfs4_stateid *stateid,
2003 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002004{
2005 struct nfs_server *server = NFS_SERVER(state->inode);
2006 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002007 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002008
2009 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2010 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2011 if (IS_ERR(opendata))
2012 return PTR_ERR(opendata);
2013 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002014 write_seqlock(&state->seqlock);
2015 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2016 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002017 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2018 switch (type & (FMODE_READ|FMODE_WRITE)) {
2019 case FMODE_READ|FMODE_WRITE:
2020 case FMODE_WRITE:
2021 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2022 if (err)
2023 break;
2024 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2025 if (err)
2026 break;
2027 case FMODE_READ:
2028 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2029 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002030 nfs4_opendata_put(opendata);
NeilBrown70f254a2017-12-13 09:57:09 +11002031 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002032}
2033
Chuck Leverbe05c862013-08-09 12:49:47 -04002034static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2035{
2036 struct nfs4_opendata *data = calldata;
2037
Peng Taocb04ad22014-06-11 05:24:15 +08002038 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2039 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002040}
2041
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002042static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2043{
2044 struct nfs4_opendata *data = calldata;
2045
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002046 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002047
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002048 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002049 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002050 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002051 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002052 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002053 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002054 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002055}
2056
2057static void nfs4_open_confirm_release(void *calldata)
2058{
2059 struct nfs4_opendata *data = calldata;
2060 struct nfs4_state *state = NULL;
2061
2062 /* If this request hasn't been cancelled, do nothing */
2063 if (data->cancelled == 0)
2064 goto out_free;
2065 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002066 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002067 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002068 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002069 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002070 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002071out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002072 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073}
2074
2075static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002076 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002077 .rpc_call_done = nfs4_open_confirm_done,
2078 .rpc_release = nfs4_open_confirm_release,
2079};
2080
2081/*
2082 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2083 */
2084static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2085{
David Howells2b0143b2015-03-17 22:25:59 +00002086 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002087 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002088 struct rpc_message msg = {
2089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2090 .rpc_argp = &data->c_arg,
2091 .rpc_resp = &data->c_res,
2092 .rpc_cred = data->owner->so_cred,
2093 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002094 struct rpc_task_setup task_setup_data = {
2095 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002096 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002097 .callback_ops = &nfs4_open_confirm_ops,
2098 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002099 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002100 .flags = RPC_TASK_ASYNC,
2101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 int status;
2103
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002104 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002105 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002106 data->rpc_done = 0;
2107 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002108 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002109 if (data->is_recover)
2110 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002111 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002112 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002113 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002114 status = nfs4_wait_for_completion_rpc_task(task);
2115 if (status != 0) {
2116 data->cancelled = 1;
2117 smp_wmb();
2118 } else
2119 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002120 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return status;
2122}
2123
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002126 struct nfs4_opendata *data = calldata;
2127 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002128 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002129 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002130
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002132 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002133 /*
2134 * Check if we still need to send an OPEN call, or if we can use
2135 * a delegation instead.
2136 */
2137 if (data->state != NULL) {
2138 struct nfs_delegation *delegation;
2139
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002140 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002141 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002142 rcu_read_lock();
2143 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002144 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002145 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002146 rcu_read_unlock();
2147 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002148 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002149 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002150 switch (claim) {
2151 default:
2152 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002153 case NFS4_OPEN_CLAIM_PREVIOUS:
2154 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2155 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002156 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002157 case NFS4_OPEN_CLAIM_FH:
2158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002159 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2160 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002161 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002162 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002163 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002164 &data->o_res.seq_res,
2165 task) != 0)
2166 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002167
2168 /* Set the create mode (note dependency on the session type) */
2169 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2170 if (data->o_arg.open_flags & O_EXCL) {
2171 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2172 if (nfs4_has_persistent_session(clp))
2173 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2174 else if (clp->cl_mvops->minor_version > 0)
2175 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2176 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002177 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002178unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002179 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002180 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002181out_no_action:
2182 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002183out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002184 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002185}
2186
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187static void nfs4_open_done(struct rpc_task *task, void *calldata)
2188{
2189 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002192
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002193 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002194 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002195
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002196 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002197 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2198 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002199 case S_IFREG:
2200 break;
2201 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 break;
2204 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002206 break;
2207 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002209 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002211 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002212 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2213 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002214 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002215 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002216}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002217
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218static void nfs4_open_release(void *calldata)
2219{
2220 struct nfs4_opendata *data = calldata;
2221 struct nfs4_state *state = NULL;
2222
2223 /* If this request hasn't been cancelled, do nothing */
2224 if (data->cancelled == 0)
2225 goto out_free;
2226 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002227 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228 goto out_free;
2229 /* In case we need an open_confirm, no cleanup! */
2230 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2231 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002233 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002234 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002235out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002236 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237}
2238
2239static const struct rpc_call_ops nfs4_open_ops = {
2240 .rpc_call_prepare = nfs4_open_prepare,
2241 .rpc_call_done = nfs4_open_done,
2242 .rpc_release = nfs4_open_release,
2243};
2244
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002245static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002246{
David Howells2b0143b2015-03-17 22:25:59 +00002247 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248 struct nfs_server *server = NFS_SERVER(dir);
2249 struct nfs_openargs *o_arg = &data->o_arg;
2250 struct nfs_openres *o_res = &data->o_res;
2251 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002252 struct rpc_message msg = {
2253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2254 .rpc_argp = o_arg,
2255 .rpc_resp = o_res,
2256 .rpc_cred = data->owner->so_cred,
2257 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 struct rpc_task_setup task_setup_data = {
2259 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002260 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002261 .callback_ops = &nfs4_open_ops,
2262 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002263 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002264 .flags = RPC_TASK_ASYNC,
2265 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266 int status;
2267
Chuck Levera9c92d62013-08-09 12:48:18 -04002268 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002269 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002270 data->rpc_done = 0;
2271 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002272 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002273 data->is_recover = 0;
2274 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002275 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002276 data->is_recover = 1;
2277 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002278 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002279 if (IS_ERR(task))
2280 return PTR_ERR(task);
2281 status = nfs4_wait_for_completion_rpc_task(task);
2282 if (status != 0) {
2283 data->cancelled = 1;
2284 smp_wmb();
2285 } else
2286 status = data->rpc_status;
2287 rpc_put_task(task);
2288
2289 return status;
2290}
2291
2292static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2293{
David Howells2b0143b2015-03-17 22:25:59 +00002294 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002295 struct nfs_openres *o_res = &data->o_res;
2296 int status;
2297
2298 status = nfs4_run_open_task(data, 1);
2299 if (status != 0 || !data->rpc_done)
2300 return status;
2301
Trond Myklebust6926afd2012-01-07 13:22:46 -05002302 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2303
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002304 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2305 status = _nfs4_proc_open_confirm(data);
2306 if (status != 0)
2307 return status;
2308 }
2309
2310 return status;
2311}
2312
Trond Myklebustf3792d62014-07-10 08:54:32 -04002313/*
2314 * Additional permission checks in order to distinguish between an
2315 * open for read, and an open for execute. This works around the
2316 * fact that NFSv4 OPEN treats read and execute permissions as being
2317 * the same.
2318 * Note that in the non-execute case, we want to turn off permission
2319 * checking if we just created a new file (POSIX open() semantics).
2320 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002321static int nfs4_opendata_access(struct rpc_cred *cred,
2322 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002323 struct nfs4_state *state, fmode_t fmode,
2324 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002325{
2326 struct nfs_access_entry cache;
2327 u32 mask;
2328
2329 /* access call failed or for some reason the server doesn't
2330 * support any access modes -- defer access call until later */
2331 if (opendata->o_res.access_supported == 0)
2332 return 0;
2333
2334 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002335 /*
2336 * Use openflags to check for exec, because fmode won't
2337 * always have FMODE_EXEC set when file open for exec.
2338 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002339 if (openflags & __FMODE_EXEC) {
2340 /* ONLY check for exec rights */
2341 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002342 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002343 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002344
2345 cache.cred = cred;
2346 cache.jiffies = jiffies;
2347 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2348 nfs_access_add_cache(state->inode, &cache);
2349
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002350 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002351 return 0;
2352
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002353 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002354}
2355
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002356/*
2357 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2358 */
2359static int _nfs4_proc_open(struct nfs4_opendata *data)
2360{
David Howells2b0143b2015-03-17 22:25:59 +00002361 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002362 struct nfs_server *server = NFS_SERVER(dir);
2363 struct nfs_openargs *o_arg = &data->o_arg;
2364 struct nfs_openres *o_res = &data->o_res;
2365 int status;
2366
2367 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002368 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002369 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002370 if (status != 0) {
2371 if (status == -NFS4ERR_BADNAME &&
2372 !(o_arg->open_flags & O_CREAT))
2373 return -ENOENT;
2374 return status;
2375 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002376
Trond Myklebust6926afd2012-01-07 13:22:46 -05002377 nfs_fattr_map_and_free_names(server, &data->f_attr);
2378
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002379 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002380 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002381 if (o_arg->open_flags & O_EXCL)
2382 data->file_created = 1;
2383 else if (o_res->cinfo.before != o_res->cinfo.after)
2384 data->file_created = 1;
2385 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002386 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2387 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002389 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002391 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002393 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2394 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002395 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002396 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Andy Adamsond83217c2011-03-01 01:34:17 +00002400static int nfs4_recover_expired_lease(struct nfs_server *server)
2401{
2402 return nfs4_client_recover_expired_lease(server->nfs_client);
2403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405/*
2406 * OPEN_EXPIRED:
2407 * reclaim state on the server after a network partition.
2408 * Assumes caller holds the appropriate lock
2409 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002410static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002412 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002413 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002415 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002416 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002417 if (IS_ERR(opendata))
2418 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002419 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002420 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002421 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002422 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002426static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002427{
Trond Myklebust539cd032007-06-05 11:46:42 -04002428 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002429 struct nfs4_exception exception = { };
2430 int err;
2431
2432 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002433 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002434 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002435 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2436 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002437 switch (err) {
2438 default:
2439 goto out;
2440 case -NFS4ERR_GRACE:
2441 case -NFS4ERR_DELAY:
2442 nfs4_handle_exception(server, err, &exception);
2443 err = 0;
2444 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002445 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002446out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002447 return err;
2448}
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002453 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Trond Myklebust864472e2006-01-03 09:55:15 +01002455 ctx = nfs4_state_find_open_context(state);
2456 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002457 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002458 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002459 put_nfs_open_context(ctx);
2460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Trond Myklebust41020b62016-09-22 13:38:58 -04002463static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2464 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002465{
Trond Myklebust41020b62016-09-22 13:38:58 -04002466 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002467 write_seqlock(&state->seqlock);
2468 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2469 write_sequnlock(&state->seqlock);
2470 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2471}
2472
2473static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2474{
2475 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002476 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002477}
2478
2479static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2480{
2481 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2482 nfs40_clear_delegation_stateid(state);
2483 return nfs4_open_expired(sp, state);
2484}
2485
Trond Myklebust45870d62016-09-22 13:38:59 -04002486static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2487 nfs4_stateid *stateid,
2488 struct rpc_cred *cred)
2489{
2490 return -NFS4ERR_BAD_STATEID;
2491}
2492
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002493#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002494static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2495 nfs4_stateid *stateid,
2496 struct rpc_cred *cred)
2497{
2498 int status;
2499
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002500 switch (stateid->type) {
2501 default:
2502 break;
2503 case NFS4_INVALID_STATEID_TYPE:
2504 case NFS4_SPECIAL_STATEID_TYPE:
2505 return -NFS4ERR_BAD_STATEID;
2506 case NFS4_REVOKED_STATEID_TYPE:
2507 goto out_free;
2508 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002509
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002510 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002511 switch (status) {
2512 case -NFS4ERR_EXPIRED:
2513 case -NFS4ERR_ADMIN_REVOKED:
2514 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002515 break;
2516 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002517 return status;
2518 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002519out_free:
2520 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002521 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002522 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002523}
2524
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002525static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002526{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002527 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002528 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002529 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002530 struct rpc_cred *cred;
2531 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002532
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 /* Get the delegation credential for use by test/free_stateid */
2534 rcu_read_lock();
2535 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002536 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002537 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002538 return;
2539 }
2540
2541 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia461bbb92017-03-30 13:49:03 -04002542 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2543 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2544 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002545 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002546 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002547 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002548 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002549
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002550 cred = get_rpccred(delegation->cred);
2551 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002552 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002553 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002554 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002555 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002556
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002557 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002558}
2559
2560/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002561 * nfs41_check_expired_locks - possibly free a lock stateid
2562 *
2563 * @state: NFSv4 state for an inode
2564 *
2565 * Returns NFS_OK if recovery for this stateid is now finished.
2566 * Otherwise a negative NFS4ERR value is returned.
2567 */
2568static int nfs41_check_expired_locks(struct nfs4_state *state)
2569{
2570 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002571 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002572 struct nfs_server *server = NFS_SERVER(state->inode);
2573
2574 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2575 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002576
2577 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002578 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2579 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2580 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2581
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002582 atomic_inc(&lsp->ls_count);
2583 spin_unlock(&state->state_lock);
2584
2585 nfs4_put_lock_state(prev);
2586 prev = lsp;
2587
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002588 status = nfs41_test_and_free_expired_stateid(server,
2589 &lsp->ls_stateid,
2590 cred);
2591 trace_nfs4_test_lock_stateid(state, lsp, status);
2592 if (status == -NFS4ERR_EXPIRED ||
2593 status == -NFS4ERR_BAD_STATEID) {
2594 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002595 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002596 if (!recover_lost_locks)
2597 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2598 } else if (status != NFS_OK) {
2599 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002600 nfs4_put_lock_state(prev);
2601 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002602 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002603 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002604 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002605 }
2606 spin_unlock(&state->state_lock);
2607 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002608out:
2609 return ret;
2610}
2611
2612/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002613 * nfs41_check_open_stateid - possibly free an open stateid
2614 *
2615 * @state: NFSv4 state for an inode
2616 *
2617 * Returns NFS_OK if recovery for this stateid is now finished.
2618 * Otherwise a negative NFS4ERR value is returned.
2619 */
2620static int nfs41_check_open_stateid(struct nfs4_state *state)
2621{
2622 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002623 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002624 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625 int status;
2626
Trond Myklebustb134fc42016-09-22 13:39:16 -04002627 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002628 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2629 if (nfs4_have_delegation(state->inode, state->state))
2630 return NFS_OK;
2631 return -NFS4ERR_OPENMODE;
2632 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002633 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002634 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002635 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002636 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002637 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002638 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2639 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2640 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002641 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002642 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002643 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002644 if (status != NFS_OK)
2645 return status;
2646 if (nfs_open_stateid_recover_openmode(state))
2647 return -NFS4ERR_OPENMODE;
2648 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002649}
2650
2651static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2652{
Chuck Levereb64cf92012-07-11 16:30:05 -04002653 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002654
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002655 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002656 status = nfs41_check_expired_locks(state);
2657 if (status != NFS_OK)
2658 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002659 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002660 if (status != NFS_OK)
2661 status = nfs4_open_expired(sp, state);
2662 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002663}
2664#endif
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002667 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2668 * fields corresponding to attributes that were used to store the verifier.
2669 * Make sure we clobber those fields in the later setattr call
2670 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002671static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2672 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002673{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002674 const u32 *attrset = opendata->o_res.attrset;
2675
2676 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002677 !(sattr->ia_valid & ATTR_ATIME_SET))
2678 sattr->ia_valid |= ATTR_ATIME;
2679
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002680 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002681 !(sattr->ia_valid & ATTR_MTIME_SET))
2682 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002683
2684 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddington56379492017-01-24 11:34:20 -05002685 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2686 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002687 sattr->ia_valid &= ~ATTR_MODE;
2688
2689 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2690 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002691}
2692
Trond Myklebustc21443c2013-02-07 14:26:21 -05002693static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2694 fmode_t fmode,
2695 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002696 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002697{
2698 struct nfs4_state_owner *sp = opendata->owner;
2699 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002700 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002701 struct nfs4_state *state;
2702 unsigned int seq;
2703 int ret;
2704
2705 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2706
2707 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002708 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002709 goto out;
2710
Trond Myklebustb05c4602018-05-22 11:17:16 -04002711 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002712 ret = PTR_ERR(state);
2713 if (IS_ERR(state))
2714 goto out;
Trond Myklebust04653392017-02-08 11:29:46 -05002715 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002716 if (server->caps & NFS_CAP_POSIX_LOCK)
2717 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002718 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2719 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002720
Trond Myklebust275bb302013-05-29 13:11:28 -04002721 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002722 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002723 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002724 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002725 alias = d_exact_alias(dentry, state->inode);
2726 if (!alias)
2727 alias = d_splice_alias(igrab(state->inode), dentry);
2728 /* d_splice_alias() can't fail here - it's a non-directory */
2729 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002730 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002731 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002732 }
2733 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002734 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002735 }
2736
Trond Myklebustc21443c2013-02-07 14:26:21 -05002737 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2738 if (ret != 0)
2739 goto out;
2740
David Howells2b0143b2015-03-17 22:25:59 +00002741 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002742 nfs_inode_attach_open_context(ctx);
2743 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2744 nfs4_schedule_stateid_recovery(server, state);
2745 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002746out:
Trond Myklebustb05c4602018-05-22 11:17:16 -04002747 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002748 return ret;
2749}
2750
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002751/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002752 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 */
Andy Adamson82be4172012-05-23 05:02:35 -04002754static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002755 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002756 int flags,
2757 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002758 struct nfs4_label *label,
2759 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 struct nfs4_state_owner *sp;
2762 struct nfs4_state *state = NULL;
2763 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002764 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002765 struct dentry *dentry = ctx->dentry;
2766 struct rpc_cred *cred = ctx->cred;
2767 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2768 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002769 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002770 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002771 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002775 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2776 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2778 goto out_err;
2779 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002780 status = nfs4_recover_expired_lease(server);
2781 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002782 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002783 if (d_really_is_positive(dentry))
2784 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002785 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002786 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002787 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002788 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002789 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002790 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002791 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
David Quigley14c43f72013-05-22 12:50:43 -04002793 if (label) {
2794 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2795 if (IS_ERR(olabel)) {
2796 status = PTR_ERR(olabel);
2797 goto err_opendata_put;
2798 }
2799 }
2800
Trond Myklebuste911b812014-03-26 13:24:37 -07002801 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2802 if (!opendata->f_attr.mdsthreshold) {
2803 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2804 if (!opendata->f_attr.mdsthreshold)
2805 goto err_free_label;
2806 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002807 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002808 }
David Howells2b0143b2015-03-17 22:25:59 +00002809 if (d_really_is_positive(dentry))
2810 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002811
Trond Myklebust3efb9722013-05-29 13:17:04 -04002812 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002813 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002814 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002815 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002816
NeilBrownefcbc042015-07-30 13:00:56 +10002817 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002818 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002819 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002820 /*
2821 * send create attributes which was not set by open
2822 * with an extra setattr.
2823 */
2824 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2825 nfs_fattr_init(opendata->o_res.f_attr);
2826 status = nfs4_do_setattr(state->inode, cred,
2827 opendata->o_res.f_attr, sattr,
2828 state, label, olabel);
2829 if (status == 0) {
2830 nfs_setattr_update_inode(state->inode, sattr,
2831 opendata->o_res.f_attr);
2832 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2833 }
David Quigley1775fd32013-05-22 12:50:42 -04002834 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002835 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002836 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002837 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002838
Trond Myklebuste911b812014-03-26 13:24:37 -07002839 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002840 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002841 opendata->f_attr.mdsthreshold = NULL;
2842 }
Andy Adamson82be4172012-05-23 05:02:35 -04002843
David Quigley14c43f72013-05-22 12:50:43 -04002844 nfs4_label_free(olabel);
2845
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002846 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002849err_free_label:
2850 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002851err_opendata_put:
2852 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002853err_put_state_owner:
2854 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return status;
2857}
2858
2859
Andy Adamson82be4172012-05-23 05:02:35 -04002860static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002861 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002862 int flags,
2863 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002864 struct nfs4_label *label,
2865 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002867 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 struct nfs4_exception exception = { };
2869 struct nfs4_state *res;
2870 int status;
2871
2872 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002873 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002874 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002875 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (status == 0)
2877 break;
2878 /* NOTE: BAD_SEQID means the server and client disagree about the
2879 * book-keeping w.r.t. state-changing operations
2880 * (OPEN/CLOSE/LOCK/LOCKU...)
2881 * It is actually a sign of a bug on the client or on the server.
2882 *
2883 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002884 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 * have unhashed the old state_owner for us, and that we can
2886 * therefore safely retry using a new one. We should still warn
2887 * the user though...
2888 */
2889 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002890 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002891 " returned a bad sequence-id error!\n",
2892 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 exception.retry = 1;
2894 continue;
2895 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002896 /*
2897 * BAD_STATEID on OPEN means that the server cancelled our
2898 * state before it received the OPEN_CONFIRM.
2899 * Recover by retrying the request as per the discussion
2900 * on Page 181 of RFC3530.
2901 */
2902 if (status == -NFS4ERR_BAD_STATEID) {
2903 exception.retry = 1;
2904 continue;
2905 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002906 if (status == -EAGAIN) {
2907 /* We must have found a delegation */
2908 exception.retry = 1;
2909 continue;
2910 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002911 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2912 continue;
2913 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 status, &exception));
2915 } while (exception.retry);
2916 return res;
2917}
2918
Trond Myklebust8487c472016-06-26 08:44:35 -04002919static int _nfs4_do_setattr(struct inode *inode,
2920 struct nfs_setattrargs *arg,
2921 struct nfs_setattrres *res,
2922 struct rpc_cred *cred,
2923 struct nfs4_state *state)
2924{
2925 struct nfs_server *server = NFS_SERVER(inode);
2926 struct rpc_message msg = {
2927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2928 .rpc_argp = arg,
2929 .rpc_resp = res,
2930 .rpc_cred = cred,
2931 };
2932 struct rpc_cred *delegation_cred = NULL;
2933 unsigned long timestamp = jiffies;
2934 fmode_t fmode;
2935 bool truncate;
2936 int status;
2937
2938 nfs_fattr_init(res->fattr);
2939
2940 /* Servers should only apply open mode checks for file size changes */
2941 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2942 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2943
2944 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2945 /* Use that stateid */
2946 } else if (truncate && state != NULL) {
2947 struct nfs_lockowner lockowner = {
2948 .l_owner = current->files,
2949 .l_pid = current->tgid,
2950 };
2951 if (!nfs4_valid_open_stateid(state))
2952 return -EBADF;
2953 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2954 &arg->stateid, &delegation_cred) == -EIO)
2955 return -EBADF;
2956 } else
2957 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2958 if (delegation_cred)
2959 msg.rpc_cred = delegation_cred;
2960
2961 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2962
2963 put_rpccred(delegation_cred);
2964 if (status == 0 && state != NULL)
2965 renew_lease(server, timestamp);
2966 trace_nfs4_setattr(inode, &arg->stateid, status);
2967 return status;
2968}
2969
2970static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2971 struct nfs_fattr *fattr, struct iattr *sattr,
2972 struct nfs4_state *state, struct nfs4_label *ilabel,
2973 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002975 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002977 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 .iap = sattr,
2979 .server = server,
2980 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002981 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 };
2983 struct nfs_setattrres res = {
2984 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002985 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 .server = server,
2987 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002988 struct nfs4_exception exception = {
2989 .state = state,
2990 .inode = inode,
2991 .stateid = &arg.stateid,
2992 };
2993 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
David Quigleyaa9c2662013-05-22 12:50:44 -04002995 arg.bitmask = nfs4_bitmask(server, ilabel);
2996 if (ilabel)
2997 arg.bitmask = nfs4_bitmask(server, olabel);
2998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003000 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003001 switch (err) {
3002 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003003 if (!(sattr->ia_valid & ATTR_SIZE)) {
3004 pr_warn_once("NFSv4: server %s is incorrectly "
3005 "applying open mode checks to "
3006 "a SETATTR that is not "
3007 "changing file size.\n",
3008 server->nfs_client->cl_hostname);
3009 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003010 if (state && !(state->state & FMODE_WRITE)) {
3011 err = -EBADF;
3012 if (sattr->ia_valid & ATTR_OPEN)
3013 err = -EACCES;
3014 goto out;
3015 }
3016 }
3017 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003019out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return err;
3021}
3022
Peng Tao500d7012015-09-22 11:35:22 +08003023static bool
3024nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3025{
3026 if (inode == NULL || !nfs_have_layout(inode))
3027 return false;
3028
3029 return pnfs_wait_on_layoutreturn(inode, task);
3030}
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032struct nfs4_closedata {
3033 struct inode *inode;
3034 struct nfs4_state *state;
3035 struct nfs_closeargs arg;
3036 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003037 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003038 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003039 bool roc;
3040 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041};
3042
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003043static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003044{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003045 struct nfs4_closedata *calldata = data;
3046 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003047 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003048
Fred Isamanf7e89172011-01-06 11:36:32 +00003049 if (calldata->roc)
3050 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003051 nfs4_put_open_state(calldata->state);
3052 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003053 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003054 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003055 kfree(calldata);
3056}
3057
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003058static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003060 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003063 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Chuck Levera3ca5652012-03-01 17:00:40 -05003065 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003066 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3067 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003068 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* hmm. we are done with the inode, and in the process of freeing
3070 * the state_owner. we keep this around to process errors
3071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 switch (task->tk_status) {
3073 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003074 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003075 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003076 pnfs_roc_set_barrier(state->inode,
3077 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003078 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003079 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003080 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003082 case -NFS4ERR_EXPIRED:
3083 nfs4_free_revoked_stateid(server,
3084 &calldata->arg.stateid,
3085 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003086 case -NFS4ERR_OLD_STATEID:
3087 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003088 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003089 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003090 rpc_restart_call_prepare(task);
3091 goto out_release;
3092 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003093 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003096 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003097 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003098 goto out_release;
3099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003101 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3102 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003103out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003104 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003105 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003106 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003109static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003111 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003112 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003113 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003114 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003115 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003116
Chuck Levera3ca5652012-03-01 17:00:40 -05003117 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003118 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003119 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003120
Trond Myklebust88069f72009-12-08 08:33:16 -05003121 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003122 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003123 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3124 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3125 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003126 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003127 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003128 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003129 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003130 if (state->n_rdonly == 0)
3131 call_close |= is_rdonly;
3132 else if (is_rdonly)
3133 calldata->arg.fmode |= FMODE_READ;
3134 if (state->n_wronly == 0)
3135 call_close |= is_wronly;
3136 else if (is_wronly)
3137 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003138 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3139 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003140 } else if (is_rdwr)
3141 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3142
Trond Myklebust5cc78612016-11-14 11:19:56 -05003143 if (!nfs4_valid_open_stateid(state) ||
3144 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003145 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003146 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003147
3148 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003149 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003150 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003151 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003152
Peng Tao500d7012015-09-22 11:35:22 +08003153 if (nfs4_wait_on_layoutreturn(inode, task)) {
3154 nfs_release_seqid(calldata->arg.seqid);
3155 goto out_wait;
3156 }
3157
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003158 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003159 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003160 if (calldata->roc)
3161 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3162
Trond Myklebust6ae37332015-01-30 14:21:14 -05003163 calldata->arg.share_access =
3164 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3165 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003166
Trond Myklebust516a6af2005-10-27 22:12:41 -04003167 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003168 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003169 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003170 &calldata->arg.seq_args,
3171 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003172 task) != 0)
3173 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003174 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003175 return;
3176out_no_action:
3177 task->tk_action = NULL;
3178out_wait:
3179 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003182static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003183 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003184 .rpc_call_done = nfs4_close_done,
3185 .rpc_release = nfs4_free_closedata,
3186};
3187
Peng Taofe08c542014-07-03 13:05:01 +08003188static bool nfs4_roc(struct inode *inode)
3189{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003190 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003191 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003192 return pnfs_roc(inode);
3193}
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195/*
3196 * It is possible for data to be read/written from a mem-mapped file
3197 * after the sys_close call (which hits the vfs layer as a flush).
3198 * This means that we can't safely call nfsv4 close on a file until
3199 * the inode is cleared. This in turn means that we are not good
3200 * NFSv4 citizens - we do not indicate to the server to update the file's
3201 * share state even when we are done with one of the three share
3202 * stateid's in the inode.
3203 *
3204 * NOTE: Caller must be holding the sp->so_owner semaphore!
3205 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003206int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003208 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003209 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003211 struct nfs4_state_owner *sp = state->owner;
3212 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003213 struct rpc_message msg = {
3214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3215 .rpc_cred = state->owner->so_cred,
3216 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003217 struct rpc_task_setup task_setup_data = {
3218 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003219 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003220 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003221 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003222 .flags = RPC_TASK_ASYNC,
3223 };
Trond Myklebust95121352005-10-18 14:20:12 -07003224 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003226 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3227 &task_setup_data.rpc_client, &msg);
3228
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003229 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003231 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003232 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003233 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003235 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003237 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3238 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003239 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003240 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003241 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003242 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003243 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003244 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003245 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003246 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003247 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003248
Trond Myklebust1174dd12010-12-21 10:52:24 -05003249 msg.rpc_argp = &calldata->arg;
3250 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003251 task_setup_data.callback_data = calldata;
3252 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003253 if (IS_ERR(task))
3254 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003255 status = 0;
3256 if (wait)
3257 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003258 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003259 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003260out_free_calldata:
3261 kfree(calldata);
3262out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003263 nfs4_put_open_state(state);
3264 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003265 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Trond Myklebust2b484292010-09-17 10:56:51 -04003268static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003269nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3270 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003273 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3274
3275 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Trond Myklebust565277f2007-10-15 18:17:53 -04003277 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003278 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003279
3280 nfs4_label_release_security(label);
3281
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003282 if (IS_ERR(state))
3283 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003284 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
Trond Myklebust1185a552009-12-03 15:54:02 -05003287static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003288{
3289 if (ctx->state == NULL)
3290 return;
3291 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003292 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003293 else
Al Viro643168c2011-06-22 18:20:23 -04003294 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Trond Myklebustb944dba2013-11-04 15:20:20 -05003297#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3298#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003299#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3302{
Kinglong Mee8c612822015-08-26 21:12:58 +08003303 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003304 struct nfs4_server_caps_arg args = {
3305 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003306 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003307 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 struct nfs4_server_caps_res res = {};
3309 struct rpc_message msg = {
3310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003311 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 .rpc_resp = &res,
3313 };
3314 int status;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003315 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Kinglong Mee8c612822015-08-26 21:12:58 +08003317 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3318 FATTR4_WORD0_FH_EXPIRE_TYPE |
3319 FATTR4_WORD0_LINK_SUPPORT |
3320 FATTR4_WORD0_SYMLINK_SUPPORT |
3321 FATTR4_WORD0_ACLSUPPORT;
3322 if (minorversion)
3323 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3324
Bryan Schumaker7c513052011-03-24 17:12:24 +00003325 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003327 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003328 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003329 case 0:
3330 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3331 res.attr_bitmask[2] = 0;
3332 break;
3333 case 1:
3334 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3335 break;
3336 case 2:
3337 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003340 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3341 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3342 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3343 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003344 NFS_CAP_CTIME|NFS_CAP_MTIME|
3345 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003346 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3347 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 server->caps |= NFS_CAP_ACLS;
3349 if (res.has_links != 0)
3350 server->caps |= NFS_CAP_HARDLINKS;
3351 if (res.has_symlinks != 0)
3352 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003353 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3354 server->caps |= NFS_CAP_FILEID;
3355 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3356 server->caps |= NFS_CAP_MODE;
3357 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3358 server->caps |= NFS_CAP_NLINK;
3359 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3360 server->caps |= NFS_CAP_OWNER;
3361 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3362 server->caps |= NFS_CAP_OWNER_GROUP;
3363 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3364 server->caps |= NFS_CAP_ATIME;
3365 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3366 server->caps |= NFS_CAP_CTIME;
3367 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3368 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003369#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3370 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3371 server->caps |= NFS_CAP_SECURITY_LABEL;
3372#endif
3373 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3374 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003375 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003376
Trond Myklebusta65318b2009-03-11 14:10:28 -04003377 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3378 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3379 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003380 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003381
3382 /* Avoid a regression due to buggy server */
3383 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3384 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003385 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3386 sizeof(server->exclcreat_bitmask));
Trond Myklebustfca22d52017-05-09 15:47:15 -04003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003389 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return status;
3393}
3394
Trond Myklebust55a97592006-06-09 09:34:19 -04003395int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397 struct nfs4_exception exception = { };
3398 int err;
3399 do {
3400 err = nfs4_handle_exception(server,
3401 _nfs4_server_capabilities(server, fhandle),
3402 &exception);
3403 } while (exception.retry);
3404 return err;
3405}
3406
3407static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3408 struct nfs_fsinfo *info)
3409{
David Quigleyaa9c2662013-05-22 12:50:44 -04003410 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003412 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 };
3414 struct nfs4_lookup_res res = {
3415 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003416 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 .fh = fhandle,
3418 };
3419 struct rpc_message msg = {
3420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3421 .rpc_argp = &args,
3422 .rpc_resp = &res,
3423 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003424
David Quigleyaa9c2662013-05-22 12:50:44 -04003425 bitmask[0] = nfs4_fattr_bitmap[0];
3426 bitmask[1] = nfs4_fattr_bitmap[1];
3427 /*
3428 * Process the label in the upcoming getfattr
3429 */
3430 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3431
Trond Myklebust0e574af2005-10-27 22:12:38 -04003432 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003433 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
3436static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3437 struct nfs_fsinfo *info)
3438{
3439 struct nfs4_exception exception = { };
3440 int err;
3441 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003442 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003443 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003444 switch (err) {
3445 case 0:
3446 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003447 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003448 default:
3449 err = nfs4_handle_exception(server, err, &exception);
3450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003452out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return err;
3454}
3455
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003456static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3457 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3458{
Trond Myklebustc2190662013-08-26 19:23:04 -04003459 struct rpc_auth_create_args auth_args = {
3460 .pseudoflavor = flavor,
3461 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003462 struct rpc_auth *auth;
3463 int ret;
3464
Trond Myklebustc2190662013-08-26 19:23:04 -04003465 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003466 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003467 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003468 goto out;
3469 }
3470 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003471out:
3472 return ret;
3473}
3474
Chuck Lever9a744ba2013-03-16 15:56:02 -04003475/*
3476 * Retry pseudoroot lookup with various security flavors. We do this when:
3477 *
3478 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3479 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3480 *
3481 * Returns zero on success, or a negative NFS4ERR value, or a
3482 * negative errno value.
3483 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003484static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003485 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003487 /* Per 3530bis 15.33.5 */
3488 static const rpc_authflavor_t flav_array[] = {
3489 RPC_AUTH_GSS_KRB5P,
3490 RPC_AUTH_GSS_KRB5I,
3491 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003492 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003493 RPC_AUTH_NULL,
3494 };
3495 int status = -EPERM;
3496 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003498 if (server->auth_info.flavor_len > 0) {
3499 /* try each flavor specified by user */
3500 for (i = 0; i < server->auth_info.flavor_len; i++) {
3501 status = nfs4_lookup_root_sec(server, fhandle, info,
3502 server->auth_info.flavors[i]);
3503 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3504 continue;
3505 break;
3506 }
3507 } else {
3508 /* no flavors specified by user, try default list */
3509 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3510 status = nfs4_lookup_root_sec(server, fhandle, info,
3511 flav_array[i]);
3512 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3513 continue;
3514 break;
3515 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003516 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003517
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003518 /*
3519 * -EACCESS could mean that the user doesn't have correct permissions
3520 * to access the mount. It could also mean that we tried to mount
3521 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3522 * existing mount programs don't handle -EACCES very well so it should
3523 * be mapped to -EPERM instead.
3524 */
3525 if (status == -EACCES)
3526 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003527 return status;
3528}
3529
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003530/**
3531 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3532 * @server: initialized nfs_server handle
3533 * @fhandle: we fill in the pseudo-fs root file handle
3534 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003535 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003536 *
3537 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003538 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003539int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003540 struct nfs_fsinfo *info,
3541 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003542{
Andre Przywarac7757072015-04-23 17:17:40 +01003543 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003544
Andre Przywarac7757072015-04-23 17:17:40 +01003545 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003546 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003547
3548 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003549 status = server->nfs_client->cl_mvops->find_root_sec(server,
3550 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (status == 0)
3553 status = nfs4_server_capabilities(server, fhandle);
3554 if (status == 0)
3555 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003556
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003557 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558}
3559
Bryan Schumakerbae36242012-05-10 15:07:31 -04003560static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3561 struct nfs_fsinfo *info)
3562{
3563 int error;
3564 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003565 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003566
3567 error = nfs4_server_capabilities(server, mntfh);
3568 if (error < 0) {
3569 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3570 return error;
3571 }
3572
David Quigley14c43f72013-05-22 12:50:43 -04003573 label = nfs4_label_alloc(server, GFP_KERNEL);
3574 if (IS_ERR(label))
3575 return PTR_ERR(label);
3576
David Quigley1775fd32013-05-22 12:50:42 -04003577 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003578 if (error < 0) {
3579 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003580 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003581 }
3582
3583 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3584 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3585 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3586
David Quigley14c43f72013-05-22 12:50:43 -04003587err_free_label:
3588 nfs4_label_free(label);
3589
Bryan Schumakerbae36242012-05-10 15:07:31 -04003590 return error;
3591}
3592
Manoj Naik6b97fd32006-06-09 09:34:29 -04003593/*
3594 * Get locations and (maybe) other attributes of a referral.
3595 * Note that we'll actually follow the referral later when
3596 * we detect fsid mismatch in inode revalidation
3597 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003598static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3599 const struct qstr *name, struct nfs_fattr *fattr,
3600 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003601{
3602 int status = -ENOMEM;
3603 struct page *page = NULL;
3604 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003605
3606 page = alloc_page(GFP_KERNEL);
3607 if (page == NULL)
3608 goto out;
3609 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3610 if (locations == NULL)
3611 goto out;
3612
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003613 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003614 if (status != 0)
3615 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003616
3617 /*
3618 * If the fsid didn't change, this is a migration event, not a
3619 * referral. Cause us to drop into the exception handler, which
3620 * will kick off migration recovery.
3621 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003622 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003623 dprintk("%s: server did not return a different fsid for"
3624 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003625 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003626 goto out;
3627 }
Andy Adamson533eb462011-06-13 18:25:56 -04003628 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3629 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003630
Andy Adamson533eb462011-06-13 18:25:56 -04003631 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003632 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003633 memset(fhandle, 0, sizeof(struct nfs_fh));
3634out:
3635 if (page)
3636 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003637 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003638 return status;
3639}
3640
David Quigley1775fd32013-05-22 12:50:42 -04003641static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3642 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
3644 struct nfs4_getattr_arg args = {
3645 .fh = fhandle,
3646 .bitmask = server->attr_bitmask,
3647 };
3648 struct nfs4_getattr_res res = {
3649 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003650 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 .server = server,
3652 };
3653 struct rpc_message msg = {
3654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3655 .rpc_argp = &args,
3656 .rpc_resp = &res,
3657 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003658
3659 args.bitmask = nfs4_bitmask(server, label);
3660
Trond Myklebust0e574af2005-10-27 22:12:38 -04003661 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003662 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
David Quigley1775fd32013-05-22 12:50:42 -04003665static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3666 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
3668 struct nfs4_exception exception = { };
3669 int err;
3670 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003671 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3672 trace_nfs4_getattr(server, fhandle, fattr, err);
3673 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 &exception);
3675 } while (exception.retry);
3676 return err;
3677}
3678
3679/*
3680 * The file is not closed if it is opened due to the a request to change
3681 * the size of the file. The open call will not be needed once the
3682 * VFS layer lookup-intents are implemented.
3683 *
3684 * Close is called when the inode is destroyed.
3685 * If we haven't opened the file for O_WRONLY, we
3686 * need to in the size_change case to obtain a stateid.
3687 *
3688 * Got race?
3689 * Because OPEN is always done by name in nfsv4, it is
3690 * possible that we opened a different file by the same
3691 * name. We can recognize this race condition, but we
3692 * can't do anything about it besides returning an error.
3693 *
3694 * This will be fixed with VFS changes (lookup-intent).
3695 */
3696static int
3697nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3698 struct iattr *sattr)
3699{
David Howells2b0143b2015-03-17 22:25:59 +00003700 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003701 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003702 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003703 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 int status;
3705
Peng Tao88ac8152014-09-12 11:04:10 +08003706 if (pnfs_ld_layoutret_on_setattr(inode) &&
3707 sattr->ia_valid & ATTR_SIZE &&
3708 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003709 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003710
Trond Myklebust0e574af2005-10-27 22:12:38 -04003711 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Andy Adamson26699402012-05-30 16:12:24 -04003713 /* Deal with open(O_TRUNC) */
3714 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003715 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003716
3717 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003718 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003719 return 0;
3720
Trond Myklebustd5308382005-11-04 15:33:38 -05003721 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003722 if (sattr->ia_valid & ATTR_FILE) {
3723 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003724
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003725 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003726 if (ctx) {
3727 cred = ctx->cred;
3728 state = ctx->state;
3729 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003730 }
3731
David Quigley14c43f72013-05-22 12:50:43 -04003732 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3733 if (IS_ERR(label))
3734 return PTR_ERR(label);
3735
3736 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003737 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003738 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003739 nfs_setsecurity(inode, fattr, label);
3740 }
David Quigley14c43f72013-05-22 12:50:43 -04003741 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return status;
3743}
3744
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003745static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3746 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003747 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003748{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003749 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003750 int status;
3751 struct nfs4_lookup_arg args = {
3752 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003753 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003754 .name = name,
3755 };
3756 struct nfs4_lookup_res res = {
3757 .server = server,
3758 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003759 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003760 .fh = fhandle,
3761 };
3762 struct rpc_message msg = {
3763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3764 .rpc_argp = &args,
3765 .rpc_resp = &res,
3766 };
3767
David Quigleyaa9c2662013-05-22 12:50:44 -04003768 args.bitmask = nfs4_bitmask(server, label);
3769
David Howells2b3de442006-08-22 20:06:09 -04003770 nfs_fattr_init(fattr);
3771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003773 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 dprintk("NFS reply lookup: %d\n", status);
3775 return status;
3776}
3777
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003778static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003779{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003780 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003781 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003782 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3783 fattr->nlink = 2;
3784}
3785
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003786static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003787 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003788 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003789{
3790 struct nfs4_exception exception = { };
3791 struct rpc_clnt *client = *clnt;
3792 int err;
3793 do {
David Quigley1775fd32013-05-22 12:50:42 -04003794 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003795 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003796 switch (err) {
3797 case -NFS4ERR_BADNAME:
3798 err = -ENOENT;
3799 goto out;
3800 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003801 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003802 if (err == -NFS4ERR_MOVED)
3803 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003804 goto out;
3805 case -NFS4ERR_WRONGSEC:
3806 err = -EPERM;
3807 if (client != *clnt)
3808 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003809 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003810 if (IS_ERR(client))
3811 return PTR_ERR(client);
3812
3813 exception.retry = 1;
3814 break;
3815 default:
3816 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3817 }
3818 } while (exception.retry);
3819
3820out:
3821 if (err == 0)
3822 *clnt = client;
3823 else if (client != *clnt)
3824 rpc_shutdown_client(client);
3825
3826 return err;
3827}
3828
Al Virobeffb8f2016-07-20 16:34:42 -04003829static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003830 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3831 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003833 int status;
3834 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003835
David Quigley1775fd32013-05-22 12:50:42 -04003836 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003837 if (client != NFS_CLIENT(dir)) {
3838 rpc_shutdown_client(client);
3839 nfs_fixup_secinfo_attributes(fattr);
3840 }
3841 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842}
3843
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003844struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003845nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003846 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3847{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003848 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003849 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003850
David Quigley1775fd32013-05-22 12:50:42 -04003851 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003852 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003853 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003854 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003855}
3856
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3858{
Trond Myklebust76b32992007-08-10 17:45:11 -04003859 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 struct nfs4_accessargs args = {
3861 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003862 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003864 struct nfs4_accessres res = {
3865 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003866 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 struct rpc_message msg = {
3868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3869 .rpc_argp = &args,
3870 .rpc_resp = &res,
3871 .rpc_cred = entry->cred,
3872 };
3873 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003874 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
3876 /*
3877 * Determine which access bits we want to ask for...
3878 */
3879 if (mode & MAY_READ)
3880 args.access |= NFS4_ACCESS_READ;
3881 if (S_ISDIR(inode->i_mode)) {
3882 if (mode & MAY_WRITE)
3883 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3884 if (mode & MAY_EXEC)
3885 args.access |= NFS4_ACCESS_LOOKUP;
3886 } else {
3887 if (mode & MAY_WRITE)
3888 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3889 if (mode & MAY_EXEC)
3890 args.access |= NFS4_ACCESS_EXECUTE;
3891 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003892
3893 res.fattr = nfs_alloc_fattr();
3894 if (res.fattr == NULL)
3895 return -ENOMEM;
3896
Bryan Schumaker7c513052011-03-24 17:12:24 +00003897 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003899 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003900 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003902 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return status;
3904}
3905
3906static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3907{
3908 struct nfs4_exception exception = { };
3909 int err;
3910 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003911 err = _nfs4_proc_access(inode, entry);
3912 trace_nfs4_access(inode, err);
3913 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 &exception);
3915 } while (exception.retry);
3916 return err;
3917}
3918
3919/*
3920 * TODO: For the time being, we don't try to get any attributes
3921 * along with any of the zero-copy operations READ, READDIR,
3922 * READLINK, WRITE.
3923 *
3924 * In the case of the first three, we want to put the GETATTR
3925 * after the read-type operation -- this is because it is hard
3926 * to predict the length of a GETATTR response in v4, and thus
3927 * align the READ data correctly. This means that the GETATTR
3928 * may end up partially falling into the page cache, and we should
3929 * shift it into the 'tail' of the xdr_buf before processing.
3930 * To do this efficiently, we need to know the total length
3931 * of data received, which doesn't seem to be available outside
3932 * of the RPC layer.
3933 *
3934 * In the case of WRITE, we also want to put the GETATTR after
3935 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003936 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 *
3938 * Both of these changes to the XDR layer would in fact be quite
3939 * minor, but I decided to leave them for a subsequent patch.
3940 */
3941static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3942 unsigned int pgbase, unsigned int pglen)
3943{
3944 struct nfs4_readlink args = {
3945 .fh = NFS_FH(inode),
3946 .pgbase = pgbase,
3947 .pglen = pglen,
3948 .pages = &page,
3949 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003950 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 struct rpc_message msg = {
3952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3953 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003954 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 };
3956
Bryan Schumaker7c513052011-03-24 17:12:24 +00003957 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 -07003958}
3959
3960static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3961 unsigned int pgbase, unsigned int pglen)
3962{
3963 struct nfs4_exception exception = { };
3964 int err;
3965 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003966 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3967 trace_nfs4_readlink(inode, err);
3968 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 &exception);
3970 } while (exception.retry);
3971 return err;
3972}
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003975 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977static int
3978nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003979 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
David Quigleyaa9c2662013-05-22 12:50:44 -04003981 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003982 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 int status = 0;
3985
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003986 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3987 if (IS_ERR(ctx))
3988 return PTR_ERR(ctx);
3989
David Quigleyaa9c2662013-05-22 12:50:44 -04003990 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3991
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003992 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003993 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 if (IS_ERR(state)) {
3995 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003996 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003999 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004000 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 return status;
4002}
4003
Al Virobeffb8f2016-07-20 16:34:42 -04004004static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Trond Myklebust16e42952005-10-27 22:12:44 -04004006 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004007 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004009 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004011 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004012 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004013 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4016 .rpc_argp = &args,
4017 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004019 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004020
Bryan Schumaker7c513052011-03-24 17:12:24 +00004021 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004022 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004023 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return status;
4025}
4026
Al Virobeffb8f2016-07-20 16:34:42 -04004027static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
4029 struct nfs4_exception exception = { };
4030 int err;
4031 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004032 err = _nfs4_proc_remove(dir, name);
4033 trace_nfs4_remove(dir, name, err);
4034 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 &exception);
4036 } while (exception.retry);
4037 return err;
4038}
4039
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004040static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004042 struct nfs_server *server = NFS_SERVER(dir);
4043 struct nfs_removeargs *args = msg->rpc_argp;
4044 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004046 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004048 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004049
4050 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051}
4052
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004053static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4054{
Al Viro884be172016-04-28 23:56:31 -04004055 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004056 &data->args.seq_args,
4057 &data->res.seq_res,
4058 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004061static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004063 struct nfs_unlinkdata *data = task->tk_calldata;
4064 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004065
Trond Myklebust14516c32010-07-31 14:29:06 -04004066 if (!nfs4_sequence_done(task, &res->seq_res))
4067 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004068 if (nfs4_async_handle_error(task, res->server, NULL,
4069 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004070 return 0;
4071 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004072 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
Jeff Laytond3d41522010-09-17 17:31:57 -04004075static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4076{
4077 struct nfs_server *server = NFS_SERVER(dir);
4078 struct nfs_renameargs *arg = msg->rpc_argp;
4079 struct nfs_renameres *res = msg->rpc_resp;
4080
4081 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004082 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004083 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004084}
4085
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004086static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4087{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004088 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4089 &data->args.seq_args,
4090 &data->res.seq_res,
4091 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004092}
4093
4094static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4095 struct inode *new_dir)
4096{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004097 struct nfs_renamedata *data = task->tk_calldata;
4098 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004099
4100 if (!nfs4_sequence_done(task, &res->seq_res))
4101 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004102 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004103 return 0;
4104
4105 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004106 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004107 return 1;
4108}
4109
Al Virobeffb8f2016-07-20 16:34:42 -04004110static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004112 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 struct nfs4_link_arg arg = {
4114 .fh = NFS_FH(inode),
4115 .dir_fh = NFS_FH(dir),
4116 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004117 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004119 struct nfs4_link_res res = {
4120 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004121 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 struct rpc_message msg = {
4124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4125 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004126 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004128 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Trond Myklebust136f2622010-04-16 16:22:49 -04004130 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004131 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004132 goto out;
4133
David Quigley14c43f72013-05-22 12:50:43 -04004134 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4135 if (IS_ERR(res.label)) {
4136 status = PTR_ERR(res.label);
4137 goto out;
4138 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004139 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004140
Bryan Schumaker7c513052011-03-24 17:12:24 +00004141 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004142 if (!status) {
4143 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004144 status = nfs_post_op_update_inode(inode, res.fattr);
4145 if (!status)
4146 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004147 }
David Quigley14c43f72013-05-22 12:50:43 -04004148
4149
4150 nfs4_label_free(res.label);
4151
Trond Myklebust136f2622010-04-16 16:22:49 -04004152out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004153 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return status;
4155}
4156
Al Virobeffb8f2016-07-20 16:34:42 -04004157static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
4159 struct nfs4_exception exception = { };
4160 int err;
4161 do {
4162 err = nfs4_handle_exception(NFS_SERVER(inode),
4163 _nfs4_proc_link(inode, dir, name),
4164 &exception);
4165 } while (exception.retry);
4166 return err;
4167}
4168
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004169struct nfs4_createdata {
4170 struct rpc_message msg;
4171 struct nfs4_create_arg arg;
4172 struct nfs4_create_res res;
4173 struct nfs_fh fh;
4174 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004175 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004176};
4177
4178static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004179 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004180{
4181 struct nfs4_createdata *data;
4182
4183 data = kzalloc(sizeof(*data), GFP_KERNEL);
4184 if (data != NULL) {
4185 struct nfs_server *server = NFS_SERVER(dir);
4186
David Quigley14c43f72013-05-22 12:50:43 -04004187 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4188 if (IS_ERR(data->label))
4189 goto out_free;
4190
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004191 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4192 data->msg.rpc_argp = &data->arg;
4193 data->msg.rpc_resp = &data->res;
4194 data->arg.dir_fh = NFS_FH(dir);
4195 data->arg.server = server;
4196 data->arg.name = name;
4197 data->arg.attrs = sattr;
4198 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004199 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004200 data->res.server = server;
4201 data->res.fh = &data->fh;
4202 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004203 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004205 }
4206 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004207out_free:
4208 kfree(data);
4209 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004210}
4211
4212static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4213{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004214 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004215 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 if (status == 0) {
4217 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004218 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004219 }
4220 return status;
4221}
4222
4223static void nfs4_free_createdata(struct nfs4_createdata *data)
4224{
David Quigley14c43f72013-05-22 12:50:43 -04004225 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004226 kfree(data);
4227}
4228
Chuck Lever4f390c12006-08-22 20:06:22 -04004229static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004230 struct page *page, unsigned int len, struct iattr *sattr,
4231 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004233 struct nfs4_createdata *data;
4234 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Chuck Lever94a6d752006-08-22 20:06:23 -04004236 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004238
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004239 status = -ENOMEM;
4240 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4241 if (data == NULL)
4242 goto out;
4243
4244 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4245 data->arg.u.symlink.pages = &page;
4246 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004247 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 status = nfs4_do_create(dir, dentry, data);
4250
4251 nfs4_free_createdata(data);
4252out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return status;
4254}
4255
Chuck Lever4f390c12006-08-22 20:06:22 -04004256static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004257 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004260 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004262
4263 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004266 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4267 trace_nfs4_symlink(dir, &dentry->d_name, err);
4268 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 &exception);
4270 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004271
4272 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return err;
4274}
4275
4276static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004277 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004279 struct nfs4_createdata *data;
4280 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004282 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4283 if (data == NULL)
4284 goto out;
4285
David Quigley1775fd32013-05-22 12:50:42 -04004286 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004287 status = nfs4_do_create(dir, dentry, data);
4288
4289 nfs4_free_createdata(data);
4290out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 return status;
4292}
4293
4294static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4295 struct iattr *sattr)
4296{
4297 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004298 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004300
David Quigleyaa9c2662013-05-22 12:50:44 -04004301 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4302
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004303 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004305 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4306 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4307 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 &exception);
4309 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004310 nfs4_label_release_security(label);
4311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 return err;
4313}
4314
4315static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004316 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
David Howells2b0143b2015-03-17 22:25:59 +00004318 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 struct nfs4_readdir_arg args = {
4320 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004321 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 .pgbase = 0,
4323 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004324 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004325 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 };
4327 struct nfs4_readdir_res res;
4328 struct rpc_message msg = {
4329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4330 .rpc_argp = &args,
4331 .rpc_resp = &res,
4332 .rpc_cred = cred,
4333 };
4334 int status;
4335
Al Viro6de14722013-09-16 10:53:17 -04004336 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4337 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004338 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004339 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004341 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 -05004342 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004343 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004344 status += args.pgbase;
4345 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004346
4347 nfs_invalidate_atime(dir);
4348
Harvey Harrison3110ff82008-05-02 13:42:44 -07004349 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return status;
4351}
4352
4353static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004354 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
4356 struct nfs4_exception exception = { };
4357 int err;
4358 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004359 err = _nfs4_proc_readdir(dentry, cred, cookie,
4360 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004361 trace_nfs4_readdir(d_inode(dentry), err);
4362 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 &exception);
4364 } while (exception.retry);
4365 return err;
4366}
4367
4368static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004369 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004371 struct nfs4_createdata *data;
4372 int mode = sattr->ia_mode;
4373 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004375 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4376 if (data == NULL)
4377 goto out;
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004380 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004382 data->arg.ftype = NF4BLK;
4383 data->arg.u.device.specdata1 = MAJOR(rdev);
4384 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 }
4386 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004387 data->arg.ftype = NF4CHR;
4388 data->arg.u.device.specdata1 = MAJOR(rdev);
4389 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004390 } else if (!S_ISSOCK(mode)) {
4391 status = -EINVAL;
4392 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 }
David Quigley1775fd32013-05-22 12:50:42 -04004394
David Quigleyaa9c2662013-05-22 12:50:44 -04004395 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004397out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004398 nfs4_free_createdata(data);
4399out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return status;
4401}
4402
4403static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4404 struct iattr *sattr, dev_t rdev)
4405{
4406 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004407 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004409
David Quigleyaa9c2662013-05-22 12:50:44 -04004410 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4411
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004412 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004414 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4415 trace_nfs4_mknod(dir, &dentry->d_name, err);
4416 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 &exception);
4418 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004419
4420 nfs4_label_release_security(label);
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 return err;
4423}
4424
4425static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4426 struct nfs_fsstat *fsstat)
4427{
4428 struct nfs4_statfs_arg args = {
4429 .fh = fhandle,
4430 .bitmask = server->attr_bitmask,
4431 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004432 struct nfs4_statfs_res res = {
4433 .fsstat = fsstat,
4434 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 struct rpc_message msg = {
4436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4437 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004438 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 };
4440
Trond Myklebust0e574af2005-10-27 22:12:38 -04004441 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004442 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
4445static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4446{
4447 struct nfs4_exception exception = { };
4448 int err;
4449 do {
4450 err = nfs4_handle_exception(server,
4451 _nfs4_proc_statfs(server, fhandle, fsstat),
4452 &exception);
4453 } while (exception.retry);
4454 return err;
4455}
4456
4457static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4458 struct nfs_fsinfo *fsinfo)
4459{
4460 struct nfs4_fsinfo_arg args = {
4461 .fh = fhandle,
4462 .bitmask = server->attr_bitmask,
4463 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004464 struct nfs4_fsinfo_res res = {
4465 .fsinfo = fsinfo,
4466 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 struct rpc_message msg = {
4468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4469 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004470 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 };
4472
Bryan Schumaker7c513052011-03-24 17:12:24 +00004473 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
4476static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4477{
4478 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004479 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 int err;
4481
4482 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004483 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004484 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004485 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004486 nfs4_set_lease_period(server->nfs_client,
4487 fsinfo->lease_time * HZ,
4488 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004489 break;
4490 }
4491 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 } while (exception.retry);
4493 return err;
4494}
4495
4496static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4497{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004498 int error;
4499
Trond Myklebust0e574af2005-10-27 22:12:38 -04004500 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004501 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004502 if (error == 0) {
4503 /* block layout checks this! */
4504 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004505 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004506 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004507
4508 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509}
4510
4511static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4512 struct nfs_pathconf *pathconf)
4513{
4514 struct nfs4_pathconf_arg args = {
4515 .fh = fhandle,
4516 .bitmask = server->attr_bitmask,
4517 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004518 struct nfs4_pathconf_res res = {
4519 .pathconf = pathconf,
4520 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 struct rpc_message msg = {
4522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4523 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004524 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 };
4526
4527 /* None of the pathconf attributes are mandatory to implement */
4528 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4529 memset(pathconf, 0, sizeof(*pathconf));
4530 return 0;
4531 }
4532
Trond Myklebust0e574af2005-10-27 22:12:38 -04004533 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004534 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
4537static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4538 struct nfs_pathconf *pathconf)
4539{
4540 struct nfs4_exception exception = { };
4541 int err;
4542
4543 do {
4544 err = nfs4_handle_exception(server,
4545 _nfs4_proc_pathconf(server, fhandle, pathconf),
4546 &exception);
4547 } while (exception.retry);
4548 return err;
4549}
4550
Trond Myklebust5521abf2013-03-16 20:54:34 -04004551int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004552 const struct nfs_open_context *ctx,
4553 const struct nfs_lock_context *l_ctx,
4554 fmode_t fmode)
4555{
4556 const struct nfs_lockowner *lockowner = NULL;
4557
4558 if (l_ctx != NULL)
4559 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004560 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004561}
4562EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4563
Trond Myklebust5521abf2013-03-16 20:54:34 -04004564static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4565 const struct nfs_open_context *ctx,
4566 const struct nfs_lock_context *l_ctx,
4567 fmode_t fmode)
4568{
4569 nfs4_stateid current_stateid;
4570
Trond Myklebuste1253be2014-03-05 08:44:23 -05004571 /* If the current stateid represents a lost lock, then exit */
4572 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4573 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004574 return nfs4_stateid_match(stateid, &current_stateid);
4575}
4576
4577static bool nfs4_error_stateid_expired(int err)
4578{
4579 switch (err) {
4580 case -NFS4ERR_DELEG_REVOKED:
4581 case -NFS4ERR_ADMIN_REVOKED:
4582 case -NFS4ERR_BAD_STATEID:
4583 case -NFS4ERR_STALE_STATEID:
4584 case -NFS4ERR_OLD_STATEID:
4585 case -NFS4ERR_OPENMODE:
4586 case -NFS4ERR_EXPIRED:
4587 return true;
4588 }
4589 return false;
4590}
4591
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004592static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004594 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004596 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004597 if (task->tk_status < 0) {
4598 struct nfs4_exception exception = {
4599 .inode = hdr->inode,
4600 .state = hdr->args.context->state,
4601 .stateid = &hdr->args.stateid,
4602 };
4603 task->tk_status = nfs4_async_handle_exception(task,
4604 server, task->tk_status, &exception);
4605 if (exception.retry) {
4606 rpc_restart_call_prepare(task);
4607 return -EAGAIN;
4608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614}
4615
Trond Myklebust5521abf2013-03-16 20:54:34 -04004616static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004617 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004618{
4619
4620 if (!nfs4_error_stateid_expired(task->tk_status) ||
4621 nfs4_stateid_is_current(&args->stateid,
4622 args->context,
4623 args->lock_context,
4624 FMODE_READ))
4625 return false;
4626 rpc_restart_call_prepare(task);
4627 return true;
4628}
4629
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004631{
4632
4633 dprintk("--> %s\n", __func__);
4634
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004635 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004636 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004637 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004638 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004639 if (task->tk_status > 0)
4640 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004641 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4642 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004643}
4644
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004645static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4646 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004648 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004649 if (!hdr->pgio_done_cb)
4650 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004651 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004652 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004655static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4656 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004657{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004658 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4659 &hdr->args.seq_args,
4660 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004661 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004662 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4664 hdr->args.lock_context,
4665 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004666 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004668 return -EIO;
4669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672static int nfs4_write_done_cb(struct rpc_task *task,
4673 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004676
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004678 if (task->tk_status < 0) {
4679 struct nfs4_exception exception = {
4680 .inode = hdr->inode,
4681 .state = hdr->args.context->state,
4682 .stateid = &hdr->args.stateid,
4683 };
4684 task->tk_status = nfs4_async_handle_exception(task,
4685 NFS_SERVER(inode), task->tk_status,
4686 &exception);
4687 if (exception.retry) {
4688 rpc_restart_call_prepare(task);
4689 return -EAGAIN;
4690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004692 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004694 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004695 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
Trond Myklebust5521abf2013-03-16 20:54:34 -04004699static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004700 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004701{
4702
4703 if (!nfs4_error_stateid_expired(task->tk_status) ||
4704 nfs4_stateid_is_current(&args->stateid,
4705 args->context,
4706 args->lock_context,
4707 FMODE_WRITE))
4708 return false;
4709 rpc_restart_call_prepare(task);
4710 return true;
4711}
4712
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004714{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004715 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004716 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004717 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004718 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4720 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004721}
4722
Trond Myklebust5a37f852012-04-28 14:55:16 -04004723static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004724bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004725{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004726 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004727 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004728 return false;
4729 /* Otherwise, request attributes if and only if we don't hold
4730 * a delegation
4731 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004732 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004733}
Fred Isamana69aef12011-03-03 15:13:47 +00004734
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004735static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4736 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004738 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004739
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4741 hdr->args.bitmask = NULL;
4742 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004743 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004744 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004745
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004746 if (!hdr->pgio_done_cb)
4747 hdr->pgio_done_cb = nfs4_write_done_cb;
4748 hdr->res.server = server;
4749 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004751 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004752 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753}
4754
Fred Isaman0b7c0152012-04-20 14:47:39 -04004755static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4756{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004757 nfs4_setup_sequence(NFS_SERVER(data->inode),
4758 &data->args.seq_args,
4759 &data->res.seq_res,
4760 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761}
4762
Fred Isaman0b7c0152012-04-20 14:47:39 -04004763static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004766
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004767 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004768 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4769 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004770 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004771 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774}
4775
Fred Isaman0b7c0152012-04-20 14:47:39 -04004776static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004777{
4778 if (!nfs4_sequence_done(task, &data->res.seq_res))
4779 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004780 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004781}
4782
Fred Isaman0b7c0152012-04-20 14:47:39 -04004783static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004785 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004786
Fred Isaman0b7c0152012-04-20 14:47:39 -04004787 if (data->commit_done_cb == NULL)
4788 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004789 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004790 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004791 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792}
4793
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004794struct nfs4_renewdata {
4795 struct nfs_client *client;
4796 unsigned long timestamp;
4797};
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799/*
4800 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4801 * standalone procedure for queueing an asynchronous RENEW.
4802 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004803static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004804{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004805 struct nfs4_renewdata *data = calldata;
4806 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004807
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004808 if (atomic_read(&clp->cl_count) > 1)
4809 nfs4_schedule_state_renewal(clp);
4810 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004811 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004812}
4813
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004814static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004816 struct nfs4_renewdata *data = calldata;
4817 struct nfs_client *clp = data->client;
4818 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004820 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004821 switch (task->tk_status) {
4822 case 0:
4823 break;
4824 case -NFS4ERR_LEASE_MOVED:
4825 nfs4_schedule_lease_moved_recovery(clp);
4826 break;
4827 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004828 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004829 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4830 return;
4831 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004832 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004833 return;
4834 }
4835 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004837 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838}
4839
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004840static const struct rpc_call_ops nfs4_renew_ops = {
4841 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004842 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004843};
4844
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004845static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
4847 struct rpc_message msg = {
4848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4849 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004850 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004852 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004854 if (renew_flags == 0)
4855 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004856 if (!atomic_inc_not_zero(&clp->cl_count))
4857 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004858 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004859 if (data == NULL)
4860 return -ENOMEM;
4861 data->client = clp;
4862 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004863 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004864 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004867static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
4869 struct rpc_message msg = {
4870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4871 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004872 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 };
4874 unsigned long now = jiffies;
4875 int status;
4876
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004877 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 if (status < 0)
4879 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004880 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return 0;
4882}
4883
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004884static inline int nfs4_server_supports_acls(struct nfs_server *server)
4885{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004886 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004887}
4888
Trond Myklebust21f498c2012-08-24 10:59:25 -04004889/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4890 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004891 * the stack.
4892 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004893#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004894
Neil Hormane9e3d722011-03-04 19:26:03 -05004895static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004896 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004897{
4898 struct page *newpage, **spages;
4899 int rc = 0;
4900 size_t len;
4901 spages = pages;
4902
4903 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004904 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004905 newpage = alloc_page(GFP_KERNEL);
4906
4907 if (newpage == NULL)
4908 goto unwind;
4909 memcpy(page_address(newpage), buf, len);
4910 buf += len;
4911 buflen -= len;
4912 *pages++ = newpage;
4913 rc++;
4914 } while (buflen != 0);
4915
4916 return rc;
4917
4918unwind:
4919 for(; rc > 0; rc--)
4920 __free_page(spages[rc-1]);
4921 return -ENOMEM;
4922}
4923
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004924struct nfs4_cached_acl {
4925 int cached;
4926 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004927 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004928};
4929
4930static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004931{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004932 struct nfs_inode *nfsi = NFS_I(inode);
4933
4934 spin_lock(&inode->i_lock);
4935 kfree(nfsi->nfs4_acl);
4936 nfsi->nfs4_acl = acl;
4937 spin_unlock(&inode->i_lock);
4938}
4939
4940static void nfs4_zap_acl_attr(struct inode *inode)
4941{
4942 nfs4_set_cached_acl(inode, NULL);
4943}
4944
4945static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4946{
4947 struct nfs_inode *nfsi = NFS_I(inode);
4948 struct nfs4_cached_acl *acl;
4949 int ret = -ENOENT;
4950
4951 spin_lock(&inode->i_lock);
4952 acl = nfsi->nfs4_acl;
4953 if (acl == NULL)
4954 goto out;
4955 if (buf == NULL) /* user is just asking for length */
4956 goto out_len;
4957 if (acl->cached == 0)
4958 goto out;
4959 ret = -ERANGE; /* see getxattr(2) man page */
4960 if (acl->len > buflen)
4961 goto out;
4962 memcpy(buf, acl->data, acl->len);
4963out_len:
4964 ret = acl->len;
4965out:
4966 spin_unlock(&inode->i_lock);
4967 return ret;
4968}
4969
Sachin Prabhu5794d212012-04-17 14:36:40 +01004970static 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 +00004971{
4972 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004973 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004974
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004975 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004976 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004977 if (acl == NULL)
4978 goto out;
4979 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004980 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004981 } else {
4982 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4983 if (acl == NULL)
4984 goto out;
4985 acl->cached = 0;
4986 }
4987 acl->len = acl_len;
4988out:
4989 nfs4_set_cached_acl(inode, acl);
4990}
4991
Andy Adamsonbf118a32011-12-07 11:55:27 -05004992/*
4993 * The getxattr API returns the required buffer length when called with a
4994 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4995 * the required buf. On a NULL buf, we send a page of data to the server
4996 * guessing that the ACL request can be serviced by a page. If so, we cache
4997 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4998 * the cache. If not so, we throw away the page, and cache the required
4999 * length. The next getxattr call will then produce another round trip to
5000 * the server, this time with the input buf of the required size.
5001 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005002static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005003{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005004 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005005 struct nfs_getaclargs args = {
5006 .fh = NFS_FH(inode),
5007 .acl_pages = pages,
5008 .acl_len = buflen,
5009 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005010 struct nfs_getaclres res = {
5011 .acl_len = buflen,
5012 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005013 struct rpc_message msg = {
5014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5015 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005016 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005017 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005018 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005019 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005020
Trond Myklebust21f498c2012-08-24 10:59:25 -04005021 if (npages > ARRAY_SIZE(pages))
5022 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005023
Andy Adamsonbf118a32011-12-07 11:55:27 -05005024 for (i = 0; i < npages; i++) {
5025 pages[i] = alloc_page(GFP_KERNEL);
5026 if (!pages[i])
5027 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005028 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005029
5030 /* for decoding across pages */
5031 res.acl_scratch = alloc_page(GFP_KERNEL);
5032 if (!res.acl_scratch)
5033 goto out_free;
5034
Andy Adamsonbf118a32011-12-07 11:55:27 -05005035 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005036
Peng Taode040be2012-01-10 22:42:47 +08005037 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005038 __func__, buf, buflen, npages, args.acl_len);
5039 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5040 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005041 if (ret)
5042 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005043
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005044 /* Handle the case where the passed-in buffer is too short */
5045 if (res.acl_flags & NFS4_ACL_TRUNC) {
5046 /* Did the user only issue a request for the acl length? */
5047 if (buf == NULL)
5048 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005049 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005050 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005051 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005052 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005053 if (buf) {
5054 if (res.acl_len > buflen) {
5055 ret = -ERANGE;
5056 goto out_free;
5057 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005058 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005059 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005060out_ok:
5061 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005062out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005063 for (i = 0; i < npages; i++)
5064 if (pages[i])
5065 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005066 if (res.acl_scratch)
5067 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005068 return ret;
5069}
5070
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005071static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5072{
5073 struct nfs4_exception exception = { };
5074 ssize_t ret;
5075 do {
5076 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005077 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005078 if (ret >= 0)
5079 break;
5080 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5081 } while (exception.retry);
5082 return ret;
5083}
5084
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005085static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5086{
5087 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005088 int ret;
5089
5090 if (!nfs4_server_supports_acls(server))
5091 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005092 ret = nfs_revalidate_inode(server, inode);
5093 if (ret < 0)
5094 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005095 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5096 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005097 ret = nfs4_read_cached_acl(inode, buf, buflen);
5098 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005099 /* -ENOENT is returned if there is no ACL or if there is an ACL
5100 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005101 return ret;
5102 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005103}
5104
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005105static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005106{
5107 struct nfs_server *server = NFS_SERVER(inode);
5108 struct page *pages[NFS4ACL_MAXPAGES];
5109 struct nfs_setaclargs arg = {
5110 .fh = NFS_FH(inode),
5111 .acl_pages = pages,
5112 .acl_len = buflen,
5113 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005114 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005115 struct rpc_message msg = {
5116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5117 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005118 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005119 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005120 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005121 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005122
5123 if (!nfs4_server_supports_acls(server))
5124 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005125 if (npages > ARRAY_SIZE(pages))
5126 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005127 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005128 if (i < 0)
5129 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005130 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005131 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005132
5133 /*
5134 * Free each page after tx, so the only ref left is
5135 * held by the network stack
5136 */
5137 for (; i > 0; i--)
5138 put_page(pages[i-1]);
5139
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005140 /*
5141 * Acl update can result in inode attribute update.
5142 * so mark the attribute cache invalid.
5143 */
5144 spin_lock(&inode->i_lock);
5145 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5146 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005147 nfs_access_zap_cache(inode);
5148 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005149 return ret;
5150}
5151
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005152static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5153{
5154 struct nfs4_exception exception = { };
5155 int err;
5156 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005157 err = __nfs4_proc_set_acl(inode, buf, buflen);
5158 trace_nfs4_set_acl(inode, err);
5159 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005160 &exception);
5161 } while (exception.retry);
5162 return err;
5163}
5164
David Quigleyaa9c2662013-05-22 12:50:44 -04005165#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5166static int _nfs4_get_security_label(struct inode *inode, void *buf,
5167 size_t buflen)
5168{
5169 struct nfs_server *server = NFS_SERVER(inode);
5170 struct nfs_fattr fattr;
5171 struct nfs4_label label = {0, 0, buflen, buf};
5172
5173 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005174 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005175 .fh = NFS_FH(inode),
5176 .bitmask = bitmask,
5177 };
5178 struct nfs4_getattr_res res = {
5179 .fattr = &fattr,
5180 .label = &label,
5181 .server = server,
5182 };
5183 struct rpc_message msg = {
5184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005185 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005186 .rpc_resp = &res,
5187 };
5188 int ret;
5189
5190 nfs_fattr_init(&fattr);
5191
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005192 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005193 if (ret)
5194 return ret;
5195 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5196 return -ENOENT;
5197 if (buflen < label.len)
5198 return -ERANGE;
5199 return 0;
5200}
5201
5202static int nfs4_get_security_label(struct inode *inode, void *buf,
5203 size_t buflen)
5204{
5205 struct nfs4_exception exception = { };
5206 int err;
5207
5208 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5209 return -EOPNOTSUPP;
5210
5211 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005212 err = _nfs4_get_security_label(inode, buf, buflen);
5213 trace_nfs4_get_security_label(inode, err);
5214 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005215 &exception);
5216 } while (exception.retry);
5217 return err;
5218}
5219
5220static int _nfs4_do_set_security_label(struct inode *inode,
5221 struct nfs4_label *ilabel,
5222 struct nfs_fattr *fattr,
5223 struct nfs4_label *olabel)
5224{
5225
5226 struct iattr sattr = {0};
5227 struct nfs_server *server = NFS_SERVER(inode);
5228 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005229 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005230 .fh = NFS_FH(inode),
5231 .iap = &sattr,
5232 .server = server,
5233 .bitmask = bitmask,
5234 .label = ilabel,
5235 };
5236 struct nfs_setattrres res = {
5237 .fattr = fattr,
5238 .label = olabel,
5239 .server = server,
5240 };
5241 struct rpc_message msg = {
5242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005243 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005244 .rpc_resp = &res,
5245 };
5246 int status;
5247
Jeff Layton12207f62013-11-01 10:49:32 -04005248 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005249
Jeff Layton12207f62013-11-01 10:49:32 -04005250 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005251 if (status)
5252 dprintk("%s failed: %d\n", __func__, status);
5253
5254 return status;
5255}
5256
5257static int nfs4_do_set_security_label(struct inode *inode,
5258 struct nfs4_label *ilabel,
5259 struct nfs_fattr *fattr,
5260 struct nfs4_label *olabel)
5261{
5262 struct nfs4_exception exception = { };
5263 int err;
5264
5265 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005266 err = _nfs4_do_set_security_label(inode, ilabel,
5267 fattr, olabel);
5268 trace_nfs4_set_security_label(inode, err);
5269 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005270 &exception);
5271 } while (exception.retry);
5272 return err;
5273}
5274
5275static int
Al Viro59301222016-05-27 10:19:30 -04005276nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005277{
5278 struct nfs4_label ilabel, *olabel = NULL;
5279 struct nfs_fattr fattr;
5280 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005281 int status;
5282
5283 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5284 return -EOPNOTSUPP;
5285
5286 nfs_fattr_init(&fattr);
5287
5288 ilabel.pi = 0;
5289 ilabel.lfs = 0;
5290 ilabel.label = (char *)buf;
5291 ilabel.len = buflen;
5292
5293 cred = rpc_lookup_cred();
5294 if (IS_ERR(cred))
5295 return PTR_ERR(cred);
5296
5297 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5298 if (IS_ERR(olabel)) {
5299 status = -PTR_ERR(olabel);
5300 goto out;
5301 }
5302
5303 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5304 if (status == 0)
5305 nfs_setsecurity(inode, &fattr, olabel);
5306
5307 nfs4_label_free(olabel);
5308out:
5309 put_rpccred(cred);
5310 return status;
5311}
5312#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5313
5314
Chuck Leverf0920752012-05-21 22:45:41 -04005315static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5316 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005317{
5318 __be32 verf[2];
5319
Chuck Lever2c820d92012-05-21 22:45:33 -04005320 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5321 /* An impossible timestamp guarantees this value
5322 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005323 verf[0] = cpu_to_be32(U32_MAX);
5324 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005325 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005326 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005327 u64 ns = ktime_to_ns(nn->boot_time);
5328
5329 verf[0] = cpu_to_be32(ns >> 32);
5330 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005331 }
Chuck Levercd937102012-03-02 17:14:31 -05005332 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5333}
5334
Jeff Laytona3192682015-06-09 19:43:59 -04005335static int
5336nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005337{
Jeff Laytona3192682015-06-09 19:43:59 -04005338 size_t len;
5339 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005340
Trond Myklebustceb3a162015-01-03 15:16:04 -05005341 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005342 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005343
Jeff Laytona3192682015-06-09 19:43:59 -04005344 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005345 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005346 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5347 1 +
5348 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5349 1;
5350 rcu_read_unlock();
5351
5352 if (len > NFS4_OPAQUE_LIMIT + 1)
5353 return -EINVAL;
5354
5355 /*
5356 * Since this string is allocated at mount time, and held until the
5357 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5358 * about a memory-reclaim deadlock.
5359 */
5360 str = kmalloc(len, GFP_KERNEL);
5361 if (!str)
5362 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005363
Chuck Levere984a552012-09-14 17:24:21 -04005364 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005365 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005366 clp->cl_ipaddr,
5367 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5368 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005369 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005370
Jeff Laytona3192682015-06-09 19:43:59 -04005371 clp->cl_owner_id = str;
5372 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005373}
5374
Jeff Layton873e3852015-06-09 19:44:00 -04005375static int
5376nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005377{
Jeff Layton873e3852015-06-09 19:44:00 -04005378 size_t len;
5379 char *str;
5380
5381 len = 10 + 10 + 1 + 10 + 1 +
5382 strlen(nfs4_client_id_uniquifier) + 1 +
5383 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5384
5385 if (len > NFS4_OPAQUE_LIMIT + 1)
5386 return -EINVAL;
5387
5388 /*
5389 * Since this string is allocated at mount time, and held until the
5390 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5391 * about a memory-reclaim deadlock.
5392 */
5393 str = kmalloc(len, GFP_KERNEL);
5394 if (!str)
5395 return -ENOMEM;
5396
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005397 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005398 clp->rpc_ops->version, clp->cl_minorversion,
5399 nfs4_client_id_uniquifier,
5400 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005401 clp->cl_owner_id = str;
5402 return 0;
5403}
5404
5405static int
5406nfs4_init_uniform_client_string(struct nfs_client *clp)
5407{
Jeff Layton873e3852015-06-09 19:44:00 -04005408 size_t len;
5409 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005410
5411 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005412 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005413
5414 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005415 return nfs4_init_uniquifier_client_string(clp);
5416
5417 len = 10 + 10 + 1 + 10 + 1 +
5418 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5419
5420 if (len > NFS4_OPAQUE_LIMIT + 1)
5421 return -EINVAL;
5422
5423 /*
5424 * Since this string is allocated at mount time, and held until the
5425 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5426 * about a memory-reclaim deadlock.
5427 */
5428 str = kmalloc(len, GFP_KERNEL);
5429 if (!str)
5430 return -ENOMEM;
5431
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005432 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005433 clp->rpc_ops->version, clp->cl_minorversion,
5434 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005435 clp->cl_owner_id = str;
5436 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005437}
5438
Chuck Lever706cb8d2014-03-12 12:51:47 -04005439/*
5440 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5441 * services. Advertise one based on the address family of the
5442 * clientaddr.
5443 */
5444static unsigned int
5445nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5446{
5447 if (strchr(clp->cl_ipaddr, ':') != NULL)
5448 return scnprintf(buf, len, "tcp6");
5449 else
5450 return scnprintf(buf, len, "tcp");
5451}
5452
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005453static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5454{
5455 struct nfs4_setclientid *sc = calldata;
5456
5457 if (task->tk_status == 0)
5458 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5459}
5460
5461static const struct rpc_call_ops nfs4_setclientid_ops = {
5462 .rpc_call_done = nfs4_setclientid_done,
5463};
5464
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005465/**
5466 * nfs4_proc_setclientid - Negotiate client ID
5467 * @clp: state data structure
5468 * @program: RPC program for NFSv4 callback service
5469 * @port: IP port number for NFS4 callback service
5470 * @cred: RPC credential to use for this call
5471 * @res: where to place the result
5472 *
5473 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5474 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005475int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5476 unsigned short port, struct rpc_cred *cred,
5477 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478{
5479 nfs4_verifier sc_verifier;
5480 struct nfs4_setclientid setclientid = {
5481 .sc_verifier = &sc_verifier,
5482 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005483 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 };
5485 struct rpc_message msg = {
5486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5487 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005488 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005489 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005491 struct rpc_task *task;
5492 struct rpc_task_setup task_setup_data = {
5493 .rpc_client = clp->cl_rpcclient,
5494 .rpc_message = &msg,
5495 .callback_ops = &nfs4_setclientid_ops,
5496 .callback_data = &setclientid,
5497 .flags = RPC_TASK_TIMEOUT,
5498 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005499 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Chuck Leverde734832012-07-11 16:30:50 -04005501 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005502 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005503
5504 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5505 status = nfs4_init_uniform_client_string(clp);
5506 else
Jeff Laytona3192682015-06-09 19:43:59 -04005507 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005508
5509 if (status)
5510 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005511
Chuck Leverde734832012-07-11 16:30:50 -04005512 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005513 setclientid.sc_netid_len =
5514 nfs4_init_callback_netid(clp,
5515 setclientid.sc_netid,
5516 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005517 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005518 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 clp->cl_ipaddr, port >> 8, port & 255);
5520
Jeff Layton3a6bb732015-06-09 19:43:57 -04005521 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005522 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005523 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005524 task = rpc_run_task(&task_setup_data);
5525 if (IS_ERR(task)) {
5526 status = PTR_ERR(task);
5527 goto out;
5528 }
5529 status = task->tk_status;
5530 if (setclientid.sc_cred) {
5531 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5532 put_rpccred(setclientid.sc_cred);
5533 }
5534 rpc_put_task(task);
5535out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005536 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005537 dprintk("NFS reply setclientid: %d\n", status);
5538 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539}
5540
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005541/**
5542 * nfs4_proc_setclientid_confirm - Confirm client ID
5543 * @clp: state data structure
5544 * @res: result of a previous SETCLIENTID
5545 * @cred: RPC credential to use for this call
5546 *
5547 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5548 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005549int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005550 struct nfs4_setclientid_res *arg,
5551 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 struct rpc_message msg = {
5554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005555 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005556 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 int status;
5559
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005560 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5561 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5562 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005563 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005564 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005565 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return status;
5567}
5568
Trond Myklebustfe650402006-01-03 09:55:18 +01005569struct nfs4_delegreturndata {
5570 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005571 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005572 struct nfs_fh fh;
5573 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005574 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005575 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005576 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005577 struct inode *inode;
5578 bool roc;
5579 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005580};
5581
Trond Myklebustfe650402006-01-03 09:55:18 +01005582static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5583{
5584 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005585
Trond Myklebust14516c32010-07-31 14:29:06 -04005586 if (!nfs4_sequence_done(task, &data->res.seq_res))
5587 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005588
Trond Myklebustca8acf82013-08-13 10:36:56 -04005589 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005590 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005591 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005592 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005593 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005594 case -NFS4ERR_ADMIN_REVOKED:
5595 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005596 case -NFS4ERR_EXPIRED:
5597 nfs4_free_revoked_stateid(data->res.server,
5598 data->args.stateid,
5599 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005600 case -NFS4ERR_BAD_STATEID:
5601 case -NFS4ERR_OLD_STATEID:
5602 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005603 task->tk_status = 0;
5604 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005605 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005606 if (nfs4_async_handle_error(task, data->res.server,
5607 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005608 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005609 return;
5610 }
5611 }
5612 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005613 if (data->roc && data->rpc_status == 0)
5614 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005615}
5616
5617static void nfs4_delegreturn_release(void *calldata)
5618{
Peng Tao039b7562014-07-03 13:05:02 +08005619 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005620 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005621
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005622 if (inode) {
5623 if (data->roc)
5624 pnfs_roc_release(inode);
5625 nfs_iput_and_deactive(inode);
5626 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005627 kfree(calldata);
5628}
5629
Andy Adamson938e1012009-04-01 09:22:28 -04005630static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5631{
5632 struct nfs4_delegreturndata *d_data;
5633
5634 d_data = (struct nfs4_delegreturndata *)data;
5635
Peng Tao500d7012015-09-22 11:35:22 +08005636 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5637 return;
5638
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005639 if (d_data->roc)
5640 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005641
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005642 nfs4_setup_sequence(d_data->res.server,
5643 &d_data->args.seq_args,
5644 &d_data->res.seq_res,
5645 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005646}
Andy Adamson938e1012009-04-01 09:22:28 -04005647
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005648static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005649 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005650 .rpc_call_done = nfs4_delegreturn_done,
5651 .rpc_release = nfs4_delegreturn_release,
5652};
5653
Trond Myklebuste6f81072008-01-24 18:14:34 -05005654static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005655{
5656 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005657 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005658 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005659 struct rpc_message msg = {
5660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5661 .rpc_cred = cred,
5662 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005663 struct rpc_task_setup task_setup_data = {
5664 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005665 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005666 .callback_ops = &nfs4_delegreturn_ops,
5667 .flags = RPC_TASK_ASYNC,
5668 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005669 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005670
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005671 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005672 if (data == NULL)
5673 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005674 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005675
5676 nfs4_state_protect(server->nfs_client,
5677 NFS_SP4_MACH_CRED_CLEANUP,
5678 &task_setup_data.rpc_client, &msg);
5679
Trond Myklebustfe650402006-01-03 09:55:18 +01005680 data->args.fhandle = &data->fh;
5681 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005682 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005683 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005684 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005685 data->res.fattr = &data->fattr;
5686 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005687 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005688 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005689 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005690 data->inode = nfs_igrab_and_active(inode);
5691 if (data->inode)
5692 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005693
Trond Myklebustc970aa82007-07-14 15:39:59 -04005694 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005695 msg.rpc_argp = &data->args;
5696 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005697 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005698 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005699 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005700 if (!issync)
5701 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005702 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005703 if (status != 0)
5704 goto out;
5705 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005706 if (status == 0)
5707 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5708 else
5709 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005710out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005711 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005712 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713}
5714
Trond Myklebuste6f81072008-01-24 18:14:34 -05005715int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716{
5717 struct nfs_server *server = NFS_SERVER(inode);
5718 struct nfs4_exception exception = { };
5719 int err;
5720 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005721 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005722 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 switch (err) {
5724 case -NFS4ERR_STALE_STATEID:
5725 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 case 0:
5727 return 0;
5728 }
5729 err = nfs4_handle_exception(server, err, &exception);
5730 } while (exception.retry);
5731 return err;
5732}
5733
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5735{
5736 struct inode *inode = state->inode;
5737 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005738 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005739 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005741 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005743 struct nfs_lockt_res res = {
5744 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 };
5746 struct rpc_message msg = {
5747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5748 .rpc_argp = &arg,
5749 .rpc_resp = &res,
5750 .rpc_cred = state->owner->so_cred,
5751 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 struct nfs4_lock_state *lsp;
5753 int status;
5754
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005755 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005756 status = nfs4_set_lock_state(state, request);
5757 if (status != 0)
5758 goto out;
5759 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005760 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005761 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005762 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005763 switch (status) {
5764 case 0:
5765 request->fl_type = F_UNLCK;
5766 break;
5767 case -NFS4ERR_DENIED:
5768 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005770 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005771 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005772out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 return status;
5774}
5775
5776static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5777{
5778 struct nfs4_exception exception = { };
5779 int err;
5780
5781 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005782 err = _nfs4_proc_getlk(state, cmd, request);
5783 trace_nfs4_get_lock(request, state, cmd, err);
5784 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 &exception);
5786 } while (exception.retry);
5787 return err;
5788}
5789
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005790struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005791 struct nfs_locku_args arg;
5792 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005793 struct nfs4_lock_state *lsp;
5794 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005795 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005796 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005797 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005798};
5799
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005800static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5801 struct nfs_open_context *ctx,
5802 struct nfs4_lock_state *lsp,
5803 struct nfs_seqid *seqid)
5804{
5805 struct nfs4_unlockdata *p;
5806 struct inode *inode = lsp->ls_state->inode;
5807
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005808 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005809 if (p == NULL)
5810 return NULL;
5811 p->arg.fh = NFS_FH(inode);
5812 p->arg.fl = &p->fl;
5813 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005814 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005815 p->lsp = lsp;
5816 atomic_inc(&lsp->ls_count);
5817 /* Ensure we don't close file until we're done freeing locks! */
5818 p->ctx = get_nfs_open_context(ctx);
5819 memcpy(&p->fl, fl, sizeof(p->fl));
5820 p->server = NFS_SERVER(inode);
5821 return p;
5822}
5823
Trond Myklebust06f814a2006-01-03 09:55:07 +01005824static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005825{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005826 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005827 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005828 nfs4_put_lock_state(calldata->lsp);
5829 put_nfs_open_context(calldata->ctx);
5830 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005831}
5832
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005833static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005834{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005835 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005836
Trond Myklebust14516c32010-07-31 14:29:06 -04005837 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5838 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005839 switch (task->tk_status) {
5840 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005841 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005842 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005843 if (nfs4_update_lock_stateid(calldata->lsp,
5844 &calldata->res.stateid))
5845 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005846 case -NFS4ERR_ADMIN_REVOKED:
5847 case -NFS4ERR_EXPIRED:
5848 nfs4_free_revoked_stateid(calldata->server,
5849 &calldata->arg.stateid,
5850 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005851 case -NFS4ERR_BAD_STATEID:
5852 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005853 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005854 if (!nfs4_stateid_match(&calldata->arg.stateid,
5855 &calldata->lsp->ls_stateid))
5856 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005857 break;
5858 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005859 if (nfs4_async_handle_error(task, calldata->server,
5860 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005861 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005862 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005863 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005864}
5865
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005866static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005867{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005868 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005870 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005871 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005872 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005873 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005874 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005875 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005876 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005877 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005878 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005879 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005880 &calldata->res.seq_res,
5881 task) != 0)
5882 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005883 return;
5884out_no_action:
5885 task->tk_action = NULL;
5886out_wait:
5887 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888}
5889
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005890static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005891 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005892 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005893 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005894};
5895
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005896static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5897 struct nfs_open_context *ctx,
5898 struct nfs4_lock_state *lsp,
5899 struct nfs_seqid *seqid)
5900{
5901 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005902 struct rpc_message msg = {
5903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5904 .rpc_cred = ctx->cred,
5905 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005906 struct rpc_task_setup task_setup_data = {
5907 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005908 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005909 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005910 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005911 .flags = RPC_TASK_ASYNC,
5912 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005913
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005914 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5915 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5916
Frank Filz137d6ac2007-07-09 15:32:29 -07005917 /* Ensure this is an unlock - when canceling a lock, the
5918 * canceled lock is passed in, and it won't be an unlock.
5919 */
5920 fl->fl_type = F_UNLCK;
5921
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005922 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5923 if (data == NULL) {
5924 nfs_free_seqid(seqid);
5925 return ERR_PTR(-ENOMEM);
5926 }
5927
Chuck Levera9c92d62013-08-09 12:48:18 -04005928 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005929 msg.rpc_argp = &data->arg;
5930 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005931 task_setup_data.callback_data = data;
5932 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005933}
5934
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5936{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005937 struct inode *inode = state->inode;
5938 struct nfs4_state_owner *sp = state->owner;
5939 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005940 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005941 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005942 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005943 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005944 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005945 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
Trond Myklebust9b073572006-06-29 16:38:34 -04005947 status = nfs4_set_lock_state(state, request);
5948 /* Unlock _before_ we do the RPC call */
5949 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005950 /* Exclude nfs_delegation_claim_locks() */
5951 mutex_lock(&sp->so_delegreturn_mutex);
5952 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005953 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005954 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005955 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005956 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005957 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005958 }
5959 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005960 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005961 if (status != 0)
5962 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005963 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005964 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005965 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5966 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005967 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5968 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005969 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005970 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005971 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005972 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005973 status = PTR_ERR(task);
5974 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005975 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005977 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005978out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005979 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005980 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005981 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982}
5983
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005984struct nfs4_lockdata {
5985 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005986 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005987 struct nfs4_lock_state *lsp;
5988 struct nfs_open_context *ctx;
5989 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005990 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005991 int rpc_status;
5992 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005993 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005994};
5995
5996static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005997 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5998 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999{
6000 struct nfs4_lockdata *p;
6001 struct inode *inode = lsp->ls_state->inode;
6002 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006003 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006005 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006006 if (p == NULL)
6007 return NULL;
6008
6009 p->arg.fh = NFS_FH(inode);
6010 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006011 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006012 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006013 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006014 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6015 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006016 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006017 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006018 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006019 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006020 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006021 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006022 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006023 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006024 atomic_inc(&lsp->ls_count);
6025 p->ctx = get_nfs_open_context(ctx);
6026 memcpy(&p->fl, fl, sizeof(p->fl));
6027 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006028out_free_seqid:
6029 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006030out_free:
6031 kfree(p);
6032 return NULL;
6033}
6034
6035static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6036{
6037 struct nfs4_lockdata *data = calldata;
6038 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Harvey Harrison3110ff82008-05-02 13:42:44 -07006040 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006041 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006042 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006043 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006044 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006045 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006046 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006047 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006048 nfs4_stateid_copy(&data->arg.open_stateid,
6049 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006050 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006051 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006052 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006053 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006054 nfs4_stateid_copy(&data->arg.lock_stateid,
6055 &data->lsp->ls_stateid);
6056 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006057 if (!nfs4_valid_open_stateid(state)) {
6058 data->rpc_status = -EBADF;
6059 task->tk_action = NULL;
6060 goto out_release_open_seqid;
6061 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006062 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006063 if (nfs4_setup_sequence(data->server,
6064 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006065 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006066 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006067 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006068out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006069 nfs_release_seqid(data->arg.open_seqid);
6070out_release_lock_seqid:
6071 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006072out_wait:
6073 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006074 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006075}
6076
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006077static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6078{
6079 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006080 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006081
Harvey Harrison3110ff82008-05-02 13:42:44 -07006082 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006083
Trond Myklebust14516c32010-07-31 14:29:06 -04006084 if (!nfs4_sequence_done(task, &data->res.seq_res))
6085 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006086
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006088 switch (task->tk_status) {
6089 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006090 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006091 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006092 if (data->arg.new_lock) {
6093 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006094 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006095 rpc_restart_call_prepare(task);
6096 break;
6097 }
6098 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006099 if (data->arg.new_lock_owner != 0) {
6100 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6101 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6102 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6103 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6104 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006105 break;
6106 case -NFS4ERR_BAD_STATEID:
6107 case -NFS4ERR_OLD_STATEID:
6108 case -NFS4ERR_STALE_STATEID:
6109 case -NFS4ERR_EXPIRED:
6110 if (data->arg.new_lock_owner != 0) {
6111 if (!nfs4_stateid_match(&data->arg.open_stateid,
6112 &lsp->ls_state->open_stateid))
6113 rpc_restart_call_prepare(task);
6114 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6115 &lsp->ls_stateid))
6116 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006117 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006118 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006119}
6120
6121static void nfs4_lock_release(void *calldata)
6122{
6123 struct nfs4_lockdata *data = calldata;
6124
Harvey Harrison3110ff82008-05-02 13:42:44 -07006125 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006126 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006127 if (data->cancelled != 0) {
6128 struct rpc_task *task;
6129 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6130 data->arg.lock_seqid);
6131 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006132 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006133 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006134 } else
6135 nfs_free_seqid(data->arg.lock_seqid);
6136 nfs4_put_lock_state(data->lsp);
6137 put_nfs_open_context(data->ctx);
6138 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006139 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006140}
6141
6142static const struct rpc_call_ops nfs4_lock_ops = {
6143 .rpc_call_prepare = nfs4_lock_prepare,
6144 .rpc_call_done = nfs4_lock_done,
6145 .rpc_release = nfs4_lock_release,
6146};
6147
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006148static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6149{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006150 switch (error) {
6151 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006152 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006153 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006154 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006155 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006156 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006157 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006158 break;
6159 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006160 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006161 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006162 };
6163}
6164
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006165static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006166{
6167 struct nfs4_lockdata *data;
6168 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006169 struct rpc_message msg = {
6170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6171 .rpc_cred = state->owner->so_cred,
6172 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006173 struct rpc_task_setup task_setup_data = {
6174 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006175 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006176 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006177 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006178 .flags = RPC_TASK_ASYNC,
6179 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006180 int ret;
6181
Harvey Harrison3110ff82008-05-02 13:42:44 -07006182 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006183 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006184 fl->fl_u.nfs4_fl.owner,
6185 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006186 if (data == NULL)
6187 return -ENOMEM;
6188 if (IS_SETLKW(cmd))
6189 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006190 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006191 msg.rpc_argp = &data->arg;
6192 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006193 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006194 if (recovery_type > NFS_LOCK_NEW) {
6195 if (recovery_type == NFS_LOCK_RECLAIM)
6196 data->arg.reclaim = NFS_LOCK_RECLAIM;
6197 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006198 } else
6199 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006200 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006201 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006203 ret = nfs4_wait_for_completion_rpc_task(task);
6204 if (ret == 0) {
6205 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006206 if (ret)
6207 nfs4_handle_setlk_error(data->server, data->lsp,
6208 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006209 } else
6210 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006211 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006212 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006213 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006214 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215}
6216
6217static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6218{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006219 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006220 struct nfs4_exception exception = {
6221 .inode = state->inode,
6222 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006223 int err;
6224
6225 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006226 /* Cache the lock if possible... */
6227 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6228 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006229 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006230 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006231 break;
6232 nfs4_handle_exception(server, err, &exception);
6233 } while (exception.retry);
6234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235}
6236
6237static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6238{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006239 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006240 struct nfs4_exception exception = {
6241 .inode = state->inode,
6242 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006243 int err;
6244
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006245 err = nfs4_set_lock_state(state, request);
6246 if (err != 0)
6247 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006248 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006249 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6250 return 0;
6251 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006252 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006253 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6254 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006255 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006256 switch (err) {
6257 default:
6258 goto out;
6259 case -NFS4ERR_GRACE:
6260 case -NFS4ERR_DELAY:
6261 nfs4_handle_exception(server, err, &exception);
6262 err = 0;
6263 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006264 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006265out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267}
6268
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006269#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006270static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6271{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006272 struct nfs4_lock_state *lsp;
6273 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006274
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006275 status = nfs4_set_lock_state(state, request);
6276 if (status != 0)
6277 return status;
6278 lsp = request->fl_u.nfs4_fl.owner;
6279 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6280 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6281 return 0;
6282 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006283 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006284}
6285#endif
6286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6288{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006289 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006290 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006291 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006292 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Trond Myklebust01c3b862006-06-29 16:38:39 -04006294 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006295 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006296 if (status < 0)
6297 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006298 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006299 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006300 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006301 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006302 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006303 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006304 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006305 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006306 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006307 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006308 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006309 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006310 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006311 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006312out:
6313 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 return status;
6315}
6316
6317static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6318{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006319 struct nfs4_exception exception = {
6320 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006321 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006322 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 int err;
6324
6325 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006326 err = _nfs4_proc_setlk(state, cmd, request);
6327 if (err == -NFS4ERR_DENIED)
6328 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006330 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 } while (exception.retry);
6332 return err;
6333}
6334
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006335#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6336#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6337
6338static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006339nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6340 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006341{
6342 int status = -ERESTARTSYS;
6343 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6344
6345 while(!signalled()) {
6346 status = nfs4_proc_setlk(state, cmd, request);
6347 if ((status != -EAGAIN) || IS_SETLK(cmd))
6348 break;
6349 freezable_schedule_timeout_interruptible(timeout);
6350 timeout *= 2;
6351 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6352 status = -ERESTARTSYS;
6353 }
6354 return status;
6355}
6356
Jeff Laytona1d617d82016-09-17 18:17:39 -04006357#ifdef CONFIG_NFS_V4_1
6358struct nfs4_lock_waiter {
6359 struct task_struct *task;
6360 struct inode *inode;
6361 struct nfs_lowner *owner;
6362 bool notified;
6363};
6364
6365static int
6366nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6367{
6368 int ret;
6369 struct cb_notify_lock_args *cbnl = key;
6370 struct nfs4_lock_waiter *waiter = wait->private;
6371 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6372 *wowner = waiter->owner;
6373
6374 /* Only wake if the callback was for the same owner */
6375 if (lowner->clientid != wowner->clientid ||
6376 lowner->id != wowner->id ||
6377 lowner->s_dev != wowner->s_dev)
6378 return 0;
6379
6380 /* Make sure it's for the right inode */
6381 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6382 return 0;
6383
6384 waiter->notified = true;
6385
6386 /* override "private" so we can use default_wake_function */
6387 wait->private = waiter->task;
6388 ret = autoremove_wake_function(wait, mode, flags, key);
6389 wait->private = waiter;
6390 return ret;
6391}
6392
6393static int
6394nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6395{
6396 int status = -ERESTARTSYS;
6397 unsigned long flags;
6398 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6399 struct nfs_server *server = NFS_SERVER(state->inode);
6400 struct nfs_client *clp = server->nfs_client;
6401 wait_queue_head_t *q = &clp->cl_lock_waitq;
6402 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6403 .id = lsp->ls_seqid.owner_id,
6404 .s_dev = server->s_dev };
6405 struct nfs4_lock_waiter waiter = { .task = current,
6406 .inode = state->inode,
6407 .owner = &owner,
6408 .notified = false };
6409 wait_queue_t wait;
6410
6411 /* Don't bother with waitqueue if we don't expect a callback */
6412 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6413 return nfs4_retry_setlk_simple(state, cmd, request);
6414
6415 init_wait(&wait);
6416 wait.private = &waiter;
6417 wait.func = nfs4_wake_lock_waiter;
6418 add_wait_queue(q, &wait);
6419
6420 while(!signalled()) {
6421 status = nfs4_proc_setlk(state, cmd, request);
6422 if ((status != -EAGAIN) || IS_SETLK(cmd))
6423 break;
6424
6425 status = -ERESTARTSYS;
6426 spin_lock_irqsave(&q->lock, flags);
6427 if (waiter.notified) {
6428 spin_unlock_irqrestore(&q->lock, flags);
6429 continue;
6430 }
6431 set_current_state(TASK_INTERRUPTIBLE);
6432 spin_unlock_irqrestore(&q->lock, flags);
6433
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006434 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006435 }
6436
6437 finish_wait(q, &wait);
6438 return status;
6439}
6440#else /* !CONFIG_NFS_V4_1 */
6441static inline int
6442nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6443{
6444 return nfs4_retry_setlk_simple(state, cmd, request);
6445}
6446#endif
6447
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448static int
6449nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6450{
6451 struct nfs_open_context *ctx;
6452 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 int status;
6454
6455 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006456 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 state = ctx->state;
6458
6459 if (request->fl_start < 0 || request->fl_end < 0)
6460 return -EINVAL;
6461
Trond Myklebustd9531262009-07-21 19:22:38 -04006462 if (IS_GETLK(cmd)) {
6463 if (state != NULL)
6464 return nfs4_proc_getlk(state, F_GETLK, request);
6465 return 0;
6466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
6468 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6469 return -EINVAL;
6470
Trond Myklebustd9531262009-07-21 19:22:38 -04006471 if (request->fl_type == F_UNLCK) {
6472 if (state != NULL)
6473 return nfs4_proc_unlck(state, cmd, request);
6474 return 0;
6475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476
Trond Myklebustd9531262009-07-21 19:22:38 -04006477 if (state == NULL)
6478 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006479
6480 if ((request->fl_flags & FL_POSIX) &&
6481 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6482 return -ENOLCK;
6483
Trond Myklebust55725512012-04-18 12:48:35 -04006484 /*
6485 * Don't rely on the VFS having checked the file open mode,
6486 * since it won't do this for flock() locks.
6487 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006488 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006489 case F_RDLCK:
6490 if (!(filp->f_mode & FMODE_READ))
6491 return -EBADF;
6492 break;
6493 case F_WRLCK:
6494 if (!(filp->f_mode & FMODE_WRITE))
6495 return -EBADF;
6496 }
6497
Jeff Layton1ea67db2016-09-17 18:17:37 -04006498 status = nfs4_set_lock_state(state, request);
6499 if (status != 0)
6500 return status;
6501
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006502 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503}
6504
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006505int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006506{
6507 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006508 int err;
6509
6510 err = nfs4_set_lock_state(state, fl);
6511 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006512 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006513 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrown70f254a2017-12-13 09:57:09 +11006514 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006515}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006516
Trond Myklebustcf470c32012-03-07 13:49:12 -05006517struct nfs_release_lockowner_data {
6518 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006519 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006520 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006521 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006522 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006523};
6524
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006525static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6526{
6527 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006528 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006529 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6530 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006531 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006532 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006533}
6534
6535static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6536{
6537 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006538 struct nfs_server *server = data->server;
6539
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006540 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006541
6542 switch (task->tk_status) {
6543 case 0:
6544 renew_lease(server, data->timestamp);
6545 break;
6546 case -NFS4ERR_STALE_CLIENTID:
6547 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006548 nfs4_schedule_lease_recovery(server->nfs_client);
6549 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006550 case -NFS4ERR_LEASE_MOVED:
6551 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006552 if (nfs4_async_handle_error(task, server,
6553 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006554 rpc_restart_call_prepare(task);
6555 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006556}
6557
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006558static void nfs4_release_lockowner_release(void *calldata)
6559{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006560 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006561 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006562 kfree(calldata);
6563}
6564
Trond Myklebust17280172012-03-11 13:11:00 -04006565static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006566 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6567 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006568 .rpc_release = nfs4_release_lockowner_release,
6569};
6570
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006571static void
6572nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006573{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006574 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006575 struct rpc_message msg = {
6576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6577 };
6578
6579 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006580 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006581
Trond Myklebustcf470c32012-03-07 13:49:12 -05006582 data = kmalloc(sizeof(*data), GFP_NOFS);
6583 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006584 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006585 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006586 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006587 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6588 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6589 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006590
Trond Myklebustcf470c32012-03-07 13:49:12 -05006591 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006592 msg.rpc_resp = &data->res;
6593 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006594 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006595}
6596
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006597#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6598
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006599static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006600 struct dentry *unused, struct inode *inode,
6601 const char *key, const void *buf,
6602 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006603{
Al Viro59301222016-05-27 10:19:30 -04006604 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006605}
6606
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006607static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006608 struct dentry *unused, struct inode *inode,
6609 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006610{
Al Virob2968212016-04-10 20:48:24 -04006611 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006612}
6613
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006614static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006615{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006616 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006617}
6618
David Quigleyc9bccef2013-05-22 12:50:45 -04006619#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006620
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006621static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006622 struct dentry *unused, struct inode *inode,
6623 const char *key, const void *buf,
6624 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006625{
6626 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006627 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006628
6629 return -EOPNOTSUPP;
6630}
6631
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006632static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006633 struct dentry *unused, struct inode *inode,
6634 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006635{
6636 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006637 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006638 return -EOPNOTSUPP;
6639}
6640
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006641static ssize_t
6642nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006643{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006644 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006645
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006646 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6647 len = security_inode_listsecurity(inode, list, list_len);
6648 if (list_len && len > list_len)
6649 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006650 }
6651 return len;
6652}
6653
6654static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6655 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006656 .get = nfs4_xattr_get_nfs4_label,
6657 .set = nfs4_xattr_set_nfs4_label,
6658};
David Quigleyc9bccef2013-05-22 12:50:45 -04006659
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006660#else
6661
6662static ssize_t
6663nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6664{
6665 return 0;
6666}
6667
6668#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006669
Andy Adamson533eb462011-06-13 18:25:56 -04006670/*
6671 * nfs_fhget will use either the mounted_on_fileid or the fileid
6672 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006673static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6674{
Andy Adamson533eb462011-06-13 18:25:56 -04006675 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6676 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6677 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006678 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006679 return;
6680
6681 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006682 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006683 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6684 fattr->nlink = 2;
6685}
6686
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006687static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6688 const struct qstr *name,
6689 struct nfs4_fs_locations *fs_locations,
6690 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006691{
6692 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006693 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006694 struct nfs4_fs_locations_arg args = {
6695 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006696 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006697 .page = page,
6698 .bitmask = bitmask,
6699 };
Benny Halevy22958462009-04-01 09:22:02 -04006700 struct nfs4_fs_locations_res res = {
6701 .fs_locations = fs_locations,
6702 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006703 struct rpc_message msg = {
6704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6705 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006706 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006707 };
6708 int status;
6709
Harvey Harrison3110ff82008-05-02 13:42:44 -07006710 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006711
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006712 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6713 bitmask[1] = nfs4_fattr_bitmap[1];
6714
Andy Adamson533eb462011-06-13 18:25:56 -04006715 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6716 * is not supported */
6717 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006718 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006719 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006720 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006721
Trond Myklebustc228fd32007-01-13 02:28:11 -05006722 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006723 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006724 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006725 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006726 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006727 return status;
6728}
6729
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006730int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6731 const struct qstr *name,
6732 struct nfs4_fs_locations *fs_locations,
6733 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006734{
6735 struct nfs4_exception exception = { };
6736 int err;
6737 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006738 err = _nfs4_proc_fs_locations(client, dir, name,
6739 fs_locations, page);
6740 trace_nfs4_get_fs_locations(dir, name, err);
6741 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006742 &exception);
6743 } while (exception.retry);
6744 return err;
6745}
6746
Chuck Leverb03d7352013-10-17 14:12:50 -04006747/*
6748 * This operation also signals the server that this client is
6749 * performing migration recovery. The server can stop returning
6750 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6751 * appended to this compound to identify the client ID which is
6752 * performing recovery.
6753 */
6754static int _nfs40_proc_get_locations(struct inode *inode,
6755 struct nfs4_fs_locations *locations,
6756 struct page *page, struct rpc_cred *cred)
6757{
6758 struct nfs_server *server = NFS_SERVER(inode);
6759 struct rpc_clnt *clnt = server->client;
6760 u32 bitmask[2] = {
6761 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6762 };
6763 struct nfs4_fs_locations_arg args = {
6764 .clientid = server->nfs_client->cl_clientid,
6765 .fh = NFS_FH(inode),
6766 .page = page,
6767 .bitmask = bitmask,
6768 .migration = 1, /* skip LOOKUP */
6769 .renew = 1, /* append RENEW */
6770 };
6771 struct nfs4_fs_locations_res res = {
6772 .fs_locations = locations,
6773 .migration = 1,
6774 .renew = 1,
6775 };
6776 struct rpc_message msg = {
6777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6778 .rpc_argp = &args,
6779 .rpc_resp = &res,
6780 .rpc_cred = cred,
6781 };
6782 unsigned long now = jiffies;
6783 int status;
6784
6785 nfs_fattr_init(&locations->fattr);
6786 locations->server = server;
6787 locations->nlocations = 0;
6788
6789 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6790 nfs4_set_sequence_privileged(&args.seq_args);
6791 status = nfs4_call_sync_sequence(clnt, server, &msg,
6792 &args.seq_args, &res.seq_res);
6793 if (status)
6794 return status;
6795
6796 renew_lease(server, now);
6797 return 0;
6798}
6799
6800#ifdef CONFIG_NFS_V4_1
6801
6802/*
6803 * This operation also signals the server that this client is
6804 * performing migration recovery. The server can stop asserting
6805 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6806 * performing this operation is identified in the SEQUENCE
6807 * operation in this compound.
6808 *
6809 * When the client supports GETATTR(fs_locations_info), it can
6810 * be plumbed in here.
6811 */
6812static int _nfs41_proc_get_locations(struct inode *inode,
6813 struct nfs4_fs_locations *locations,
6814 struct page *page, struct rpc_cred *cred)
6815{
6816 struct nfs_server *server = NFS_SERVER(inode);
6817 struct rpc_clnt *clnt = server->client;
6818 u32 bitmask[2] = {
6819 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6820 };
6821 struct nfs4_fs_locations_arg args = {
6822 .fh = NFS_FH(inode),
6823 .page = page,
6824 .bitmask = bitmask,
6825 .migration = 1, /* skip LOOKUP */
6826 };
6827 struct nfs4_fs_locations_res res = {
6828 .fs_locations = locations,
6829 .migration = 1,
6830 };
6831 struct rpc_message msg = {
6832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6833 .rpc_argp = &args,
6834 .rpc_resp = &res,
6835 .rpc_cred = cred,
6836 };
6837 int status;
6838
6839 nfs_fattr_init(&locations->fattr);
6840 locations->server = server;
6841 locations->nlocations = 0;
6842
6843 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6844 nfs4_set_sequence_privileged(&args.seq_args);
6845 status = nfs4_call_sync_sequence(clnt, server, &msg,
6846 &args.seq_args, &res.seq_res);
6847 if (status == NFS4_OK &&
6848 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6849 status = -NFS4ERR_LEASE_MOVED;
6850 return status;
6851}
6852
6853#endif /* CONFIG_NFS_V4_1 */
6854
6855/**
6856 * nfs4_proc_get_locations - discover locations for a migrated FSID
6857 * @inode: inode on FSID that is migrating
6858 * @locations: result of query
6859 * @page: buffer
6860 * @cred: credential to use for this operation
6861 *
6862 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6863 * operation failed, or a negative errno if a local error occurred.
6864 *
6865 * On success, "locations" is filled in, but if the server has
6866 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6867 * asserted.
6868 *
6869 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6870 * from this client that require migration recovery.
6871 */
6872int nfs4_proc_get_locations(struct inode *inode,
6873 struct nfs4_fs_locations *locations,
6874 struct page *page, struct rpc_cred *cred)
6875{
6876 struct nfs_server *server = NFS_SERVER(inode);
6877 struct nfs_client *clp = server->nfs_client;
6878 const struct nfs4_mig_recovery_ops *ops =
6879 clp->cl_mvops->mig_recovery_ops;
6880 struct nfs4_exception exception = { };
6881 int status;
6882
6883 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6884 (unsigned long long)server->fsid.major,
6885 (unsigned long long)server->fsid.minor,
6886 clp->cl_hostname);
6887 nfs_display_fhandle(NFS_FH(inode), __func__);
6888
6889 do {
6890 status = ops->get_locations(inode, locations, page, cred);
6891 if (status != -NFS4ERR_DELAY)
6892 break;
6893 nfs4_handle_exception(server, status, &exception);
6894 } while (exception.retry);
6895 return status;
6896}
6897
Chuck Lever44c99932013-10-17 14:13:30 -04006898/*
6899 * This operation also signals the server that this client is
6900 * performing "lease moved" recovery. The server can stop
6901 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6902 * is appended to this compound to identify the client ID which is
6903 * performing recovery.
6904 */
6905static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6906{
6907 struct nfs_server *server = NFS_SERVER(inode);
6908 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6909 struct rpc_clnt *clnt = server->client;
6910 struct nfs4_fsid_present_arg args = {
6911 .fh = NFS_FH(inode),
6912 .clientid = clp->cl_clientid,
6913 .renew = 1, /* append RENEW */
6914 };
6915 struct nfs4_fsid_present_res res = {
6916 .renew = 1,
6917 };
6918 struct rpc_message msg = {
6919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6920 .rpc_argp = &args,
6921 .rpc_resp = &res,
6922 .rpc_cred = cred,
6923 };
6924 unsigned long now = jiffies;
6925 int status;
6926
6927 res.fh = nfs_alloc_fhandle();
6928 if (res.fh == NULL)
6929 return -ENOMEM;
6930
6931 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6932 nfs4_set_sequence_privileged(&args.seq_args);
6933 status = nfs4_call_sync_sequence(clnt, server, &msg,
6934 &args.seq_args, &res.seq_res);
6935 nfs_free_fhandle(res.fh);
6936 if (status)
6937 return status;
6938
6939 do_renew_lease(clp, now);
6940 return 0;
6941}
6942
6943#ifdef CONFIG_NFS_V4_1
6944
6945/*
6946 * This operation also signals the server that this client is
6947 * performing "lease moved" recovery. The server can stop asserting
6948 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6949 * this operation is identified in the SEQUENCE operation in this
6950 * compound.
6951 */
6952static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6953{
6954 struct nfs_server *server = NFS_SERVER(inode);
6955 struct rpc_clnt *clnt = server->client;
6956 struct nfs4_fsid_present_arg args = {
6957 .fh = NFS_FH(inode),
6958 };
6959 struct nfs4_fsid_present_res res = {
6960 };
6961 struct rpc_message msg = {
6962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6963 .rpc_argp = &args,
6964 .rpc_resp = &res,
6965 .rpc_cred = cred,
6966 };
6967 int status;
6968
6969 res.fh = nfs_alloc_fhandle();
6970 if (res.fh == NULL)
6971 return -ENOMEM;
6972
6973 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6974 nfs4_set_sequence_privileged(&args.seq_args);
6975 status = nfs4_call_sync_sequence(clnt, server, &msg,
6976 &args.seq_args, &res.seq_res);
6977 nfs_free_fhandle(res.fh);
6978 if (status == NFS4_OK &&
6979 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6980 status = -NFS4ERR_LEASE_MOVED;
6981 return status;
6982}
6983
6984#endif /* CONFIG_NFS_V4_1 */
6985
6986/**
6987 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6988 * @inode: inode on FSID to check
6989 * @cred: credential to use for this operation
6990 *
6991 * Server indicates whether the FSID is present, moved, or not
6992 * recognized. This operation is necessary to clear a LEASE_MOVED
6993 * condition for this client ID.
6994 *
6995 * Returns NFS4_OK if the FSID is present on this server,
6996 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6997 * NFS4ERR code if some error occurred on the server, or a
6998 * negative errno if a local failure occurred.
6999 */
7000int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7001{
7002 struct nfs_server *server = NFS_SERVER(inode);
7003 struct nfs_client *clp = server->nfs_client;
7004 const struct nfs4_mig_recovery_ops *ops =
7005 clp->cl_mvops->mig_recovery_ops;
7006 struct nfs4_exception exception = { };
7007 int status;
7008
7009 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7010 (unsigned long long)server->fsid.major,
7011 (unsigned long long)server->fsid.minor,
7012 clp->cl_hostname);
7013 nfs_display_fhandle(NFS_FH(inode), __func__);
7014
7015 do {
7016 status = ops->fsid_present(inode, cred);
7017 if (status != -NFS4ERR_DELAY)
7018 break;
7019 nfs4_handle_exception(server, status, &exception);
7020 } while (exception.retry);
7021 return status;
7022}
7023
Andy Adamson5ec16a82013-08-08 10:57:55 -04007024/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007025 * If 'use_integrity' is true and the state managment nfs_client
7026 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7027 * and the machine credential as per RFC3530bis and RFC5661 Security
7028 * Considerations sections. Otherwise, just use the user cred with the
7029 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007030 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007031static 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 +00007032{
7033 int status;
7034 struct nfs4_secinfo_arg args = {
7035 .dir_fh = NFS_FH(dir),
7036 .name = name,
7037 };
7038 struct nfs4_secinfo_res res = {
7039 .flavors = flavors,
7040 };
7041 struct rpc_message msg = {
7042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7043 .rpc_argp = &args,
7044 .rpc_resp = &res,
7045 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007046 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007047 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007048
7049 if (use_integrity) {
7050 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007051 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7052 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007053 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007054
7055 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007056
7057 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7058 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7059
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007060 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7061 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007062 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007063
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007064 if (cred)
7065 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007066
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007067 return status;
7068}
7069
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007070int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7071 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007072{
7073 struct nfs4_exception exception = { };
7074 int err;
7075 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007076 err = -NFS4ERR_WRONGSEC;
7077
7078 /* try to use integrity protection with machine cred */
7079 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7080 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7081
7082 /*
7083 * if unable to use integrity protection, or SECINFO with
7084 * integrity protection returns NFS4ERR_WRONGSEC (which is
7085 * disallowed by spec, but exists in deployed servers) use
7086 * the current filesystem's rpc_client and the user cred.
7087 */
7088 if (err == -NFS4ERR_WRONGSEC)
7089 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7090
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007091 trace_nfs4_secinfo(dir, name, err);
7092 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007093 &exception);
7094 } while (exception.retry);
7095 return err;
7096}
7097
Andy Adamson557134a2009-04-01 09:21:53 -04007098#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007099/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007100 * Check the exchange flags returned by the server for invalid flags, having
7101 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7102 * DS flags set.
7103 */
7104static int nfs4_check_cl_exchange_flags(u32 flags)
7105{
7106 if (flags & ~EXCHGID4_FLAG_MASK_R)
7107 goto out_inval;
7108 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7109 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7110 goto out_inval;
7111 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7112 goto out_inval;
7113 return NFS_OK;
7114out_inval:
7115 return -NFS4ERR_INVAL;
7116}
7117
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007118static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007119nfs41_same_server_scope(struct nfs41_server_scope *a,
7120 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007121{
7122 if (a->server_scope_sz == b->server_scope_sz &&
7123 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7124 return true;
7125
7126 return false;
7127}
7128
Andy Adamson02a95de2016-02-05 16:08:37 -05007129static void
7130nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7131{
7132}
7133
7134static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7135 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7136};
7137
Andy Adamson357f54d2010-12-14 10:11:57 -05007138/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007139 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007140 *
7141 * The 4.1 client currently uses the same TCP connection for the
7142 * fore and backchannel.
7143 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007144static
7145int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7146 struct rpc_xprt *xprt,
7147 struct nfs_client *clp,
7148 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007149{
7150 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007151 struct nfs41_bind_conn_to_session_args args = {
7152 .client = clp,
7153 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7154 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007155 struct nfs41_bind_conn_to_session_res res;
7156 struct rpc_message msg = {
7157 .rpc_proc =
7158 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007159 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007160 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007161 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007162 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007163 struct rpc_task_setup task_setup_data = {
7164 .rpc_client = clnt,
7165 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007166 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007167 .rpc_message = &msg,
7168 .flags = RPC_TASK_TIMEOUT,
7169 };
7170 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007171
7172 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007173
Trond Myklebust71a097c2015-02-18 09:27:18 -08007174 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7175 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7176 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007178 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7179 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7180 args.dir = NFS4_CDFC4_FORE;
7181
7182 task = rpc_run_task(&task_setup_data);
7183 if (!IS_ERR(task)) {
7184 status = task->tk_status;
7185 rpc_put_task(task);
7186 } else
7187 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007188 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007189 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007190 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007191 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7192 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7193 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007194 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007195 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007196 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007197 dprintk("NFS: %s: Unexpected direction from server\n",
7198 __func__);
7199 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007200 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007201 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007202 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007203 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7204 __func__);
7205 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007206 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007207 }
7208 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007209out:
7210 dprintk("<-- %s status= %d\n", __func__, status);
7211 return status;
7212}
7213
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007214struct rpc_bind_conn_calldata {
7215 struct nfs_client *clp;
7216 struct rpc_cred *cred;
7217};
7218
7219static int
7220nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7221 struct rpc_xprt *xprt,
7222 void *calldata)
7223{
7224 struct rpc_bind_conn_calldata *p = calldata;
7225
7226 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7227}
7228
7229int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7230{
7231 struct rpc_bind_conn_calldata data = {
7232 .clp = clp,
7233 .cred = cred,
7234 };
7235 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7236 nfs4_proc_bind_conn_to_session_callback, &data);
7237}
7238
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007239/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007240 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7241 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007242 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007243static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7244 .how = SP4_MACH_CRED,
7245 .enforce.u.words = {
7246 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7247 1 << (OP_EXCHANGE_ID - 32) |
7248 1 << (OP_CREATE_SESSION - 32) |
7249 1 << (OP_DESTROY_SESSION - 32) |
7250 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007251 },
7252 .allow.u.words = {
7253 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007254 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007255 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007256 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007257 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007258 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007259 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007260 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007261 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007262 1 << (OP_FREE_STATEID - 32) |
7263 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007264 }
7265};
7266
7267/*
7268 * Select the state protection mode for client `clp' given the server results
7269 * from exchange_id in `sp'.
7270 *
7271 * Returns 0 on success, negative errno otherwise.
7272 */
7273static int nfs4_sp4_select_mode(struct nfs_client *clp,
7274 struct nfs41_state_protection *sp)
7275{
7276 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7277 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7278 1 << (OP_EXCHANGE_ID - 32) |
7279 1 << (OP_CREATE_SESSION - 32) |
7280 1 << (OP_DESTROY_SESSION - 32) |
7281 1 << (OP_DESTROY_CLIENTID - 32)
7282 };
7283 unsigned int i;
7284
7285 if (sp->how == SP4_MACH_CRED) {
7286 /* Print state protect result */
7287 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7288 for (i = 0; i <= LAST_NFS4_OP; i++) {
7289 if (test_bit(i, sp->enforce.u.longs))
7290 dfprintk(MOUNT, " enforce op %d\n", i);
7291 if (test_bit(i, sp->allow.u.longs))
7292 dfprintk(MOUNT, " allow op %d\n", i);
7293 }
7294
7295 /* make sure nothing is on enforce list that isn't supported */
7296 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7297 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7298 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7299 return -EINVAL;
7300 }
7301 }
7302
7303 /*
7304 * Minimal mode - state operations are allowed to use machine
7305 * credential. Note this already happens by default, so the
7306 * client doesn't have to do anything more than the negotiation.
7307 *
7308 * NOTE: we don't care if EXCHANGE_ID is in the list -
7309 * we're already using the machine cred for exchange_id
7310 * and will never use a different cred.
7311 */
7312 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7313 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7314 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7315 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7316 dfprintk(MOUNT, "sp4_mach_cred:\n");
7317 dfprintk(MOUNT, " minimal mode enabled\n");
7318 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7319 } else {
7320 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7321 return -EINVAL;
7322 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007323
7324 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007325 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7326 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007327 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7328 dfprintk(MOUNT, " cleanup mode enabled\n");
7329 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7330 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007331
Andrew Elble99ade3c2015-12-02 09:39:51 -05007332 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7333 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7334 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7335 &clp->cl_sp4_flags);
7336 }
7337
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007338 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7339 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7340 dfprintk(MOUNT, " secinfo mode enabled\n");
7341 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7342 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007343
7344 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7345 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7346 dfprintk(MOUNT, " stateid mode enabled\n");
7347 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7348 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007349
7350 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7351 dfprintk(MOUNT, " write mode enabled\n");
7352 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7353 }
7354
7355 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7356 dfprintk(MOUNT, " commit mode enabled\n");
7357 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7358 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007359 }
7360
7361 return 0;
7362}
7363
Andy Adamson8d89bd72016-09-09 09:22:18 -04007364struct nfs41_exchange_id_data {
7365 struct nfs41_exchange_id_res res;
7366 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007367 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007368 int rpc_status;
7369};
7370
7371static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007372{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007373 struct nfs41_exchange_id_data *cdata =
7374 (struct nfs41_exchange_id_data *)data;
7375 struct nfs_client *clp = cdata->args.client;
7376 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007377
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007378 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007379
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007380 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007381 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007382
7383 if (cdata->xprt && status == 0) {
7384 status = nfs4_detect_session_trunking(clp, &cdata->res,
7385 cdata->xprt);
7386 goto out;
7387 }
7388
Andy Adamson8d89bd72016-09-09 09:22:18 -04007389 if (status == 0)
7390 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007391
Chuck Lever177313f2012-05-21 22:44:58 -04007392 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007393 clp->cl_clientid = cdata->res.clientid;
7394 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007395 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007396 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007397 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007398 &clp->cl_session->session_state);
7399 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007400 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007401
Chuck Leveracdeb692012-05-21 22:46:16 -04007402 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007403 clp->cl_serverowner = cdata->res.server_owner;
7404 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007405
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007406 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007407 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007408 clp->cl_implid = cdata->res.impl_id;
7409 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007410
Chuck Lever177313f2012-05-21 22:44:58 -04007411 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007412 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007413 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007414 dprintk("%s: server_scope mismatch detected\n",
7415 __func__);
7416 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007417 kfree(clp->cl_serverscope);
7418 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007419 }
7420
Chuck Lever177313f2012-05-21 22:44:58 -04007421 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007422 clp->cl_serverscope = cdata->res.server_scope;
7423 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007424 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007425 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007426 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007427 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007428 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007429out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007430 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007431 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007432}
7433
7434static void nfs4_exchange_id_release(void *data)
7435{
7436 struct nfs41_exchange_id_data *cdata =
7437 (struct nfs41_exchange_id_data *)data;
7438
Andy Adamsonad0849a2016-09-09 09:22:28 -04007439 if (cdata->xprt) {
7440 xprt_put(cdata->xprt);
7441 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7442 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007443 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007444 kfree(cdata->res.impl_id);
7445 kfree(cdata->res.server_scope);
7446 kfree(cdata->res.server_owner);
7447 kfree(cdata);
7448}
7449
7450static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7451 .rpc_call_done = nfs4_exchange_id_done,
7452 .rpc_release = nfs4_exchange_id_release,
7453};
7454
Benny Halevy99fe60d2009-04-01 09:22:29 -04007455/*
7456 * _nfs4_proc_exchange_id()
7457 *
7458 * Wrapper for EXCHANGE_ID operation.
7459 */
7460static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007461 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007462{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007463 struct rpc_message msg = {
7464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007465 .rpc_cred = cred,
7466 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 struct rpc_task_setup task_setup_data = {
7468 .rpc_client = clp->cl_rpcclient,
7469 .callback_ops = &nfs4_exchange_id_call_ops,
7470 .rpc_message = &msg,
7471 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7472 };
7473 struct nfs41_exchange_id_data *calldata;
7474 struct rpc_task *task;
7475 int status = -EIO;
7476
7477 if (!atomic_inc_not_zero(&clp->cl_count))
7478 goto out;
7479
7480 status = -ENOMEM;
7481 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7482 if (!calldata)
7483 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007485 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007486
7487 status = nfs4_init_uniform_client_string(clp);
7488 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007489 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007490
7491 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7492 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7493 clp->cl_owner_id);
7494
Andy Adamson8d89bd72016-09-09 09:22:18 -04007495 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7496 GFP_NOFS);
7497 status = -ENOMEM;
7498 if (unlikely(calldata->res.server_owner == NULL))
7499 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007500
Andy Adamson8d89bd72016-09-09 09:22:18 -04007501 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007502 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007503 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007504 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007505
Andy Adamson8d89bd72016-09-09 09:22:18 -04007506 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7507 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007508 goto out_server_scope;
7509
7510 switch (sp4_how) {
7511 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007512 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007513 break;
7514
7515 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007516 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007517 break;
7518
7519 default:
7520 /* unsupported! */
7521 WARN_ON_ONCE(1);
7522 status = -EINVAL;
7523 goto out_impl_id;
7524 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007525 if (xprt) {
7526 calldata->xprt = xprt;
7527 task_setup_data.rpc_xprt = xprt;
7528 task_setup_data.flags =
7529 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007530 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7531 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007532 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007533 calldata->args.client = clp;
7534#ifdef CONFIG_NFS_V4_1_MIGRATION
7535 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7536 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7537 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7538#else
7539 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7540 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7541#endif
7542 msg.rpc_argp = &calldata->args;
7543 msg.rpc_resp = &calldata->res;
7544 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007545
Andy Adamson8d89bd72016-09-09 09:22:18 -04007546 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007547 if (IS_ERR(task))
7548 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007549
Andy Adamsonad0849a2016-09-09 09:22:28 -04007550 if (!xprt) {
7551 status = rpc_wait_for_completion_task(task);
7552 if (!status)
7553 status = calldata->rpc_status;
7554 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007555 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007556
Andy Adamson8d89bd72016-09-09 09:22:18 -04007557 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007558out:
Chuck Lever177313f2012-05-21 22:44:58 -04007559 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007560 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007561 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007562 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007563 clp->cl_implid->date.seconds,
7564 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007565 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007566 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007567
7568out_impl_id:
7569 kfree(calldata->res.impl_id);
7570out_server_scope:
7571 kfree(calldata->res.server_scope);
7572out_server_owner:
7573 kfree(calldata->res.server_owner);
7574out_calldata:
7575 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007576 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007577 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007578}
7579
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007580/*
7581 * nfs4_proc_exchange_id()
7582 *
7583 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7584 *
7585 * Since the clientid has expired, all compounds using sessions
7586 * associated with the stale clientid will be returning
7587 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7588 * be in some phase of session reset.
7589 *
7590 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7591 */
7592int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7593{
7594 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7595 int status;
7596
7597 /* try SP4_MACH_CRED if krb5i/p */
7598 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7599 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007600 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007601 if (!status)
7602 return 0;
7603 }
7604
7605 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007606 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007607}
7608
Andy Adamson04fa2c62016-09-09 09:22:29 -04007609/**
7610 * nfs4_test_session_trunk
7611 *
7612 * This is an add_xprt_test() test function called from
7613 * rpc_clnt_setup_test_and_add_xprt.
7614 *
7615 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7616 * and is dereferrenced in nfs4_exchange_id_release
7617 *
7618 * Upon success, add the new transport to the rpc_clnt
7619 *
7620 * @clnt: struct rpc_clnt to get new transport
7621 * @xprt: the rpc_xprt to test
7622 * @data: call data for _nfs4_proc_exchange_id.
7623 */
7624int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7625 void *data)
7626{
7627 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7628 u32 sp4_how;
7629
7630 dprintk("--> %s try %s\n", __func__,
7631 xprt->address_strings[RPC_DISPLAY_ADDR]);
7632
7633 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7634
7635 /* Test connection for session trunking. Async exchange_id call */
7636 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7637}
7638EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7639
Trond Myklebust66245532012-05-25 17:18:09 -04007640static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7641 struct rpc_cred *cred)
7642{
7643 struct rpc_message msg = {
7644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7645 .rpc_argp = clp,
7646 .rpc_cred = cred,
7647 };
7648 int status;
7649
7650 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007651 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007652 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007653 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007654 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7655 return status;
7656}
7657
7658static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7659 struct rpc_cred *cred)
7660{
7661 unsigned int loop;
7662 int ret;
7663
7664 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7665 ret = _nfs4_proc_destroy_clientid(clp, cred);
7666 switch (ret) {
7667 case -NFS4ERR_DELAY:
7668 case -NFS4ERR_CLIENTID_BUSY:
7669 ssleep(1);
7670 break;
7671 default:
7672 return ret;
7673 }
7674 }
7675 return 0;
7676}
7677
7678int nfs4_destroy_clientid(struct nfs_client *clp)
7679{
7680 struct rpc_cred *cred;
7681 int ret = 0;
7682
7683 if (clp->cl_mvops->minor_version < 1)
7684 goto out;
7685 if (clp->cl_exchange_flags == 0)
7686 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007687 if (clp->cl_preserve_clid)
7688 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007689 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007690 ret = nfs4_proc_destroy_clientid(clp, cred);
7691 if (cred)
7692 put_rpccred(cred);
7693 switch (ret) {
7694 case 0:
7695 case -NFS4ERR_STALE_CLIENTID:
7696 clp->cl_exchange_flags = 0;
7697 }
7698out:
7699 return ret;
7700}
7701
Andy Adamson2050f0c2009-04-01 09:22:30 -04007702struct nfs4_get_lease_time_data {
7703 struct nfs4_get_lease_time_args *args;
7704 struct nfs4_get_lease_time_res *res;
7705 struct nfs_client *clp;
7706};
7707
7708static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7709 void *calldata)
7710{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007711 struct nfs4_get_lease_time_data *data =
7712 (struct nfs4_get_lease_time_data *)calldata;
7713
7714 dprintk("--> %s\n", __func__);
7715 /* just setup sequence, do not trigger session recovery
7716 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007717 nfs41_setup_sequence(data->clp->cl_session,
7718 &data->args->la_seq_args,
7719 &data->res->lr_seq_res,
7720 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007721 dprintk("<-- %s\n", __func__);
7722}
7723
7724/*
7725 * Called from nfs4_state_manager thread for session setup, so don't recover
7726 * from sequence operation or clientid errors.
7727 */
7728static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7729{
7730 struct nfs4_get_lease_time_data *data =
7731 (struct nfs4_get_lease_time_data *)calldata;
7732
7733 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007734 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7735 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007736 switch (task->tk_status) {
7737 case -NFS4ERR_DELAY:
7738 case -NFS4ERR_GRACE:
7739 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7740 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7741 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007742 /* fall through */
7743 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007744 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007745 return;
7746 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007747 dprintk("<-- %s\n", __func__);
7748}
7749
Trond Myklebust17280172012-03-11 13:11:00 -04007750static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007751 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7752 .rpc_call_done = nfs4_get_lease_time_done,
7753};
7754
7755int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7756{
7757 struct rpc_task *task;
7758 struct nfs4_get_lease_time_args args;
7759 struct nfs4_get_lease_time_res res = {
7760 .lr_fsinfo = fsinfo,
7761 };
7762 struct nfs4_get_lease_time_data data = {
7763 .args = &args,
7764 .res = &res,
7765 .clp = clp,
7766 };
7767 struct rpc_message msg = {
7768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7769 .rpc_argp = &args,
7770 .rpc_resp = &res,
7771 };
7772 struct rpc_task_setup task_setup = {
7773 .rpc_client = clp->cl_rpcclient,
7774 .rpc_message = &msg,
7775 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007776 .callback_data = &data,
7777 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007778 };
7779 int status;
7780
Chuck Levera9c92d62013-08-09 12:48:18 -04007781 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007782 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007783 dprintk("--> %s\n", __func__);
7784 task = rpc_run_task(&task_setup);
7785
7786 if (IS_ERR(task))
7787 status = PTR_ERR(task);
7788 else {
7789 status = task->tk_status;
7790 rpc_put_task(task);
7791 }
7792 dprintk("<-- %s return %d\n", __func__, status);
7793
7794 return status;
7795}
7796
Andy Adamsonfc931582009-04-01 09:22:31 -04007797/*
7798 * Initialize the values to be used by the client in CREATE_SESSION
7799 * If nfs4_init_session set the fore channel request and response sizes,
7800 * use them.
7801 *
7802 * Set the back channel max_resp_sz_cached to zero to force the client to
7803 * always set csa_cachethis to FALSE because the current implementation
7804 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7805 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007806static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7807 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007808{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007809 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007810 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007811
Andy Adamson18aad3d2013-06-26 12:21:49 -04007812 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7813 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7814
Andy Adamsonfc931582009-04-01 09:22:31 -04007815 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007816 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7817 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007818 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007819 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007820
7821 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007822 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007823 __func__,
7824 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007825 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007826
7827 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007828 args->bc_attrs.max_rqst_sz = max_bc_payload;
7829 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007830 args->bc_attrs.max_resp_sz_cached = 0;
7831 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007832 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007833
7834 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7835 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7836 __func__,
7837 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7838 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7839 args->bc_attrs.max_reqs);
7840}
7841
Trond Myklebust79969dd2015-02-18 11:30:18 -08007842static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7843 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007844{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007845 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007846 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007847
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007848 if (rcvd->max_resp_sz > sent->max_resp_sz)
7849 return -EINVAL;
7850 /*
7851 * Our requested max_ops is the minimum we need; we're not
7852 * prepared to break up compounds into smaller pieces than that.
7853 * So, no point even trying to continue if the server won't
7854 * cooperate:
7855 */
7856 if (rcvd->max_ops < sent->max_ops)
7857 return -EINVAL;
7858 if (rcvd->max_reqs == 0)
7859 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007860 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7861 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007862 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007863}
7864
Trond Myklebust79969dd2015-02-18 11:30:18 -08007865static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7866 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007867{
7868 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007870
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007871 if (!(res->flags & SESSION4_BACK_CHAN))
7872 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007873 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7874 return -EINVAL;
7875 if (rcvd->max_resp_sz < sent->max_resp_sz)
7876 return -EINVAL;
7877 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7878 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007879 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007880 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007881 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007882 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007883out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007884 return 0;
7885}
Andy Adamson8d353012009-04-01 09:22:32 -04007886
Andy Adamson8d353012009-04-01 09:22:32 -04007887static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007888 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007889{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007890 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007891
Trond Myklebust79969dd2015-02-18 11:30:18 -08007892 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007893 if (ret)
7894 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007895 return nfs4_verify_back_channel_attrs(args, res);
7896}
7897
7898static void nfs4_update_session(struct nfs4_session *session,
7899 struct nfs41_create_session_res *res)
7900{
7901 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007902 /* Mark client id and session as being confirmed */
7903 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7904 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007905 session->flags = res->flags;
7906 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007907 if (res->flags & SESSION4_BACK_CHAN)
7908 memcpy(&session->bc_attrs, &res->bc_attrs,
7909 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007910}
7911
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007912static int _nfs4_proc_create_session(struct nfs_client *clp,
7913 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007914{
7915 struct nfs4_session *session = clp->cl_session;
7916 struct nfs41_create_session_args args = {
7917 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007918 .clientid = clp->cl_clientid,
7919 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007920 .cb_program = NFS4_CALLBACK,
7921 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007922 struct nfs41_create_session_res res;
7923
Andy Adamsonfc931582009-04-01 09:22:31 -04007924 struct rpc_message msg = {
7925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7926 .rpc_argp = &args,
7927 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007928 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007929 };
7930 int status;
7931
Chuck Lever6b26cc82016-05-02 14:40:40 -04007932 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007933 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007934
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007935 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007936 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007937
Trond Myklebustb519d402016-09-11 14:50:01 -04007938 switch (status) {
7939 case -NFS4ERR_STALE_CLIENTID:
7940 case -NFS4ERR_DELAY:
7941 case -ETIMEDOUT:
7942 case -EACCES:
7943 case -EAGAIN:
7944 goto out;
7945 };
7946
7947 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007948 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007949 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007950 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007951 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007952 if (status)
7953 goto out;
7954 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007955 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007956out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007957 return status;
7958}
7959
7960/*
7961 * Issues a CREATE_SESSION operation to the server.
7962 * It is the responsibility of the caller to verify the session is
7963 * expired before calling this routine.
7964 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007965int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007966{
7967 int status;
7968 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007969 struct nfs4_session *session = clp->cl_session;
7970
7971 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7972
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007973 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007974 if (status)
7975 goto out;
7976
Andy Adamsonaacd5532011-11-09 13:58:21 -05007977 /* Init or reset the session slot tables */
7978 status = nfs4_setup_session_slot_tables(session);
7979 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007980 if (status)
7981 goto out;
7982
7983 ptr = (unsigned *)&session->sess_id.data[0];
7984 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7985 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007986out:
7987 dprintk("<-- %s\n", __func__);
7988 return status;
7989}
7990
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007991/*
7992 * Issue the over-the-wire RPC DESTROY_SESSION.
7993 * The caller must serialize access to this routine.
7994 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007995int nfs4_proc_destroy_session(struct nfs4_session *session,
7996 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007997{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007998 struct rpc_message msg = {
7999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8000 .rpc_argp = session,
8001 .rpc_cred = cred,
8002 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008003 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008004
8005 dprintk("--> nfs4_proc_destroy_session\n");
8006
8007 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008008 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8009 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008010
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008011 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008012 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008013
8014 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008015 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008016 "Session has been destroyed regardless...\n", status);
8017
8018 dprintk("<-- nfs4_proc_destroy_session\n");
8019 return status;
8020}
8021
Trond Myklebust7b38c362012-05-23 13:23:31 -04008022/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008023 * Renew the cl_session lease.
8024 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008025struct nfs4_sequence_data {
8026 struct nfs_client *clp;
8027 struct nfs4_sequence_args args;
8028 struct nfs4_sequence_res res;
8029};
8030
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008031static void nfs41_sequence_release(void *data)
8032{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008033 struct nfs4_sequence_data *calldata = data;
8034 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008035
Alexandros Batsakis71358402010-02-05 03:45:05 -08008036 if (atomic_read(&clp->cl_count) > 1)
8037 nfs4_schedule_state_renewal(clp);
8038 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008039 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008040}
8041
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008042static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8043{
8044 switch(task->tk_status) {
8045 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008046 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8047 return -EAGAIN;
8048 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008049 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008050 }
8051 return 0;
8052}
8053
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008054static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008055{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008056 struct nfs4_sequence_data *calldata = data;
8057 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008058
Trond Myklebust14516c32010-07-31 14:29:06 -04008059 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8060 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008061
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008062 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063 if (task->tk_status < 0) {
8064 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008065 if (atomic_read(&clp->cl_count) == 1)
8066 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008067
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008068 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8069 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008070 return;
8071 }
8072 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008073 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008074out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008075 dprintk("<-- %s\n", __func__);
8076}
8077
8078static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8079{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008080 struct nfs4_sequence_data *calldata = data;
8081 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008082 struct nfs4_sequence_args *args;
8083 struct nfs4_sequence_res *res;
8084
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008085 args = task->tk_msg.rpc_argp;
8086 res = task->tk_msg.rpc_resp;
8087
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008088 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089}
8090
8091static const struct rpc_call_ops nfs41_sequence_ops = {
8092 .rpc_call_done = nfs41_sequence_call_done,
8093 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008094 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008095};
8096
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008097static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8098 struct rpc_cred *cred,
8099 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008101 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008102 struct rpc_message msg = {
8103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8104 .rpc_cred = cred,
8105 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008106 struct rpc_task_setup task_setup_data = {
8107 .rpc_client = clp->cl_rpcclient,
8108 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008109 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008110 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008111 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008112
Alexandros Batsakis71358402010-02-05 03:45:05 -08008113 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008114 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008115 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008116 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008117 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008118 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008119 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008120 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008121 if (is_privileged)
8122 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008123 msg.rpc_argp = &calldata->args;
8124 msg.rpc_resp = &calldata->res;
8125 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008126 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008127
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008128 return rpc_run_task(&task_setup_data);
8129}
8130
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008131static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008132{
8133 struct rpc_task *task;
8134 int ret = 0;
8135
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008136 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008137 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008138 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008139 if (IS_ERR(task))
8140 ret = PTR_ERR(task);
8141 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008142 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008143 dprintk("<-- %s status=%d\n", __func__, ret);
8144 return ret;
8145}
8146
8147static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8148{
8149 struct rpc_task *task;
8150 int ret;
8151
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008152 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008153 if (IS_ERR(task)) {
8154 ret = PTR_ERR(task);
8155 goto out;
8156 }
8157 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008158 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008159 ret = task->tk_status;
8160 rpc_put_task(task);
8161out:
8162 dprintk("<-- %s status=%d\n", __func__, ret);
8163 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008164}
8165
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008166struct nfs4_reclaim_complete_data {
8167 struct nfs_client *clp;
8168 struct nfs41_reclaim_complete_args arg;
8169 struct nfs41_reclaim_complete_res res;
8170};
8171
8172static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8173{
8174 struct nfs4_reclaim_complete_data *calldata = data;
8175
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008176 nfs41_setup_sequence(calldata->clp->cl_session,
8177 &calldata->arg.seq_args,
8178 &calldata->res.seq_res,
8179 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008180}
8181
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008182static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8183{
8184 switch(task->tk_status) {
8185 case 0:
8186 case -NFS4ERR_COMPLETE_ALREADY:
8187 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8188 break;
8189 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008190 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008191 /* fall through */
8192 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008193 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008194 case -NFS4ERR_BADSESSION:
8195 case -NFS4ERR_DEADSESSION:
8196 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8197 nfs4_schedule_session_recovery(clp->cl_session,
8198 task->tk_status);
8199 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008200 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008201 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008202 }
8203 return 0;
8204}
8205
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008206static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8207{
8208 struct nfs4_reclaim_complete_data *calldata = data;
8209 struct nfs_client *clp = calldata->clp;
8210 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8211
8212 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008213 if (!nfs41_sequence_done(task, res))
8214 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008215
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008216 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008217 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8218 rpc_restart_call_prepare(task);
8219 return;
8220 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008221 dprintk("<-- %s\n", __func__);
8222}
8223
8224static void nfs4_free_reclaim_complete_data(void *data)
8225{
8226 struct nfs4_reclaim_complete_data *calldata = data;
8227
8228 kfree(calldata);
8229}
8230
8231static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8232 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8233 .rpc_call_done = nfs4_reclaim_complete_done,
8234 .rpc_release = nfs4_free_reclaim_complete_data,
8235};
8236
8237/*
8238 * Issue a global reclaim complete.
8239 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008240static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8241 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008242{
8243 struct nfs4_reclaim_complete_data *calldata;
8244 struct rpc_task *task;
8245 struct rpc_message msg = {
8246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008247 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008248 };
8249 struct rpc_task_setup task_setup_data = {
8250 .rpc_client = clp->cl_rpcclient,
8251 .rpc_message = &msg,
8252 .callback_ops = &nfs4_reclaim_complete_call_ops,
8253 .flags = RPC_TASK_ASYNC,
8254 };
8255 int status = -ENOMEM;
8256
8257 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008258 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008259 if (calldata == NULL)
8260 goto out;
8261 calldata->clp = clp;
8262 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008263
Chuck Levera9c92d62013-08-09 12:48:18 -04008264 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008265 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008266 msg.rpc_argp = &calldata->arg;
8267 msg.rpc_resp = &calldata->res;
8268 task_setup_data.callback_data = calldata;
8269 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008270 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008271 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008272 goto out;
8273 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008274 status = nfs4_wait_for_completion_rpc_task(task);
8275 if (status == 0)
8276 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008277 rpc_put_task(task);
8278out:
8279 dprintk("<-- %s status=%d\n", __func__, status);
8280 return status;
8281}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008282
8283static void
8284nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8285{
8286 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008287 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008288 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008289
8290 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008291 nfs41_setup_sequence(session, &lgp->args.seq_args,
8292 &lgp->res.seq_res, task);
8293 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008294}
8295
8296static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8297{
8298 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008299
8300 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008301 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008302 dprintk("<-- %s\n", __func__);
8303}
8304
8305static int
8306nfs4_layoutget_handle_exception(struct rpc_task *task,
8307 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8308{
Trond Myklebustee314c22012-10-01 17:25:48 -07008309 struct inode *inode = lgp->args.inode;
8310 struct nfs_server *server = NFS_SERVER(inode);
8311 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008312 int nfs4err = task->tk_status;
8313 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008314 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008315
Boaz Harroshed7e5422014-01-22 20:34:54 +02008316 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008317
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008318 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008319 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008320 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008321
8322 /*
8323 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8324 * on the file. set tk_status to -ENODATA to tell upper layer to
8325 * retry go inband.
8326 */
8327 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008328 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008329 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008330 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008331 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8332 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8333 */
8334 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008335 status = -EOVERFLOW;
8336 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008337 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008338 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008339 * (or clients) writing to the same RAID stripe except when
8340 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008341 *
8342 * Treat it like we would RECALLCONFLICT -- we retry for a little
8343 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008344 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008345 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008346 if (lgp->args.minlength == 0) {
8347 status = -EOVERFLOW;
8348 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008349 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008350 status = -EBUSY;
8351 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008352 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008353 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008354 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008355 case -NFS4ERR_DELEG_REVOKED:
8356 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008357 case -NFS4ERR_EXPIRED:
8358 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008359 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008360 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008361 lo = NFS_I(inode)->layout;
8362 /* If the open stateid was bad, then recover it. */
8363 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8364 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008365 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008366 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008367 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008368 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008369 break;
8370 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008371
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008372 /*
8373 * Mark the bad layout state as invalid, then retry
8374 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008375 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008376 spin_unlock(&inode->i_lock);
8377 pnfs_free_lseg_list(&head);
8378 status = -EAGAIN;
8379 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008380 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008381
Trond Myklebust0a702352017-01-23 22:44:12 -05008382 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008383 err = nfs4_handle_exception(server, nfs4err, exception);
8384 if (!status) {
8385 if (exception->retry)
8386 status = -EAGAIN;
8387 else
8388 status = err;
8389 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008390out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008391 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008392 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008393}
8394
Idan Kedar85541162012-08-02 11:47:10 +03008395static size_t max_response_pages(struct nfs_server *server)
8396{
8397 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8398 return nfs_page_array_len(0, max_resp_sz);
8399}
8400
8401static void nfs4_free_pages(struct page **pages, size_t size)
8402{
8403 int i;
8404
8405 if (!pages)
8406 return;
8407
8408 for (i = 0; i < size; i++) {
8409 if (!pages[i])
8410 break;
8411 __free_page(pages[i]);
8412 }
8413 kfree(pages);
8414}
8415
8416static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8417{
8418 struct page **pages;
8419 int i;
8420
8421 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8422 if (!pages) {
8423 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8424 return NULL;
8425 }
8426
8427 for (i = 0; i < size; i++) {
8428 pages[i] = alloc_page(gfp_flags);
8429 if (!pages[i]) {
8430 dprintk("%s: failed to allocate page\n", __func__);
8431 nfs4_free_pages(pages, size);
8432 return NULL;
8433 }
8434 }
8435
8436 return pages;
8437}
8438
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008439static void nfs4_layoutget_release(void *calldata)
8440{
8441 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008442 struct inode *inode = lgp->args.inode;
8443 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008444 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008445
8446 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008447 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008448 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008449 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008450 put_nfs_open_context(lgp->args.ctx);
8451 kfree(calldata);
8452 dprintk("<-- %s\n", __func__);
8453}
8454
8455static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8456 .rpc_call_prepare = nfs4_layoutget_prepare,
8457 .rpc_call_done = nfs4_layoutget_done,
8458 .rpc_release = nfs4_layoutget_release,
8459};
8460
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008461struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008462nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008463{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008464 struct inode *inode = lgp->args.inode;
8465 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008466 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008467 struct rpc_task *task;
8468 struct rpc_message msg = {
8469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8470 .rpc_argp = &lgp->args,
8471 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008472 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008473 };
8474 struct rpc_task_setup task_setup_data = {
8475 .rpc_client = server->client,
8476 .rpc_message = &msg,
8477 .callback_ops = &nfs4_layoutget_call_ops,
8478 .callback_data = lgp,
8479 .flags = RPC_TASK_ASYNC,
8480 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008481 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008482 struct nfs4_exception exception = {
8483 .inode = inode,
8484 .timeout = *timeout,
8485 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008486 int status = 0;
8487
8488 dprintk("--> %s\n", __func__);
8489
Peng Tao4bd5a982014-11-17 11:05:17 +08008490 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8491 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8492
Idan Kedar85541162012-08-02 11:47:10 +03008493 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8494 if (!lgp->args.layout.pages) {
8495 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008496 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008497 }
8498 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8499
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008500 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008501 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008502 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008503
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008504 task = rpc_run_task(&task_setup_data);
8505 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008506 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008507 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008508 if (status == 0) {
8509 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8510 *timeout = exception.timeout;
8511 }
8512
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008513 trace_nfs4_layoutget(lgp->args.ctx,
8514 &lgp->args.range,
8515 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008516 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008517 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008518
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008519 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8520 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008521 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008522 rpc_put_task(task);
8523 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008524 if (status)
8525 return ERR_PTR(status);
8526 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008527}
8528
Benny Halevycbe82602011-05-22 19:52:37 +03008529static void
8530nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8531{
8532 struct nfs4_layoutreturn *lrp = calldata;
8533
8534 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008535 nfs41_setup_sequence(lrp->clp->cl_session,
8536 &lrp->args.seq_args,
8537 &lrp->res.seq_res,
8538 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008539}
8540
8541static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8542{
8543 struct nfs4_layoutreturn *lrp = calldata;
8544 struct nfs_server *server;
8545
8546 dprintk("--> %s\n", __func__);
8547
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008548 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008549 return;
8550
8551 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008552 switch (task->tk_status) {
8553 default:
8554 task->tk_status = 0;
8555 case 0:
8556 break;
8557 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008558 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008559 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008560 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008561 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008562 return;
8563 }
Benny Halevycbe82602011-05-22 19:52:37 +03008564 dprintk("<-- %s\n", __func__);
8565}
8566
8567static void nfs4_layoutreturn_release(void *calldata)
8568{
8569 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008570 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008571 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008572
8573 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008574 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008575 if (lrp->res.lrs_present) {
8576 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8577 &lrp->args.range,
8578 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008579 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008580 } else
8581 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008582 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008583 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008584 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008585 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008586 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008587 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008588 kfree(calldata);
8589 dprintk("<-- %s\n", __func__);
8590}
8591
8592static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8593 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8594 .rpc_call_done = nfs4_layoutreturn_done,
8595 .rpc_release = nfs4_layoutreturn_release,
8596};
8597
Peng Tao6c166052014-11-17 09:30:40 +08008598int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008599{
8600 struct rpc_task *task;
8601 struct rpc_message msg = {
8602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8603 .rpc_argp = &lrp->args,
8604 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008605 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008606 };
8607 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008608 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008609 .rpc_message = &msg,
8610 .callback_ops = &nfs4_layoutreturn_call_ops,
8611 .callback_data = lrp,
8612 };
Peng Tao6c166052014-11-17 09:30:40 +08008613 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008614
Andrew Elble99ade3c2015-12-02 09:39:51 -05008615 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8616 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8617 &task_setup_data.rpc_client, &msg);
8618
Benny Halevycbe82602011-05-22 19:52:37 +03008619 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008620 if (!sync) {
8621 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8622 if (!lrp->inode) {
8623 nfs4_layoutreturn_release(lrp);
8624 return -EAGAIN;
8625 }
8626 task_setup_data.flags |= RPC_TASK_ASYNC;
8627 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008628 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008629 task = rpc_run_task(&task_setup_data);
8630 if (IS_ERR(task))
8631 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008632 if (sync)
8633 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008634 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008635 dprintk("<-- %s status=%d\n", __func__, status);
8636 rpc_put_task(task);
8637 return status;
8638}
8639
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008640static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008641_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8642 struct pnfs_device *pdev,
8643 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008644{
8645 struct nfs4_getdeviceinfo_args args = {
8646 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008647 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8648 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008649 };
8650 struct nfs4_getdeviceinfo_res res = {
8651 .pdev = pdev,
8652 };
8653 struct rpc_message msg = {
8654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8655 .rpc_argp = &args,
8656 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008657 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008658 };
8659 int status;
8660
8661 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008662 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008663 if (res.notification & ~args.notify_types)
8664 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008665 if (res.notification != args.notify_types)
8666 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008667
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008668 dprintk("<-- %s status=%d\n", __func__, status);
8669
8670 return status;
8671}
8672
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008673int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8674 struct pnfs_device *pdev,
8675 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008676{
8677 struct nfs4_exception exception = { };
8678 int err;
8679
8680 do {
8681 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008682 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 &exception);
8684 } while (exception.retry);
8685 return err;
8686}
8687EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8688
Andy Adamson863a3c62011-03-23 13:27:54 +00008689static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8690{
8691 struct nfs4_layoutcommit_data *data = calldata;
8692 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008693 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008694
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008695 nfs41_setup_sequence(session,
8696 &data->args.seq_args,
8697 &data->res.seq_res,
8698 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008699}
8700
8701static void
8702nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8703{
8704 struct nfs4_layoutcommit_data *data = calldata;
8705 struct nfs_server *server = NFS_SERVER(data->args.inode);
8706
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008707 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008708 return;
8709
8710 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008711 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8712 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8713 case -NFS4ERR_BADLAYOUT: /* no layout */
8714 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008715 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008716 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008717 break;
8718 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008719 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008720 rpc_restart_call_prepare(task);
8721 return;
8722 }
8723 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008724}
8725
8726static void nfs4_layoutcommit_release(void *calldata)
8727{
8728 struct nfs4_layoutcommit_data *data = calldata;
8729
Andy Adamsondb29c082011-07-30 20:52:38 -04008730 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008731 nfs_post_op_update_inode_force_wcc(data->args.inode,
8732 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008733 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008734 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008735 kfree(data);
8736}
8737
8738static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8739 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8740 .rpc_call_done = nfs4_layoutcommit_done,
8741 .rpc_release = nfs4_layoutcommit_release,
8742};
8743
8744int
Andy Adamsonef311532011-03-12 02:58:10 -05008745nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008746{
8747 struct rpc_message msg = {
8748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8749 .rpc_argp = &data->args,
8750 .rpc_resp = &data->res,
8751 .rpc_cred = data->cred,
8752 };
8753 struct rpc_task_setup task_setup_data = {
8754 .task = &data->task,
8755 .rpc_client = NFS_CLIENT(data->args.inode),
8756 .rpc_message = &msg,
8757 .callback_ops = &nfs4_layoutcommit_ops,
8758 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008759 };
8760 struct rpc_task *task;
8761 int status = 0;
8762
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008763 dprintk("NFS: initiating layoutcommit call. sync %d "
8764 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008765 data->args.lastbytewritten,
8766 data->args.inode->i_ino);
8767
Trond Myklebust472e2592015-02-05 16:50:30 -05008768 if (!sync) {
8769 data->inode = nfs_igrab_and_active(data->args.inode);
8770 if (data->inode == NULL) {
8771 nfs4_layoutcommit_release(data);
8772 return -EAGAIN;
8773 }
8774 task_setup_data.flags = RPC_TASK_ASYNC;
8775 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008776 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008777 task = rpc_run_task(&task_setup_data);
8778 if (IS_ERR(task))
8779 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008780 if (sync)
8781 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008782 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008783 dprintk("%s: status %d\n", __func__, status);
8784 rpc_put_task(task);
8785 return status;
8786}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008787
Andy Adamson97431202013-08-08 10:57:56 -04008788/**
8789 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8790 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8791 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008792static int
8793_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008794 struct nfs_fsinfo *info,
8795 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008796{
8797 struct nfs41_secinfo_no_name_args args = {
8798 .style = SECINFO_STYLE_CURRENT_FH,
8799 };
8800 struct nfs4_secinfo_res res = {
8801 .flavors = flavors,
8802 };
8803 struct rpc_message msg = {
8804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8805 .rpc_argp = &args,
8806 .rpc_resp = &res,
8807 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008808 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008809 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008810 int status;
8811
8812 if (use_integrity) {
8813 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008814 cred = nfs4_get_clid_cred(server->nfs_client);
8815 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008816 }
8817
8818 dprintk("--> %s\n", __func__);
8819 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8820 &res.seq_res, 0);
8821 dprintk("<-- %s status=%d\n", __func__, status);
8822
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008823 if (cred)
8824 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008825
8826 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008827}
8828
8829static int
8830nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8831 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8832{
8833 struct nfs4_exception exception = { };
8834 int err;
8835 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008836 /* first try using integrity protection */
8837 err = -NFS4ERR_WRONGSEC;
8838
8839 /* try to use integrity protection with machine cred */
8840 if (_nfs4_is_integrity_protected(server->nfs_client))
8841 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8842 flavors, true);
8843
8844 /*
8845 * if unable to use integrity protection, or SECINFO with
8846 * integrity protection returns NFS4ERR_WRONGSEC (which is
8847 * disallowed by spec, but exists in deployed servers) use
8848 * the current filesystem's rpc_client and the user cred.
8849 */
8850 if (err == -NFS4ERR_WRONGSEC)
8851 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8852 flavors, false);
8853
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008854 switch (err) {
8855 case 0:
8856 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008857 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008858 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008859 default:
8860 err = nfs4_handle_exception(server, err, &exception);
8861 }
8862 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008863out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008864 return err;
8865}
8866
8867static int
8868nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8869 struct nfs_fsinfo *info)
8870{
8871 int err;
8872 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008873 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008874 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008875 struct nfs4_secinfo4 *secinfo;
8876 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008877
8878 page = alloc_page(GFP_KERNEL);
8879 if (!page) {
8880 err = -ENOMEM;
8881 goto out;
8882 }
8883
8884 flavors = page_address(page);
8885 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8886
8887 /*
8888 * Fall back on "guess and check" method if
8889 * the server doesn't support SECINFO_NO_NAME
8890 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008891 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008892 err = nfs4_find_root_sec(server, fhandle, info);
8893 goto out_freepage;
8894 }
8895 if (err)
8896 goto out_freepage;
8897
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008898 for (i = 0; i < flavors->num_flavors; i++) {
8899 secinfo = &flavors->flavors[i];
8900
8901 switch (secinfo->flavor) {
8902 case RPC_AUTH_NULL:
8903 case RPC_AUTH_UNIX:
8904 case RPC_AUTH_GSS:
8905 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8906 &secinfo->flavor_info);
8907 break;
8908 default:
8909 flavor = RPC_AUTH_MAXFLAVOR;
8910 break;
8911 }
8912
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008913 if (!nfs_auth_info_match(&server->auth_info, flavor))
8914 flavor = RPC_AUTH_MAXFLAVOR;
8915
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008916 if (flavor != RPC_AUTH_MAXFLAVOR) {
8917 err = nfs4_lookup_root_sec(server, fhandle,
8918 info, flavor);
8919 if (!err)
8920 break;
8921 }
8922 }
8923
8924 if (flavor == RPC_AUTH_MAXFLAVOR)
8925 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008926
8927out_freepage:
8928 put_page(page);
8929 if (err == -EACCES)
8930 return -EPERM;
8931out:
8932 return err;
8933}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008934
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008935static int _nfs41_test_stateid(struct nfs_server *server,
8936 nfs4_stateid *stateid,
8937 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008938{
8939 int status;
8940 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008941 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008942 };
8943 struct nfs41_test_stateid_res res;
8944 struct rpc_message msg = {
8945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8946 .rpc_argp = &args,
8947 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008948 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008949 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008950 struct rpc_clnt *rpc_client = server->client;
8951
8952 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8953 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008954
Chuck Lever38527b12012-07-11 16:30:23 -04008955 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008956 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008957 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008958 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008959 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008960 if (status != NFS_OK) {
8961 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008962 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008963 }
8964 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008965 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008966}
8967
Trond Myklebust43912bb2016-09-22 13:38:56 -04008968static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8969 int err, struct nfs4_exception *exception)
8970{
8971 exception->retry = 0;
8972 switch(err) {
8973 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008974 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008975 nfs4_handle_exception(server, err, exception);
8976 break;
8977 case -NFS4ERR_BADSESSION:
8978 case -NFS4ERR_BADSLOT:
8979 case -NFS4ERR_BAD_HIGH_SLOT:
8980 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8981 case -NFS4ERR_DEADSESSION:
8982 nfs4_do_handle_exception(server, err, exception);
8983 }
8984}
8985
Chuck Lever38527b12012-07-11 16:30:23 -04008986/**
8987 * nfs41_test_stateid - perform a TEST_STATEID operation
8988 *
8989 * @server: server / transport on which to perform the operation
8990 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008991 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008992 *
8993 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8994 * Otherwise a negative NFS4ERR value is returned if the operation
8995 * failed or the state ID is not currently valid.
8996 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008997static int nfs41_test_stateid(struct nfs_server *server,
8998 nfs4_stateid *stateid,
8999 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009000{
9001 struct nfs4_exception exception = { };
9002 int err;
9003 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009004 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009005 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009006 } while (exception.retry);
9007 return err;
9008}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009009
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009010struct nfs_free_stateid_data {
9011 struct nfs_server *server;
9012 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009013 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009014};
9015
9016static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9017{
9018 struct nfs_free_stateid_data *data = calldata;
9019 nfs41_setup_sequence(nfs4_get_session(data->server),
9020 &data->args.seq_args,
9021 &data->res.seq_res,
9022 task);
9023}
9024
9025static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9026{
9027 struct nfs_free_stateid_data *data = calldata;
9028
9029 nfs41_sequence_done(task, &data->res.seq_res);
9030
9031 switch (task->tk_status) {
9032 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009033 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009034 rpc_restart_call_prepare(task);
9035 }
9036}
9037
9038static void nfs41_free_stateid_release(void *calldata)
9039{
9040 kfree(calldata);
9041}
9042
Trond Myklebust17f26b12013-08-21 15:48:42 -04009043static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009044 .rpc_call_prepare = nfs41_free_stateid_prepare,
9045 .rpc_call_done = nfs41_free_stateid_done,
9046 .rpc_release = nfs41_free_stateid_release,
9047};
9048
9049static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009050 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009051 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009052 bool privileged)
9053{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009054 struct rpc_message msg = {
9055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009056 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009057 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009058 struct rpc_task_setup task_setup = {
9059 .rpc_client = server->client,
9060 .rpc_message = &msg,
9061 .callback_ops = &nfs41_free_stateid_ops,
9062 .flags = RPC_TASK_ASYNC,
9063 };
9064 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009065
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009066 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9067 &task_setup.rpc_client, &msg);
9068
Chuck Lever38527b12012-07-11 16:30:23 -04009069 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009070 data = kmalloc(sizeof(*data), GFP_NOFS);
9071 if (!data)
9072 return ERR_PTR(-ENOMEM);
9073 data->server = server;
9074 nfs4_stateid_copy(&data->args.stateid, stateid);
9075
9076 task_setup.callback_data = data;
9077
9078 msg.rpc_argp = &data->args;
9079 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009080 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009081 if (privileged)
9082 nfs4_set_sequence_privileged(&data->args.seq_args);
9083
9084 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009085}
9086
Chuck Lever38527b12012-07-11 16:30:23 -04009087/**
9088 * nfs41_free_stateid - perform a FREE_STATEID operation
9089 *
9090 * @server: server / transport on which to perform the operation
9091 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009092 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009093 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009094 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009095 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009096 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009097static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009098 const nfs4_stateid *stateid,
9099 struct rpc_cred *cred,
9100 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009101{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009102 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009103
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009104 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009105 if (IS_ERR(task))
9106 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009107 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009108 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009109}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009110
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009111static void
9112nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009113{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009114 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009115
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009116 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009117 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009118}
9119
Trond Myklebust36281ca2012-03-04 18:13:56 -05009120static bool nfs41_match_stateid(const nfs4_stateid *s1,
9121 const nfs4_stateid *s2)
9122{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009123 if (s1->type != s2->type)
9124 return false;
9125
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009126 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009127 return false;
9128
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009129 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009130 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009131 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009132 return true;
9133
9134 return false;
9135}
9136
Andy Adamson557134a2009-04-01 09:21:53 -04009137#endif /* CONFIG_NFS_V4_1 */
9138
Trond Myklebust36281ca2012-03-04 18:13:56 -05009139static bool nfs4_match_stateid(const nfs4_stateid *s1,
9140 const nfs4_stateid *s2)
9141{
Trond Myklebustf597c532012-03-04 18:13:56 -05009142 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009143}
9144
9145
Trond Myklebust17280172012-03-11 13:11:00 -04009146static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009147 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009148 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149 .recover_open = nfs4_open_reclaim,
9150 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009151 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009152 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009153};
9154
Andy Adamson591d71c2009-04-01 09:22:47 -04009155#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009156static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009157 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9158 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9159 .recover_open = nfs4_open_reclaim,
9160 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009161 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009162 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009163 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009164};
9165#endif /* CONFIG_NFS_V4_1 */
9166
Trond Myklebust17280172012-03-11 13:11:00 -04009167static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009168 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009169 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009170 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009172 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009173};
9174
Andy Adamson591d71c2009-04-01 09:22:47 -04009175#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009176static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009177 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9178 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009179 .recover_open = nfs41_open_expired,
9180 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009181 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009182};
9183#endif /* CONFIG_NFS_V4_1 */
9184
Trond Myklebust17280172012-03-11 13:11:00 -04009185static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009186 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009187 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009188 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009189};
9190
9191#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009192static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009193 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009194 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009195 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009196};
9197#endif
9198
Chuck Leverec011fe2013-10-17 14:12:39 -04009199static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009200 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009201 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009202};
9203
9204#if defined(CONFIG_NFS_V4_1)
9205static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009206 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009207 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009208};
9209#endif /* CONFIG_NFS_V4_1 */
9210
Trond Myklebust97dc1352010-06-16 09:52:26 -04009211static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9212 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009213 .init_caps = NFS_CAP_READDIRPLUS
9214 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009215 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009216 .init_client = nfs40_init_client,
9217 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009218 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009219 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009220 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009221 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009222 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009223 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009224 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9225 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9226 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009227 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009228};
9229
9230#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009231static struct nfs_seqid *
9232nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9233{
9234 return NULL;
9235}
9236
Trond Myklebust97dc1352010-06-16 09:52:26 -04009237static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9238 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009239 .init_caps = NFS_CAP_READDIRPLUS
9240 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009241 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009242 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009243 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009244 .init_client = nfs41_init_client,
9245 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009246 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009247 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009248 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009249 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009250 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009251 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009252 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009253 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9254 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9255 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009256 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009257};
9258#endif
9259
Steve Dickson42c2c422013-05-22 12:50:38 -04009260#if defined(CONFIG_NFS_V4_2)
9261static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9262 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009263 .init_caps = NFS_CAP_READDIRPLUS
9264 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009265 | NFS_CAP_POSIX_LOCK
9266 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009267 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009268 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009269 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009270 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009271 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009272 | NFS_CAP_LAYOUTSTATS
9273 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009274 .init_client = nfs41_init_client,
9275 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009276 .match_stateid = nfs41_match_stateid,
9277 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009278 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009279 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009280 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009281 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009282 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009283 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9284 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9285 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009286 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009287};
9288#endif
9289
Trond Myklebust97dc1352010-06-16 09:52:26 -04009290const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9291 [0] = &nfs_v4_0_minor_ops,
9292#if defined(CONFIG_NFS_V4_1)
9293 [1] = &nfs_v4_1_minor_ops,
9294#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009295#if defined(CONFIG_NFS_V4_2)
9296 [2] = &nfs_v4_2_minor_ops,
9297#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009298};
9299
Trond Myklebust13997822016-07-24 17:10:52 -04009300static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009301{
9302 ssize_t error, error2;
9303
9304 error = generic_listxattr(dentry, list, size);
9305 if (error < 0)
9306 return error;
9307 if (list) {
9308 list += error;
9309 size -= error;
9310 }
9311
9312 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9313 if (error2 < 0)
9314 return error2;
9315 return error + error2;
9316}
9317
Trond Myklebust17f26b12013-08-21 15:48:42 -04009318static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009319 .create = nfs_create,
9320 .lookup = nfs_lookup,
9321 .atomic_open = nfs_atomic_open,
9322 .link = nfs_link,
9323 .unlink = nfs_unlink,
9324 .symlink = nfs_symlink,
9325 .mkdir = nfs_mkdir,
9326 .rmdir = nfs_rmdir,
9327 .mknod = nfs_mknod,
9328 .rename = nfs_rename,
9329 .permission = nfs_permission,
9330 .getattr = nfs_getattr,
9331 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009332 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009333};
9334
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009335static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009336 .permission = nfs_permission,
9337 .getattr = nfs_getattr,
9338 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009339 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009340};
9341
David Howells509de812006-08-22 20:06:11 -04009342const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009343 .version = 4, /* protocol version */
9344 .dentry_ops = &nfs4_dentry_operations,
9345 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009346 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009347 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009349 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009350 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 .getattr = nfs4_proc_getattr,
9352 .setattr = nfs4_proc_setattr,
9353 .lookup = nfs4_proc_lookup,
9354 .access = nfs4_proc_access,
9355 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356 .create = nfs4_proc_create,
9357 .remove = nfs4_proc_remove,
9358 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009359 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009361 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009362 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009363 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 .link = nfs4_proc_link,
9365 .symlink = nfs4_proc_symlink,
9366 .mkdir = nfs4_proc_mkdir,
9367 .rmdir = nfs4_proc_remove,
9368 .readdir = nfs4_proc_readdir,
9369 .mknod = nfs4_proc_mknod,
9370 .statfs = nfs4_proc_statfs,
9371 .fsinfo = nfs4_proc_fsinfo,
9372 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009373 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009375 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009377 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009379 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009381 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009382 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009384 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009385 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009386 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009387 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009388 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009389 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009390 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009391 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009392 .create_server = nfs4_create_server,
9393 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394};
9395
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009396static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009397 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009398 .list = nfs4_xattr_list_nfs4_acl,
9399 .get = nfs4_xattr_get_nfs4_acl,
9400 .set = nfs4_xattr_set_nfs4_acl,
9401};
9402
9403const struct xattr_handler *nfs4_xattr_handlers[] = {
9404 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009405#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9406 &nfs4_xattr_nfs4_label_handler,
9407#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009408 NULL
9409};
9410
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411/*
9412 * Local variables:
9413 * c-basic-offset: 8
9414 * End:
9415 */