blob: d0d2a5e980aa7f33f7b3f289da7323a8808d5f59 [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:
544 if (ret == 0)
545 exception->retry = 1;
546 return ret;
547}
548
549static int
550nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
551 struct nfs4_state *state, long *timeout)
552{
553 struct nfs4_exception exception = {
554 .state = state,
555 };
556
557 if (task->tk_status >= 0)
558 return 0;
559 if (timeout)
560 exception.timeout = *timeout;
561 task->tk_status = nfs4_async_handle_exception(task, server,
562 task->tk_status,
563 &exception);
564 if (exception.delay && timeout)
565 *timeout = exception.timeout;
566 if (exception.retry)
567 return -EAGAIN;
568 return 0;
569}
570
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400571/*
572 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
573 * or 'false' otherwise.
574 */
575static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
576{
577 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
578
579 if (flavor == RPC_AUTH_GSS_KRB5I ||
580 flavor == RPC_AUTH_GSS_KRB5P)
581 return true;
582
583 return false;
584}
Trond Myklebust65de8722008-12-23 15:21:44 -0500585
Trond Myklebust452e9352010-07-31 14:29:06 -0400586static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spin_lock(&clp->cl_lock);
589 if (time_before(clp->cl_last_renewal,timestamp))
590 clp->cl_last_renewal = timestamp;
591 spin_unlock(&clp->cl_lock);
592}
593
Trond Myklebust452e9352010-07-31 14:29:06 -0400594static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
595{
Trond Myklebustbe824162015-07-05 14:50:46 -0400596 struct nfs_client *clp = server->nfs_client;
597
598 if (!nfs4_has_session(clp))
599 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400600}
601
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400602struct nfs4_call_sync_data {
603 const struct nfs_server *seq_server;
604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
606};
607
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800608void nfs4_init_sequence(struct nfs4_sequence_args *args,
609 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400610{
611 args->sa_slot = NULL;
612 args->sa_cache_this = cache_reply;
613 args->sa_privileged = 0;
614
615 res->sr_slot = NULL;
616}
617
618static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
619{
620 args->sa_privileged = 1;
621}
622
Peng Taocb04ad22014-06-11 05:24:15 +0800623int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
624 struct nfs4_sequence_args *args,
625 struct nfs4_sequence_res *res,
626 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400627{
Chuck Lever3bd23842013-08-09 12:49:19 -0400628 struct nfs4_slot *slot;
629
630 /* slot already allocated? */
631 if (res->sr_slot != NULL)
632 goto out_start;
633
634 spin_lock(&tbl->slot_tbl_lock);
635 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
636 goto out_sleep;
637
638 slot = nfs4_alloc_slot(tbl);
639 if (IS_ERR(slot)) {
640 if (slot == ERR_PTR(-ENOMEM))
641 task->tk_timeout = HZ >> 2;
642 goto out_sleep;
643 }
644 spin_unlock(&tbl->slot_tbl_lock);
645
Trond Myklebust0a014a42016-09-22 13:38:51 -0400646 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400647 args->sa_slot = slot;
648 res->sr_slot = slot;
649
650out_start:
651 rpc_call_start(task);
652 return 0;
653
654out_sleep:
655 if (args->sa_privileged)
656 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
657 NULL, RPC_PRIORITY_PRIVILEGED);
658 else
659 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
660 spin_unlock(&tbl->slot_tbl_lock);
661 return -EAGAIN;
662}
Peng Taocb04ad22014-06-11 05:24:15 +0800663EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400664
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400665static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400666{
667 struct nfs4_slot *slot = res->sr_slot;
668 struct nfs4_slot_table *tbl;
669
Chuck Lever3bd23842013-08-09 12:49:19 -0400670 tbl = slot->table;
671 spin_lock(&tbl->slot_tbl_lock);
672 if (!nfs41_wake_and_assign_slot(tbl, slot))
673 nfs4_free_slot(tbl, slot);
674 spin_unlock(&tbl->slot_tbl_lock);
675
676 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400677}
678
679static int nfs40_sequence_done(struct rpc_task *task,
680 struct nfs4_sequence_res *res)
681{
682 if (res->sr_slot != NULL)
683 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400684 return 1;
685}
686
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400687#if defined(CONFIG_NFS_V4_1)
688
Trond Myklebustd185a332010-06-16 09:52:25 -0400689static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400690{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500691 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400692 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500693 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500694 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400695
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500698
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400699 /* Bump the slot sequence number */
700 if (slot->seq_done)
701 slot->seq_nr++;
702 slot->seq_done = 0;
703
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500704 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
707 */
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
710
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500712 send_new_highest_used_slotid = false;
713 goto out_unlock;
714 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500716
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500720 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400721 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500722 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400723 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400724 if (waitqueue_active(&tbl->slot_waitq))
725 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400726}
727
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400728static int nfs41_sequence_process(struct rpc_task *task,
729 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400730{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500731 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500732 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400733 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500734 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500735 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400736
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500737 if (slot == NULL)
738 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400739 /* don't increment the sequence number if the task wasn't sent */
740 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400741 goto out;
742
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500743 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500744
Trond Myklebustac20d162012-12-15 15:36:07 -0500745 if (slot->interrupted) {
746 slot->interrupted = 0;
747 interrupted = true;
748 }
749
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400750 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500751 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400752 switch (res->sr_status) {
753 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400754 /* If previous op on slot was interrupted and we reused
755 * the seq# and got a reply from the cache, then retry
756 */
757 if (task->tk_status == -EREMOTEIO && interrupted) {
758 ++slot->seq_nr;
759 goto retry_nowait;
760 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400761 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400762 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500763 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500764 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500765 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400766 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
767 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500768 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400769 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500770 case 1:
771 /*
772 * sr_status remains 1 if an RPC level error occurred.
773 * The server may or may not have processed the sequence
774 * operation..
775 * Mark the slot as having hosted an interrupted RPC call.
776 */
777 slot->interrupted = 1;
778 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400779 case -NFS4ERR_DELAY:
780 /* The server detected a resend of the RPC call and
781 * returned NFS4ERR_DELAY as per Section 2.10.6.2
782 * of RFC5661.
783 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500784 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400785 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500786 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500787 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400788 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500789 case -NFS4ERR_BADSLOT:
790 /*
791 * The slot id we used was probably retired. Try again
792 * using a different slot id.
793 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 goto retry_nowait;
795 case -NFS4ERR_SEQ_MISORDERED:
796 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500797 * Was the last operation on this sequence interrupted?
798 * If so, retry after bumping the sequence number.
799 */
800 if (interrupted) {
801 ++slot->seq_nr;
802 goto retry_nowait;
803 }
804 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500805 * Could this slot have been previously retired?
806 * If so, then the server may be expecting seq_nr = 1!
807 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500808 if (slot->seq_nr != 1) {
809 slot->seq_nr = 1;
810 goto retry_nowait;
811 }
812 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500813 case -NFS4ERR_SEQ_FALSE_RETRY:
814 ++slot->seq_nr;
815 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400816 default:
817 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400818 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400819 }
820out:
821 /* The session may be reset by one of the error handlers. */
822 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500823out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500824 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500825retry_nowait:
826 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400827 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500828 task->tk_status = 0;
829 ret = 0;
830 }
831 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400832out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400833 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400834 goto out;
835 rpc_delay(task, NFS4_POLL_RETRY_MAX);
836 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400837}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400838
839int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
840{
841 if (!nfs41_sequence_process(task, res))
842 return 0;
843 if (res->sr_slot != NULL)
844 nfs41_sequence_free_slot(res);
845 return 1;
846
847}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500848EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400849
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400850static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
851{
852 if (res->sr_slot == NULL)
853 return 1;
854 if (res->sr_slot->table->session != NULL)
855 return nfs41_sequence_process(task, res);
856 return nfs40_sequence_done(task, res);
857}
858
859static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
860{
861 if (res->sr_slot != NULL) {
862 if (res->sr_slot->table->session != NULL)
863 nfs41_sequence_free_slot(res);
864 else
865 nfs40_sequence_free_slot(res);
866 }
867}
868
Peng Tao2c4b1312014-06-11 05:24:16 +0800869int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400870{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500871 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400872 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400873 if (!res->sr_slot->table->session)
874 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400875 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400876}
Peng Tao2c4b1312014-06-11 05:24:16 +0800877EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400878
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000879int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400880 struct nfs4_sequence_args *args,
881 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400882 struct rpc_task *task)
883{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400884 struct nfs4_slot *slot;
885 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400886
887 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400888 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400889 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400890 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400891
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400892 tbl = &session->fc_slot_table;
893
Trond Myklebust69d206b2012-11-22 13:21:02 -0500894 task->tk_timeout = 0;
895
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400896 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400897 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400898 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500899 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500900 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400901 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400902 }
903
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500904 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500905 if (IS_ERR(slot)) {
906 /* If out of memory, try again in 1/4 second */
907 if (slot == ERR_PTR(-ENOMEM))
908 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400909 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400910 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400911 }
912 spin_unlock(&tbl->slot_tbl_lock);
913
Trond Myklebust0a014a42016-09-22 13:38:51 -0400914 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500915 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916
Chuck Levere8d92382013-08-09 12:47:51 -0400917 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500918 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400919
Benny Halevydfb4f3092010-09-24 09:17:01 -0400920 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500921 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400922 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923 /*
924 * sr_status is only set in decode_sequence, and so will remain
925 * set to 1 if an rpc level failure occurs.
926 */
927 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400928 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400929out_success:
930 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400931 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400932out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400933 /* Privileged tasks are queued with top priority */
934 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400935 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
936 NULL, RPC_PRIORITY_PRIVILEGED);
937 else
938 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939 spin_unlock(&tbl->slot_tbl_lock);
940 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400941}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000942EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400943
Chuck Lever5a580e02013-08-09 12:48:09 -0400944static int nfs4_setup_sequence(const struct nfs_server *server,
945 struct nfs4_sequence_args *args,
946 struct nfs4_sequence_res *res,
947 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948{
Trond Myklebust035168a2010-06-16 09:52:26 -0400949 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950 int ret = 0;
951
Chuck Lever3bd23842013-08-09 12:49:19 -0400952 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800953 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
954 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400955
Chuck Levere8d92382013-08-09 12:47:51 -0400956 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400957 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400958 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400959
Trond Myklebust9d12b212012-01-17 22:04:25 -0500960 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400961
Andy Adamsonce5039c2009-04-01 09:22:13 -0400962 dprintk("<-- %s status=%d\n", __func__, ret);
963 return ret;
964}
965
Andy Adamsonce5039c2009-04-01 09:22:13 -0400966static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
967{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400968 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400969 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400970
Trond Myklebust035168a2010-06-16 09:52:26 -0400971 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
972
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400973 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400974}
975
Andy Adamson69ab40c2009-04-01 09:22:19 -0400976static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
977{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400978 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400979
Trond Myklebust14516c32010-07-31 14:29:06 -0400980 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400981}
982
Trond Myklebust17280172012-03-11 13:11:00 -0400983static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400984 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400986};
987
Chuck Lever3bd23842013-08-09 12:49:19 -0400988#else /* !CONFIG_NFS_V4_1 */
989
Chuck Lever5a580e02013-08-09 12:48:09 -0400990static int nfs4_setup_sequence(const struct nfs_server *server,
991 struct nfs4_sequence_args *args,
992 struct nfs4_sequence_res *res,
993 struct rpc_task *task)
994{
Peng Taocb04ad22014-06-11 05:24:15 +0800995 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
996 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400997}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400998
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400999static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1000{
1001 return nfs40_sequence_done(task, res);
1002}
1003
1004static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1005{
1006 if (res->sr_slot != NULL)
1007 nfs40_sequence_free_slot(res);
1008}
1009
Peng Tao2c4b1312014-06-11 05:24:16 +08001010int nfs4_sequence_done(struct rpc_task *task,
1011 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001012{
Chuck Lever3bd23842013-08-09 12:49:19 -04001013 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001014}
Peng Tao2c4b1312014-06-11 05:24:16 +08001015EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001016
1017#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001018
Chuck Lever9915ea72013-08-09 12:48:27 -04001019static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1020{
1021 struct nfs4_call_sync_data *data = calldata;
1022 nfs4_setup_sequence(data->seq_server,
1023 data->seq_args, data->seq_res, task);
1024}
1025
1026static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_sequence_done(task, data->seq_res);
1030}
1031
1032static const struct rpc_call_ops nfs40_call_sync_ops = {
1033 .rpc_call_prepare = nfs40_call_sync_prepare,
1034 .rpc_call_done = nfs40_call_sync_done,
1035};
1036
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001037static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001038 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001042{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 int ret;
1044 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001045 struct nfs_client *clp = server->nfs_client;
1046 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 .seq_server = server,
1048 .seq_args = args,
1049 .seq_res = res,
1050 };
1051 struct rpc_task_setup task_setup = {
1052 .rpc_client = clnt,
1053 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001054 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001055 .callback_data = &data
1056 };
1057
1058 task = rpc_run_task(&task_setup);
1059 if (IS_ERR(task))
1060 ret = PTR_ERR(task);
1061 else {
1062 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001063 rpc_put_task(task);
1064 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001065 return ret;
1066}
1067
Bryan Schumaker7c513052011-03-24 17:12:24 +00001068int nfs4_call_sync(struct rpc_clnt *clnt,
1069 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001070 struct rpc_message *msg,
1071 struct nfs4_sequence_args *args,
1072 struct nfs4_sequence_res *res,
1073 int cache_reply)
1074{
Chuck Levera9c92d62013-08-09 12:48:18 -04001075 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001076 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1080{
1081 struct nfs_inode *nfsi = NFS_I(dir);
1082
1083 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001084 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001085 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001087 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001088 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001089 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 spin_unlock(&dir->i_lock);
1091}
1092
1093struct nfs4_opendata {
1094 struct kref kref;
1095 struct nfs_openargs o_arg;
1096 struct nfs_openres o_res;
1097 struct nfs_open_confirmargs c_arg;
1098 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001099 struct nfs4_string owner_name;
1100 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001101 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001103 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001105 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct nfs4_state_owner *owner;
1107 struct nfs4_state *state;
1108 struct iattr attrs;
1109 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001110 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001111 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001112 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001113 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int cancelled;
1115};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001116
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001117static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1118 int err, struct nfs4_exception *exception)
1119{
1120 if (err != -EINVAL)
1121 return false;
1122 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1123 return false;
1124 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1125 exception->retry = 1;
1126 return true;
1127}
1128
Trond Myklebust6ae37332015-01-30 14:21:14 -05001129static u32
1130nfs4_map_atomic_open_share(struct nfs_server *server,
1131 fmode_t fmode, int openflags)
1132{
1133 u32 res = 0;
1134
1135 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1136 case FMODE_READ:
1137 res = NFS4_SHARE_ACCESS_READ;
1138 break;
1139 case FMODE_WRITE:
1140 res = NFS4_SHARE_ACCESS_WRITE;
1141 break;
1142 case FMODE_READ|FMODE_WRITE:
1143 res = NFS4_SHARE_ACCESS_BOTH;
1144 }
1145 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1146 goto out;
1147 /* Want no delegation if we're using O_DIRECT */
1148 if (openflags & O_DIRECT)
1149 res |= NFS4_SHARE_WANT_NO_DELEG;
1150out:
1151 return res;
1152}
1153
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001154static enum open_claim_type4
1155nfs4_map_atomic_open_claim(struct nfs_server *server,
1156 enum open_claim_type4 claim)
1157{
1158 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1159 return claim;
1160 switch (claim) {
1161 default:
1162 return claim;
1163 case NFS4_OPEN_CLAIM_FH:
1164 return NFS4_OPEN_CLAIM_NULL;
1165 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1166 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1167 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1168 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1169 }
1170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173{
1174 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001175 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 p->o_res.seqid = p->o_arg.seqid;
1177 p->c_res.seqid = p->c_arg.seqid;
1178 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001179 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001181 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182}
1183
Al Viro82a2c1b2011-06-22 18:30:55 -04001184static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001185 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001186 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001187 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001188 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001189 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190{
Al Viro82a2c1b2011-06-22 18:30:55 -04001191 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001192 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001194 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 struct nfs4_opendata *p;
1196
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001197 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 if (p == NULL)
1199 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001200
1201 p->f_label = nfs4_label_alloc(server, gfp_mask);
1202 if (IS_ERR(p->f_label))
1203 goto err_free_p;
1204
Kinglong Meea49c2692015-07-27 15:31:38 +08001205 p->a_label = nfs4_label_alloc(server, gfp_mask);
1206 if (IS_ERR(p->a_label))
1207 goto err_free_f;
1208
Trond Myklebust63f5f792015-01-23 19:19:25 -05001209 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1210 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001211 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001212 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001213 nfs_sb_active(dentry->d_sb);
1214 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001215 p->dir = parent;
1216 p->owner = sp;
1217 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218 p->o_arg.open_flags = flags;
1219 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001220 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1221 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001222 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1223 * will return permission denied for all bits until close */
1224 if (!(flags & O_EXCL)) {
1225 /* ask server to check for all possible rights as results
1226 * are cached */
1227 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1228 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001229 }
David Howells7539bba2006-08-22 20:06:09 -04001230 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001231 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1232 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001233 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001234 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001235 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001236 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001237 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001238 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1239 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001240 case NFS4_OPEN_CLAIM_NULL:
1241 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1242 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1243 p->o_arg.fh = NFS_FH(dir);
1244 break;
1245 case NFS4_OPEN_CLAIM_PREVIOUS:
1246 case NFS4_OPEN_CLAIM_FH:
1247 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1248 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001249 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001250 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001251 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001252 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001253
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001254 p->o_arg.u.attrs = &p->attrs;
1255 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001256
1257 verf[0] = jiffies;
1258 verf[1] = current->pid;
1259 memcpy(p->o_arg.u.verifier.data, verf,
1260 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001261 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001262 p->c_arg.fh = &p->o_res.fh;
1263 p->c_arg.stateid = &p->o_res.stateid;
1264 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001265 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001266 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001268
1269err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001270 nfs4_label_free(p->a_label);
1271err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001272 nfs4_label_free(p->f_label);
1273err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274 kfree(p);
1275err:
1276 dput(parent);
1277 return NULL;
1278}
1279
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001281{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282 struct nfs4_opendata *p = container_of(kref,
1283 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001284 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001285
1286 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001287 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001288 if (p->state != NULL)
1289 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001290 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001291
Kinglong Meea49c2692015-07-27 15:31:38 +08001292 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001293 nfs4_label_free(p->f_label);
1294
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001296 dput(p->dentry);
1297 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001298 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001299 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 kfree(p);
1301}
1302
1303static void nfs4_opendata_put(struct nfs4_opendata *p)
1304{
1305 if (p != NULL)
1306 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001307}
1308
Trond Myklebust06f814a2006-01-03 09:55:07 +01001309static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1310{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001311 int ret;
1312
Trond Myklebust06f814a2006-01-03 09:55:07 +01001313 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001314 return ret;
1315}
1316
Trond Myklebust24311f82015-09-20 10:50:17 -04001317static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1318 fmode_t fmode)
1319{
1320 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1321 case FMODE_READ|FMODE_WRITE:
1322 return state->n_rdwr != 0;
1323 case FMODE_WRITE:
1324 return state->n_wronly != 0;
1325 case FMODE_READ:
1326 return state->n_rdonly != 0;
1327 }
1328 WARN_ON_ONCE(1);
1329 return false;
1330}
1331
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001332static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001333{
1334 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335
Trond Myklebust536e43d2012-01-17 22:04:26 -05001336 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337 goto out;
1338 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001339 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001340 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1341 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001342 break;
1343 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001344 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1345 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 break;
1347 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001348 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1349 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001350 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001352 return ret;
1353}
1354
Trond Myklebust2a606182015-08-19 22:30:00 -05001355static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1356 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001357{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001358 if (delegation == NULL)
1359 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001361 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001362 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1363 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001364 switch (claim) {
1365 case NFS4_OPEN_CLAIM_NULL:
1366 case NFS4_OPEN_CLAIM_FH:
1367 break;
1368 case NFS4_OPEN_CLAIM_PREVIOUS:
1369 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1370 break;
1371 default:
1372 return 0;
1373 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001374 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001375 return 1;
1376}
1377
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001378static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001379{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001380 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001381 case FMODE_WRITE:
1382 state->n_wronly++;
1383 break;
1384 case FMODE_READ:
1385 state->n_rdonly++;
1386 break;
1387 case FMODE_READ|FMODE_WRITE:
1388 state->n_rdwr++;
1389 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001390 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001391}
1392
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001393#ifdef CONFIG_NFS_V4_1
1394static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1395{
1396 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1397 return true;
1398 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1399 return true;
1400 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1401 return true;
1402 return false;
1403}
1404#endif /* CONFIG_NFS_V4_1 */
1405
Trond Myklebust4f14c192014-02-12 19:15:06 -05001406static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001407{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001408 struct nfs_client *clp = state->owner->so_server->nfs_client;
1409 bool need_recover = false;
1410
1411 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1412 need_recover = true;
1413 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1414 need_recover = true;
1415 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1416 need_recover = true;
1417 if (need_recover)
1418 nfs4_state_mark_reclaim_nograce(clp, state);
1419}
1420
Trond Myklebuste999e802014-02-10 18:20:47 -05001421static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001422 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001423{
1424 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1425 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001426 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001427 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001428 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001429 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001430 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001431 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1432 return true;
1433 return false;
1434}
1435
Trond Myklebustf95549c2015-01-23 18:06:09 -05001436static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1437{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001438 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1439 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001440 if (state->n_wronly)
1441 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1442 if (state->n_rdonly)
1443 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1444 if (state->n_rdwr)
1445 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001446 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447}
1448
Trond Myklebust226056c2014-02-11 10:41:07 -05001449static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1450 nfs4_stateid *stateid, fmode_t fmode)
1451{
1452 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1453 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1454 case FMODE_WRITE:
1455 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1456 break;
1457 case FMODE_READ:
1458 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1459 break;
1460 case 0:
1461 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1462 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1463 clear_bit(NFS_OPEN_STATE, &state->flags);
1464 }
1465 if (stateid == NULL)
1466 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001467 /* Handle OPEN+OPEN_DOWNGRADE races */
1468 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1469 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001470 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001471 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001472 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001473 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001474 nfs4_stateid_copy(&state->stateid, stateid);
1475 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001476}
1477
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001478static void nfs_clear_open_stateid(struct nfs4_state *state,
1479 nfs4_stateid *arg_stateid,
1480 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001481{
1482 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001483 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1484 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1485 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001487 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1488 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001489}
1490
Trond Myklebust1393d962016-09-22 13:39:13 -04001491static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1492 const nfs4_stateid *stateid, fmode_t fmode,
1493 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001494{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001496 case FMODE_READ:
1497 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1498 break;
1499 case FMODE_WRITE:
1500 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1501 break;
1502 case FMODE_READ|FMODE_WRITE:
1503 set_bit(NFS_O_RDWR_STATE, &state->flags);
1504 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001505 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001506 return;
1507 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1508 nfs4_stateid_copy(&state->stateid, stateid);
1509 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001510}
1511
Trond Myklebust1393d962016-09-22 13:39:13 -04001512static void __update_open_stateid(struct nfs4_state *state,
1513 const nfs4_stateid *open_stateid,
1514 const nfs4_stateid *deleg_stateid,
1515 fmode_t fmode,
1516 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001518 /*
1519 * Protect the call to nfs4_state_set_mode_locked and
1520 * serialise the stateid update
1521 */
Andrew Elble361cad32015-12-02 09:20:57 -05001522 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001523 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001524 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001525 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001526 set_bit(NFS_DELEGATED_STATE, &state->flags);
1527 }
1528 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001529 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001530 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001531 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001532 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Trond Myklebust1393d962016-09-22 13:39:13 -04001535static int update_open_stateid(struct nfs4_state *state,
1536 const nfs4_stateid *open_stateid,
1537 const nfs4_stateid *delegation,
1538 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001539{
Trond Myklebust1393d962016-09-22 13:39:13 -04001540 struct nfs_server *server = NFS_SERVER(state->inode);
1541 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001542 struct nfs_inode *nfsi = NFS_I(state->inode);
1543 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001544 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001545 int ret = 0;
1546
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001548
1549 rcu_read_lock();
1550 deleg_cur = rcu_dereference(nfsi->delegation);
1551 if (deleg_cur == NULL)
1552 goto no_delegation;
1553
1554 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001555 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001556 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001557 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001558 goto no_delegation_unlock;
1559
1560 if (delegation == NULL)
1561 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001562 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001563 goto no_delegation_unlock;
1564
Trond Myklebustb7391f42008-12-23 15:21:52 -05001565 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001566 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1567 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001568 ret = 1;
1569no_delegation_unlock:
1570 spin_unlock(&deleg_cur->lock);
1571no_delegation:
1572 rcu_read_unlock();
1573
1574 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001575 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001576 ret = 1;
1577 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001578 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001579 nfs4_schedule_state_manager(clp);
1580 if (freeme.type != 0)
1581 nfs4_test_and_free_stateid(server, &freeme,
1582 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001583
1584 return ret;
1585}
1586
Trond Myklebust39071e62015-01-24 15:07:56 -05001587static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1588 const nfs4_stateid *stateid)
1589{
1590 struct nfs4_state *state = lsp->ls_state;
1591 bool ret = false;
1592
1593 spin_lock(&state->state_lock);
1594 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1595 goto out_noupdate;
1596 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1597 goto out_noupdate;
1598 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1599 ret = true;
1600out_noupdate:
1601 spin_unlock(&state->state_lock);
1602 return ret;
1603}
Trond Myklebust34310432008-12-23 15:21:38 -05001604
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001605static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001606{
1607 struct nfs_delegation *delegation;
1608
1609 rcu_read_lock();
1610 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001611 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001612 rcu_read_unlock();
1613 return;
1614 }
1615 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001616 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001617}
1618
Trond Myklebust6ee41262007-07-08 14:11:36 -04001619static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001620{
1621 struct nfs4_state *state = opendata->state;
1622 struct nfs_inode *nfsi = NFS_I(state->inode);
1623 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001624 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001625 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001626 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627 nfs4_stateid stateid;
1628 int ret = -EAGAIN;
1629
Trond Myklebustaac00a82007-07-05 19:02:21 -04001630 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001631 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001632 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001633 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001634 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001635 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001636 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001637 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001638 rcu_read_lock();
1639 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001640 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001641 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001642 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001643 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001644 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001645 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001646 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001647 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001648 if (!opendata->is_recover) {
1649 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1650 if (ret != 0)
1651 goto out;
1652 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001654
1655 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001656 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001657 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001658 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001659out:
1660 return ERR_PTR(ret);
1661out_return_state:
1662 atomic_inc(&state->count);
1663 return state;
1664}
1665
Andy Adamsone23008e2012-10-02 21:07:32 -04001666static void
1667nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1668{
1669 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1670 struct nfs_delegation *delegation;
1671 int delegation_flags = 0;
1672
1673 rcu_read_lock();
1674 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1675 if (delegation)
1676 delegation_flags = delegation->flags;
1677 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001678 switch (data->o_arg.claim) {
1679 default:
1680 break;
1681 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1682 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001683 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1684 "returning a delegation for "
1685 "OPEN(CLAIM_DELEGATE_CUR)\n",
1686 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001687 return;
1688 }
1689 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001690 nfs_inode_set_delegation(state->inode,
1691 data->owner->so_cred,
1692 &data->o_res);
1693 else
1694 nfs_inode_reclaim_delegation(state->inode,
1695 data->owner->so_cred,
1696 &data->o_res);
1697}
1698
1699/*
1700 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1701 * and update the nfs4_state.
1702 */
1703static struct nfs4_state *
1704_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1705{
1706 struct inode *inode = data->state->inode;
1707 struct nfs4_state *state = data->state;
1708 int ret;
1709
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001710 if (!data->rpc_done) {
1711 if (data->rpc_status) {
1712 ret = data->rpc_status;
1713 goto err;
1714 }
1715 /* cached opens have already been processed */
1716 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001717 }
1718
Andy Adamsone23008e2012-10-02 21:07:32 -04001719 ret = nfs_refresh_inode(inode, &data->f_attr);
1720 if (ret)
1721 goto err;
1722
1723 if (data->o_res.delegation_type != 0)
1724 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001725update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001726 update_open_stateid(state, &data->o_res.stateid, NULL,
1727 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001728 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001729
1730 return state;
1731err:
1732 return ERR_PTR(ret);
1733
1734}
1735
1736static struct nfs4_state *
1737_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738{
1739 struct inode *inode;
1740 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001741 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001742
Trond Myklebustaac00a82007-07-05 19:02:21 -04001743 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001744 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001745 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001746 goto out;
1747 }
1748
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001749 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001750 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001751 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001752 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001753 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001754 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001755 goto err;
1756 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757 state = nfs4_get_open_state(inode, data->owner);
1758 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001759 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001760 if (data->o_res.delegation_type != 0)
1761 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001762 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001763 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001765out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001766 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001768err_put_inode:
1769 iput(inode);
1770err:
1771 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772}
1773
Andy Adamsone23008e2012-10-02 21:07:32 -04001774static struct nfs4_state *
1775nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1776{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001777 struct nfs4_state *ret;
1778
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001780 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1781 else
1782 ret = _nfs4_opendata_to_nfs4_state(data);
1783 nfs4_sequence_free_slot(&data->o_res.seq_res);
1784 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001785}
1786
Trond Myklebust864472e2006-01-03 09:55:15 +01001787static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1788{
1789 struct nfs_inode *nfsi = NFS_I(state->inode);
1790 struct nfs_open_context *ctx;
1791
1792 spin_lock(&state->inode->i_lock);
1793 list_for_each_entry(ctx, &nfsi->open_files, list) {
1794 if (ctx->state != state)
1795 continue;
1796 get_nfs_open_context(ctx);
1797 spin_unlock(&state->inode->i_lock);
1798 return ctx;
1799 }
1800 spin_unlock(&state->inode->i_lock);
1801 return ERR_PTR(-ENOENT);
1802}
1803
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001804static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1805 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001806{
1807 struct nfs4_opendata *opendata;
1808
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001809 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001810 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001811 if (opendata == NULL)
1812 return ERR_PTR(-ENOMEM);
1813 opendata->state = state;
1814 atomic_inc(&state->count);
1815 return opendata;
1816}
1817
Trond Myklebust24311f82015-09-20 10:50:17 -04001818static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1819 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001820{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001821 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001822 int ret;
1823
Trond Myklebust24311f82015-09-20 10:50:17 -04001824 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001825 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001826 opendata->o_arg.open_flags = 0;
1827 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001828 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1829 NFS_SB(opendata->dentry->d_sb),
1830 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001831 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1832 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1833 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001834 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001835 if (ret != 0)
1836 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001837 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001838 if (IS_ERR(newstate))
1839 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001840 if (newstate != opendata->state)
1841 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001842 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001843 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001844}
1845
1846static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1847{
Trond Myklebust864472e2006-01-03 09:55:15 +01001848 int ret;
1849
Trond Myklebust4f14c192014-02-12 19:15:06 -05001850 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1851 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1852 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1853 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001854 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001855 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001856 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001857 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001858 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1859 if (ret != 0)
1860 return ret;
1861 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1862 if (ret != 0)
1863 return ret;
1864 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1865 if (ret != 0)
1866 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001867 /*
1868 * We may have performed cached opens for all three recoveries.
1869 * Check if we need to update the current stateid.
1870 */
1871 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001872 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001873 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001874 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001875 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001876 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001877 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001878 return 0;
1879}
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881/*
1882 * OPEN_RECLAIM:
1883 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001885static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001887 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001888 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001889 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int status;
1891
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001892 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1893 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001894 if (IS_ERR(opendata))
1895 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001896 rcu_read_lock();
1897 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001898 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001899 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001900 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001901 opendata->o_arg.u.delegation_type = delegation_type;
1902 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001903 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return status;
1905}
1906
Trond Myklebust539cd032007-06-05 11:46:42 -04001907static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 struct nfs_server *server = NFS_SERVER(state->inode);
1910 struct nfs4_exception exception = { };
1911 int err;
1912 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001913 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001914 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001915 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1916 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001917 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001918 break;
1919 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 } while (exception.retry);
1921 return err;
1922}
1923
Trond Myklebust864472e2006-01-03 09:55:15 +01001924static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1925{
1926 struct nfs_open_context *ctx;
1927 int ret;
1928
1929 ctx = nfs4_state_find_open_context(state);
1930 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001931 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001932 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001933 put_nfs_open_context(ctx);
1934 return ret;
1935}
1936
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001937static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001939 switch (err) {
1940 default:
1941 printk(KERN_ERR "NFS: %s: unhandled error "
1942 "%d.\n", __func__, err);
1943 case 0:
1944 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001945 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001946 case -ESTALE:
1947 break;
1948 case -NFS4ERR_BADSESSION:
1949 case -NFS4ERR_BADSLOT:
1950 case -NFS4ERR_BAD_HIGH_SLOT:
1951 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1952 case -NFS4ERR_DEADSESSION:
1953 set_bit(NFS_DELEGATED_STATE, &state->flags);
1954 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1955 return -EAGAIN;
1956 case -NFS4ERR_STALE_CLIENTID:
1957 case -NFS4ERR_STALE_STATEID:
1958 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001959 /* Don't recall a delegation if it was lost */
1960 nfs4_schedule_lease_recovery(server->nfs_client);
1961 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001962 case -NFS4ERR_MOVED:
1963 nfs4_schedule_migration_recovery(server);
1964 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001965 case -NFS4ERR_LEASE_MOVED:
1966 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1967 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001968 case -NFS4ERR_DELEG_REVOKED:
1969 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001970 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001971 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001972 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001973 nfs_inode_find_state_and_recover(state->inode,
1974 stateid);
1975 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001976 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001977 case -NFS4ERR_DELAY:
1978 case -NFS4ERR_GRACE:
1979 set_bit(NFS_DELEGATED_STATE, &state->flags);
1980 ssleep(1);
1981 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001982 case -ENOMEM:
1983 case -NFS4ERR_DENIED:
1984 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1985 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return err;
1988}
1989
Trond Myklebust24311f82015-09-20 10:50:17 -04001990int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1991 struct nfs4_state *state, const nfs4_stateid *stateid,
1992 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001993{
1994 struct nfs_server *server = NFS_SERVER(state->inode);
1995 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001996 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001997
1998 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1999 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2000 if (IS_ERR(opendata))
2001 return PTR_ERR(opendata);
2002 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002003 write_seqlock(&state->seqlock);
2004 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2005 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002006 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2007 switch (type & (FMODE_READ|FMODE_WRITE)) {
2008 case FMODE_READ|FMODE_WRITE:
2009 case FMODE_WRITE:
2010 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2011 if (err)
2012 break;
2013 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2014 if (err)
2015 break;
2016 case FMODE_READ:
2017 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2018 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002019 nfs4_opendata_put(opendata);
2020 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2021}
2022
Chuck Leverbe05c862013-08-09 12:49:47 -04002023static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2024{
2025 struct nfs4_opendata *data = calldata;
2026
Peng Taocb04ad22014-06-11 05:24:15 +08002027 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2028 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002029}
2030
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002031static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2032{
2033 struct nfs4_opendata *data = calldata;
2034
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002035 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002036
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002037 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002038 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002039 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002040 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002041 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002042 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002043 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002044}
2045
2046static void nfs4_open_confirm_release(void *calldata)
2047{
2048 struct nfs4_opendata *data = calldata;
2049 struct nfs4_state *state = NULL;
2050
2051 /* If this request hasn't been cancelled, do nothing */
2052 if (data->cancelled == 0)
2053 goto out_free;
2054 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002055 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002056 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002057 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002058 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002059 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002061 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002062}
2063
2064static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002065 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002066 .rpc_call_done = nfs4_open_confirm_done,
2067 .rpc_release = nfs4_open_confirm_release,
2068};
2069
2070/*
2071 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2072 */
2073static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2074{
David Howells2b0143b2015-03-17 22:25:59 +00002075 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002076 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002077 struct rpc_message msg = {
2078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2079 .rpc_argp = &data->c_arg,
2080 .rpc_resp = &data->c_res,
2081 .rpc_cred = data->owner->so_cred,
2082 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002083 struct rpc_task_setup task_setup_data = {
2084 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002085 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002086 .callback_ops = &nfs4_open_confirm_ops,
2087 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002088 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002089 .flags = RPC_TASK_ASYNC,
2090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int status;
2092
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002093 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002094 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002095 data->rpc_done = 0;
2096 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002097 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002098 if (data->is_recover)
2099 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002100 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002101 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002102 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002103 status = nfs4_wait_for_completion_rpc_task(task);
2104 if (status != 0) {
2105 data->cancelled = 1;
2106 smp_wmb();
2107 } else
2108 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002109 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return status;
2111}
2112
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002115 struct nfs4_opendata *data = calldata;
2116 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002117 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002118 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002119
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002121 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002122 /*
2123 * Check if we still need to send an OPEN call, or if we can use
2124 * a delegation instead.
2125 */
2126 if (data->state != NULL) {
2127 struct nfs_delegation *delegation;
2128
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002129 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002130 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002131 rcu_read_lock();
2132 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002133 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002134 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002135 rcu_read_unlock();
2136 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002137 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002138 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002139 switch (claim) {
2140 default:
2141 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002142 case NFS4_OPEN_CLAIM_PREVIOUS:
2143 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2144 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002145 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002146 case NFS4_OPEN_CLAIM_FH:
2147 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002148 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2149 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002150 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002151 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002152 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002153 &data->o_res.seq_res,
2154 task) != 0)
2155 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002156
2157 /* Set the create mode (note dependency on the session type) */
2158 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2159 if (data->o_arg.open_flags & O_EXCL) {
2160 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2161 if (nfs4_has_persistent_session(clp))
2162 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2163 else if (clp->cl_mvops->minor_version > 0)
2164 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2165 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002166 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002167unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002168 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002169 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002170out_no_action:
2171 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002172out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002173 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002174}
2175
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002176static void nfs4_open_done(struct rpc_task *task, void *calldata)
2177{
2178 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002180 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002181
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002182 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002183 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002184
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002185 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002186 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2187 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002188 case S_IFREG:
2189 break;
2190 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002192 break;
2193 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002194 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002195 break;
2196 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002197 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002198 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002199 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002200 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002201 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2202 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002204 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002206
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207static void nfs4_open_release(void *calldata)
2208{
2209 struct nfs4_opendata *data = calldata;
2210 struct nfs4_state *state = NULL;
2211
2212 /* If this request hasn't been cancelled, do nothing */
2213 if (data->cancelled == 0)
2214 goto out_free;
2215 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002216 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217 goto out_free;
2218 /* In case we need an open_confirm, no cleanup! */
2219 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2220 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002222 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002223 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002224out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002225 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002226}
2227
2228static const struct rpc_call_ops nfs4_open_ops = {
2229 .rpc_call_prepare = nfs4_open_prepare,
2230 .rpc_call_done = nfs4_open_done,
2231 .rpc_release = nfs4_open_release,
2232};
2233
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002234static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002235{
David Howells2b0143b2015-03-17 22:25:59 +00002236 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237 struct nfs_server *server = NFS_SERVER(dir);
2238 struct nfs_openargs *o_arg = &data->o_arg;
2239 struct nfs_openres *o_res = &data->o_res;
2240 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002241 struct rpc_message msg = {
2242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2243 .rpc_argp = o_arg,
2244 .rpc_resp = o_res,
2245 .rpc_cred = data->owner->so_cred,
2246 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002247 struct rpc_task_setup task_setup_data = {
2248 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002249 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002250 .callback_ops = &nfs4_open_ops,
2251 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002252 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002253 .flags = RPC_TASK_ASYNC,
2254 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002255 int status;
2256
Chuck Levera9c92d62013-08-09 12:48:18 -04002257 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002258 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002259 data->rpc_done = 0;
2260 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002261 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002262 data->is_recover = 0;
2263 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002264 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002265 data->is_recover = 1;
2266 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002267 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002268 if (IS_ERR(task))
2269 return PTR_ERR(task);
2270 status = nfs4_wait_for_completion_rpc_task(task);
2271 if (status != 0) {
2272 data->cancelled = 1;
2273 smp_wmb();
2274 } else
2275 status = data->rpc_status;
2276 rpc_put_task(task);
2277
2278 return status;
2279}
2280
2281static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2282{
David Howells2b0143b2015-03-17 22:25:59 +00002283 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002284 struct nfs_openres *o_res = &data->o_res;
2285 int status;
2286
2287 status = nfs4_run_open_task(data, 1);
2288 if (status != 0 || !data->rpc_done)
2289 return status;
2290
Trond Myklebust6926afd2012-01-07 13:22:46 -05002291 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2292
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002293 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2294 status = _nfs4_proc_open_confirm(data);
2295 if (status != 0)
2296 return status;
2297 }
2298
2299 return status;
2300}
2301
Trond Myklebustf3792d62014-07-10 08:54:32 -04002302/*
2303 * Additional permission checks in order to distinguish between an
2304 * open for read, and an open for execute. This works around the
2305 * fact that NFSv4 OPEN treats read and execute permissions as being
2306 * the same.
2307 * Note that in the non-execute case, we want to turn off permission
2308 * checking if we just created a new file (POSIX open() semantics).
2309 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002310static int nfs4_opendata_access(struct rpc_cred *cred,
2311 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002312 struct nfs4_state *state, fmode_t fmode,
2313 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002314{
2315 struct nfs_access_entry cache;
2316 u32 mask;
2317
2318 /* access call failed or for some reason the server doesn't
2319 * support any access modes -- defer access call until later */
2320 if (opendata->o_res.access_supported == 0)
2321 return 0;
2322
2323 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002324 /*
2325 * Use openflags to check for exec, because fmode won't
2326 * always have FMODE_EXEC set when file open for exec.
2327 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002328 if (openflags & __FMODE_EXEC) {
2329 /* ONLY check for exec rights */
2330 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002331 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002332 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002333
2334 cache.cred = cred;
2335 cache.jiffies = jiffies;
2336 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2337 nfs_access_add_cache(state->inode, &cache);
2338
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002339 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002340 return 0;
2341
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002342 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002343}
2344
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002345/*
2346 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2347 */
2348static int _nfs4_proc_open(struct nfs4_opendata *data)
2349{
David Howells2b0143b2015-03-17 22:25:59 +00002350 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002351 struct nfs_server *server = NFS_SERVER(dir);
2352 struct nfs_openargs *o_arg = &data->o_arg;
2353 struct nfs_openres *o_res = &data->o_res;
2354 int status;
2355
2356 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002357 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002358 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002359 if (status != 0) {
2360 if (status == -NFS4ERR_BADNAME &&
2361 !(o_arg->open_flags & O_CREAT))
2362 return -ENOENT;
2363 return status;
2364 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002365
Trond Myklebust6926afd2012-01-07 13:22:46 -05002366 nfs_fattr_map_and_free_names(server, &data->f_attr);
2367
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002368 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002369 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002370 if (o_arg->open_flags & O_EXCL)
2371 data->file_created = 1;
2372 else if (o_res->cinfo.before != o_res->cinfo.after)
2373 data->file_created = 1;
2374 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002375 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2376 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002378 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002380 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002382 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2383 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002384 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002385 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Andy Adamsond83217c2011-03-01 01:34:17 +00002389static int nfs4_recover_expired_lease(struct nfs_server *server)
2390{
2391 return nfs4_client_recover_expired_lease(server->nfs_client);
2392}
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394/*
2395 * OPEN_EXPIRED:
2396 * reclaim state on the server after a network partition.
2397 * Assumes caller holds the appropriate lock
2398 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002399static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002401 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002402 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002404 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002405 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002406 if (IS_ERR(opendata))
2407 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002408 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002409 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002410 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002411 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002415static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002416{
Trond Myklebust539cd032007-06-05 11:46:42 -04002417 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002418 struct nfs4_exception exception = { };
2419 int err;
2420
2421 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002422 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002423 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002424 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2425 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002426 switch (err) {
2427 default:
2428 goto out;
2429 case -NFS4ERR_GRACE:
2430 case -NFS4ERR_DELAY:
2431 nfs4_handle_exception(server, err, &exception);
2432 err = 0;
2433 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002434 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002435out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002436 return err;
2437}
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002442 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Trond Myklebust864472e2006-01-03 09:55:15 +01002444 ctx = nfs4_state_find_open_context(state);
2445 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002446 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002447 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002448 put_nfs_open_context(ctx);
2449 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Trond Myklebust41020b62016-09-22 13:38:58 -04002452static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2453 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002454{
Trond Myklebust41020b62016-09-22 13:38:58 -04002455 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002456 write_seqlock(&state->seqlock);
2457 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2458 write_sequnlock(&state->seqlock);
2459 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2460}
2461
2462static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2463{
2464 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002465 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002466}
2467
2468static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2469{
2470 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2471 nfs40_clear_delegation_stateid(state);
2472 return nfs4_open_expired(sp, state);
2473}
2474
Trond Myklebust45870d62016-09-22 13:38:59 -04002475static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2476 nfs4_stateid *stateid,
2477 struct rpc_cred *cred)
2478{
2479 return -NFS4ERR_BAD_STATEID;
2480}
2481
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002482#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002483static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2484 nfs4_stateid *stateid,
2485 struct rpc_cred *cred)
2486{
2487 int status;
2488
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002489 switch (stateid->type) {
2490 default:
2491 break;
2492 case NFS4_INVALID_STATEID_TYPE:
2493 case NFS4_SPECIAL_STATEID_TYPE:
2494 return -NFS4ERR_BAD_STATEID;
2495 case NFS4_REVOKED_STATEID_TYPE:
2496 goto out_free;
2497 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002498
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002499 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002500 switch (status) {
2501 case -NFS4ERR_EXPIRED:
2502 case -NFS4ERR_ADMIN_REVOKED:
2503 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002504 break;
2505 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002506 return status;
2507 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002508out_free:
2509 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002510 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002511 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002512}
2513
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002514static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002515{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002516 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002517 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002518 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002519 struct rpc_cred *cred;
2520 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002521
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002522 /* Get the delegation credential for use by test/free_stateid */
2523 rcu_read_lock();
2524 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002525 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002526 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002527 return;
2528 }
2529
2530 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia461bbb92017-03-30 13:49:03 -04002531 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2532 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2533 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002534 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002535 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002536 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002537 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002538
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002539 cred = get_rpccred(delegation->cred);
2540 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002541 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002542 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002543 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002544 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002545
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002546 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002547}
2548
2549/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002550 * nfs41_check_expired_locks - possibly free a lock stateid
2551 *
2552 * @state: NFSv4 state for an inode
2553 *
2554 * Returns NFS_OK if recovery for this stateid is now finished.
2555 * Otherwise a negative NFS4ERR value is returned.
2556 */
2557static int nfs41_check_expired_locks(struct nfs4_state *state)
2558{
2559 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002560 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002561 struct nfs_server *server = NFS_SERVER(state->inode);
2562
2563 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2564 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002565
2566 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002567 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2568 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2569 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2570
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002571 atomic_inc(&lsp->ls_count);
2572 spin_unlock(&state->state_lock);
2573
2574 nfs4_put_lock_state(prev);
2575 prev = lsp;
2576
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002577 status = nfs41_test_and_free_expired_stateid(server,
2578 &lsp->ls_stateid,
2579 cred);
2580 trace_nfs4_test_lock_stateid(state, lsp, status);
2581 if (status == -NFS4ERR_EXPIRED ||
2582 status == -NFS4ERR_BAD_STATEID) {
2583 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002584 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002585 if (!recover_lost_locks)
2586 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2587 } else if (status != NFS_OK) {
2588 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002589 nfs4_put_lock_state(prev);
2590 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002591 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002592 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002593 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002594 }
2595 spin_unlock(&state->state_lock);
2596 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002597out:
2598 return ret;
2599}
2600
2601/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002602 * nfs41_check_open_stateid - possibly free an open stateid
2603 *
2604 * @state: NFSv4 state for an inode
2605 *
2606 * Returns NFS_OK if recovery for this stateid is now finished.
2607 * Otherwise a negative NFS4ERR value is returned.
2608 */
2609static int nfs41_check_open_stateid(struct nfs4_state *state)
2610{
2611 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002612 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002613 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002614 int status;
2615
Trond Myklebustb134fc42016-09-22 13:39:16 -04002616 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002617 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2618 if (nfs4_have_delegation(state->inode, state->state))
2619 return NFS_OK;
2620 return -NFS4ERR_OPENMODE;
2621 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002622 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002623 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002624 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002625 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002626 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002627 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2628 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2629 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002630 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002631 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002632 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002633 if (status != NFS_OK)
2634 return status;
2635 if (nfs_open_stateid_recover_openmode(state))
2636 return -NFS4ERR_OPENMODE;
2637 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002638}
2639
2640static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2641{
Chuck Levereb64cf92012-07-11 16:30:05 -04002642 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002643
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002644 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002645 status = nfs41_check_expired_locks(state);
2646 if (status != NFS_OK)
2647 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002648 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002649 if (status != NFS_OK)
2650 status = nfs4_open_expired(sp, state);
2651 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002652}
2653#endif
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002656 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2657 * fields corresponding to attributes that were used to store the verifier.
2658 * Make sure we clobber those fields in the later setattr call
2659 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002660static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2661 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002662{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002663 const u32 *attrset = opendata->o_res.attrset;
2664
2665 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002666 !(sattr->ia_valid & ATTR_ATIME_SET))
2667 sattr->ia_valid |= ATTR_ATIME;
2668
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002669 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002670 !(sattr->ia_valid & ATTR_MTIME_SET))
2671 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002672
2673 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddington56379492017-01-24 11:34:20 -05002674 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2675 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002676 sattr->ia_valid &= ~ATTR_MODE;
2677
2678 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2679 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002680}
2681
Trond Myklebustc21443c2013-02-07 14:26:21 -05002682static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2683 fmode_t fmode,
2684 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002685 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002686{
2687 struct nfs4_state_owner *sp = opendata->owner;
2688 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002689 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002690 struct nfs4_state *state;
2691 unsigned int seq;
2692 int ret;
2693
2694 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2695
2696 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002697 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002698 goto out;
2699
2700 state = nfs4_opendata_to_nfs4_state(opendata);
2701 ret = PTR_ERR(state);
2702 if (IS_ERR(state))
2703 goto out;
Trond Myklebust04653392017-02-08 11:29:46 -05002704 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002705 if (server->caps & NFS_CAP_POSIX_LOCK)
2706 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002707 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2708 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002709
Trond Myklebust275bb302013-05-29 13:11:28 -04002710 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002711 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002712 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002713 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002714 alias = d_exact_alias(dentry, state->inode);
2715 if (!alias)
2716 alias = d_splice_alias(igrab(state->inode), dentry);
2717 /* d_splice_alias() can't fail here - it's a non-directory */
2718 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002719 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002720 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002721 }
2722 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002723 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002724 }
2725
Trond Myklebustc21443c2013-02-07 14:26:21 -05002726 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2727 if (ret != 0)
2728 goto out;
2729
David Howells2b0143b2015-03-17 22:25:59 +00002730 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002731 nfs_inode_attach_open_context(ctx);
2732 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2733 nfs4_schedule_stateid_recovery(server, state);
2734 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002735out:
2736 return ret;
2737}
2738
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002739/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002740 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 */
Andy Adamson82be4172012-05-23 05:02:35 -04002742static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002743 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002744 int flags,
2745 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002746 struct nfs4_label *label,
2747 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 struct nfs4_state_owner *sp;
2750 struct nfs4_state *state = NULL;
2751 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002752 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002753 struct dentry *dentry = ctx->dentry;
2754 struct rpc_cred *cred = ctx->cred;
2755 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2756 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002757 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002758 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002759 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002763 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2764 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2766 goto out_err;
2767 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002768 status = nfs4_recover_expired_lease(server);
2769 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002770 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002771 if (d_really_is_positive(dentry))
2772 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002773 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002774 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002775 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002776 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002777 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002778 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002779 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
David Quigley14c43f72013-05-22 12:50:43 -04002781 if (label) {
2782 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2783 if (IS_ERR(olabel)) {
2784 status = PTR_ERR(olabel);
2785 goto err_opendata_put;
2786 }
2787 }
2788
Trond Myklebuste911b812014-03-26 13:24:37 -07002789 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2790 if (!opendata->f_attr.mdsthreshold) {
2791 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2792 if (!opendata->f_attr.mdsthreshold)
2793 goto err_free_label;
2794 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002795 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002796 }
David Howells2b0143b2015-03-17 22:25:59 +00002797 if (d_really_is_positive(dentry))
2798 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002799
Trond Myklebust3efb9722013-05-29 13:17:04 -04002800 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002801 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002802 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002803 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002804
NeilBrownefcbc042015-07-30 13:00:56 +10002805 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002806 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002807 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002808 /*
2809 * send create attributes which was not set by open
2810 * with an extra setattr.
2811 */
2812 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2813 nfs_fattr_init(opendata->o_res.f_attr);
2814 status = nfs4_do_setattr(state->inode, cred,
2815 opendata->o_res.f_attr, sattr,
2816 state, label, olabel);
2817 if (status == 0) {
2818 nfs_setattr_update_inode(state->inode, sattr,
2819 opendata->o_res.f_attr);
2820 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2821 }
David Quigley1775fd32013-05-22 12:50:42 -04002822 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002823 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002824 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002825 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002826
Trond Myklebuste911b812014-03-26 13:24:37 -07002827 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002828 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002829 opendata->f_attr.mdsthreshold = NULL;
2830 }
Andy Adamson82be4172012-05-23 05:02:35 -04002831
David Quigley14c43f72013-05-22 12:50:43 -04002832 nfs4_label_free(olabel);
2833
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002834 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002837err_free_label:
2838 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002839err_opendata_put:
2840 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002841err_put_state_owner:
2842 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return status;
2845}
2846
2847
Andy Adamson82be4172012-05-23 05:02:35 -04002848static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002849 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002850 int flags,
2851 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002852 struct nfs4_label *label,
2853 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002855 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 struct nfs4_exception exception = { };
2857 struct nfs4_state *res;
2858 int status;
2859
2860 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002861 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002862 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002863 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (status == 0)
2865 break;
2866 /* NOTE: BAD_SEQID means the server and client disagree about the
2867 * book-keeping w.r.t. state-changing operations
2868 * (OPEN/CLOSE/LOCK/LOCKU...)
2869 * It is actually a sign of a bug on the client or on the server.
2870 *
2871 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002872 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 * have unhashed the old state_owner for us, and that we can
2874 * therefore safely retry using a new one. We should still warn
2875 * the user though...
2876 */
2877 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002878 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002879 " returned a bad sequence-id error!\n",
2880 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 exception.retry = 1;
2882 continue;
2883 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002884 /*
2885 * BAD_STATEID on OPEN means that the server cancelled our
2886 * state before it received the OPEN_CONFIRM.
2887 * Recover by retrying the request as per the discussion
2888 * on Page 181 of RFC3530.
2889 */
2890 if (status == -NFS4ERR_BAD_STATEID) {
2891 exception.retry = 1;
2892 continue;
2893 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002894 if (status == -EAGAIN) {
2895 /* We must have found a delegation */
2896 exception.retry = 1;
2897 continue;
2898 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002899 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2900 continue;
2901 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 status, &exception));
2903 } while (exception.retry);
2904 return res;
2905}
2906
Trond Myklebust8487c472016-06-26 08:44:35 -04002907static int _nfs4_do_setattr(struct inode *inode,
2908 struct nfs_setattrargs *arg,
2909 struct nfs_setattrres *res,
2910 struct rpc_cred *cred,
2911 struct nfs4_state *state)
2912{
2913 struct nfs_server *server = NFS_SERVER(inode);
2914 struct rpc_message msg = {
2915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2916 .rpc_argp = arg,
2917 .rpc_resp = res,
2918 .rpc_cred = cred,
2919 };
2920 struct rpc_cred *delegation_cred = NULL;
2921 unsigned long timestamp = jiffies;
2922 fmode_t fmode;
2923 bool truncate;
2924 int status;
2925
2926 nfs_fattr_init(res->fattr);
2927
2928 /* Servers should only apply open mode checks for file size changes */
2929 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2930 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2931
2932 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2933 /* Use that stateid */
2934 } else if (truncate && state != NULL) {
2935 struct nfs_lockowner lockowner = {
2936 .l_owner = current->files,
2937 .l_pid = current->tgid,
2938 };
2939 if (!nfs4_valid_open_stateid(state))
2940 return -EBADF;
2941 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2942 &arg->stateid, &delegation_cred) == -EIO)
2943 return -EBADF;
2944 } else
2945 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2946 if (delegation_cred)
2947 msg.rpc_cred = delegation_cred;
2948
2949 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2950
2951 put_rpccred(delegation_cred);
2952 if (status == 0 && state != NULL)
2953 renew_lease(server, timestamp);
2954 trace_nfs4_setattr(inode, &arg->stateid, status);
2955 return status;
2956}
2957
2958static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2959 struct nfs_fattr *fattr, struct iattr *sattr,
2960 struct nfs4_state *state, struct nfs4_label *ilabel,
2961 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002963 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002965 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 .iap = sattr,
2967 .server = server,
2968 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002969 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 };
2971 struct nfs_setattrres res = {
2972 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002973 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 .server = server,
2975 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002976 struct nfs4_exception exception = {
2977 .state = state,
2978 .inode = inode,
2979 .stateid = &arg.stateid,
2980 };
2981 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
David Quigleyaa9c2662013-05-22 12:50:44 -04002983 arg.bitmask = nfs4_bitmask(server, ilabel);
2984 if (ilabel)
2985 arg.bitmask = nfs4_bitmask(server, olabel);
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04002988 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04002989 switch (err) {
2990 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002991 if (!(sattr->ia_valid & ATTR_SIZE)) {
2992 pr_warn_once("NFSv4: server %s is incorrectly "
2993 "applying open mode checks to "
2994 "a SETATTR that is not "
2995 "changing file size.\n",
2996 server->nfs_client->cl_hostname);
2997 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002998 if (state && !(state->state & FMODE_WRITE)) {
2999 err = -EBADF;
3000 if (sattr->ia_valid & ATTR_OPEN)
3001 err = -EACCES;
3002 goto out;
3003 }
3004 }
3005 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003007out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return err;
3009}
3010
Peng Tao500d7012015-09-22 11:35:22 +08003011static bool
3012nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3013{
3014 if (inode == NULL || !nfs_have_layout(inode))
3015 return false;
3016
3017 return pnfs_wait_on_layoutreturn(inode, task);
3018}
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020struct nfs4_closedata {
3021 struct inode *inode;
3022 struct nfs4_state *state;
3023 struct nfs_closeargs arg;
3024 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003025 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003026 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003027 bool roc;
3028 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029};
3030
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003031static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003032{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003033 struct nfs4_closedata *calldata = data;
3034 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003035 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003036
Fred Isamanf7e89172011-01-06 11:36:32 +00003037 if (calldata->roc)
3038 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003039 nfs4_put_open_state(calldata->state);
3040 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003041 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003042 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003043 kfree(calldata);
3044}
3045
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003046static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003048 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003051 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Chuck Levera3ca5652012-03-01 17:00:40 -05003053 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003054 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3055 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003056 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 /* hmm. we are done with the inode, and in the process of freeing
3058 * the state_owner. we keep this around to process errors
3059 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 switch (task->tk_status) {
3061 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003062 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003063 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003064 pnfs_roc_set_barrier(state->inode,
3065 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003066 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003067 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003068 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003070 case -NFS4ERR_EXPIRED:
3071 nfs4_free_revoked_stateid(server,
3072 &calldata->arg.stateid,
3073 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003074 case -NFS4ERR_OLD_STATEID:
3075 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003076 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003077 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003078 rpc_restart_call_prepare(task);
3079 goto out_release;
3080 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003081 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003082 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003084 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003085 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003086 goto out_release;
3087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003089 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3090 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003091out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003092 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003093 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003094 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003097static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003099 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003100 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003101 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003102 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003103 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003104
Chuck Levera3ca5652012-03-01 17:00:40 -05003105 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003106 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003107 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003108
Trond Myklebust88069f72009-12-08 08:33:16 -05003109 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003110 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003111 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3112 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3113 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003114 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003115 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003116 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003117 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003118 if (state->n_rdonly == 0)
3119 call_close |= is_rdonly;
3120 else if (is_rdonly)
3121 calldata->arg.fmode |= FMODE_READ;
3122 if (state->n_wronly == 0)
3123 call_close |= is_wronly;
3124 else if (is_wronly)
3125 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003126 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3127 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003128 } else if (is_rdwr)
3129 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3130
Trond Myklebust5cc78612016-11-14 11:19:56 -05003131 if (!nfs4_valid_open_stateid(state) ||
3132 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003133 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003134 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003135
3136 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003137 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003138 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003139 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003140
Peng Tao500d7012015-09-22 11:35:22 +08003141 if (nfs4_wait_on_layoutreturn(inode, task)) {
3142 nfs_release_seqid(calldata->arg.seqid);
3143 goto out_wait;
3144 }
3145
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003146 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003147 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003148 if (calldata->roc)
3149 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3150
Trond Myklebust6ae37332015-01-30 14:21:14 -05003151 calldata->arg.share_access =
3152 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3153 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003154
Trond Myklebust516a6af2005-10-27 22:12:41 -04003155 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003156 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003157 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003158 &calldata->arg.seq_args,
3159 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003160 task) != 0)
3161 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003162 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003163 return;
3164out_no_action:
3165 task->tk_action = NULL;
3166out_wait:
3167 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003170static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003171 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172 .rpc_call_done = nfs4_close_done,
3173 .rpc_release = nfs4_free_closedata,
3174};
3175
Peng Taofe08c542014-07-03 13:05:01 +08003176static bool nfs4_roc(struct inode *inode)
3177{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003178 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003179 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003180 return pnfs_roc(inode);
3181}
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183/*
3184 * It is possible for data to be read/written from a mem-mapped file
3185 * after the sys_close call (which hits the vfs layer as a flush).
3186 * This means that we can't safely call nfsv4 close on a file until
3187 * the inode is cleared. This in turn means that we are not good
3188 * NFSv4 citizens - we do not indicate to the server to update the file's
3189 * share state even when we are done with one of the three share
3190 * stateid's in the inode.
3191 *
3192 * NOTE: Caller must be holding the sp->so_owner semaphore!
3193 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003194int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003196 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003197 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003199 struct nfs4_state_owner *sp = state->owner;
3200 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003201 struct rpc_message msg = {
3202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3203 .rpc_cred = state->owner->so_cred,
3204 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003205 struct rpc_task_setup task_setup_data = {
3206 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003207 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003208 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003209 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003210 .flags = RPC_TASK_ASYNC,
3211 };
Trond Myklebust95121352005-10-18 14:20:12 -07003212 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003214 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3215 &task_setup_data.rpc_client, &msg);
3216
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003217 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003219 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003220 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003221 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003223 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003225 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3226 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003227 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003228 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003229 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003230 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003231 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003232 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003233 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003234 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003235 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003236
Trond Myklebust1174dd12010-12-21 10:52:24 -05003237 msg.rpc_argp = &calldata->arg;
3238 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003239 task_setup_data.callback_data = calldata;
3240 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003241 if (IS_ERR(task))
3242 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003243 status = 0;
3244 if (wait)
3245 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003246 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003247 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003248out_free_calldata:
3249 kfree(calldata);
3250out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003251 nfs4_put_open_state(state);
3252 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003253 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
Trond Myklebust2b484292010-09-17 10:56:51 -04003256static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003257nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3258 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003261 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3262
3263 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Trond Myklebust565277f2007-10-15 18:17:53 -04003265 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003266 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003267
3268 nfs4_label_release_security(label);
3269
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003270 if (IS_ERR(state))
3271 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003272 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Trond Myklebust1185a552009-12-03 15:54:02 -05003275static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003276{
3277 if (ctx->state == NULL)
3278 return;
3279 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003280 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003281 else
Al Viro643168c2011-06-22 18:20:23 -04003282 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Trond Myklebustb944dba2013-11-04 15:20:20 -05003285#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3286#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003287#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3290{
Kinglong Mee8c612822015-08-26 21:12:58 +08003291 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003292 struct nfs4_server_caps_arg args = {
3293 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003294 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 struct nfs4_server_caps_res res = {};
3297 struct rpc_message msg = {
3298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003299 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .rpc_resp = &res,
3301 };
3302 int status;
3303
Kinglong Mee8c612822015-08-26 21:12:58 +08003304 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3305 FATTR4_WORD0_FH_EXPIRE_TYPE |
3306 FATTR4_WORD0_LINK_SUPPORT |
3307 FATTR4_WORD0_SYMLINK_SUPPORT |
3308 FATTR4_WORD0_ACLSUPPORT;
3309 if (minorversion)
3310 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3311
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003314 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003315 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003316 case 0:
3317 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3318 res.attr_bitmask[2] = 0;
3319 break;
3320 case 1:
3321 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3322 break;
3323 case 2:
3324 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003327 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3328 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3329 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3330 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003331 NFS_CAP_CTIME|NFS_CAP_MTIME|
3332 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003333 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3334 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 server->caps |= NFS_CAP_ACLS;
3336 if (res.has_links != 0)
3337 server->caps |= NFS_CAP_HARDLINKS;
3338 if (res.has_symlinks != 0)
3339 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003340 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3341 server->caps |= NFS_CAP_FILEID;
3342 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3343 server->caps |= NFS_CAP_MODE;
3344 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3345 server->caps |= NFS_CAP_NLINK;
3346 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3347 server->caps |= NFS_CAP_OWNER;
3348 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3349 server->caps |= NFS_CAP_OWNER_GROUP;
3350 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3351 server->caps |= NFS_CAP_ATIME;
3352 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3353 server->caps |= NFS_CAP_CTIME;
3354 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3355 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003356#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3357 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3358 server->caps |= NFS_CAP_SECURITY_LABEL;
3359#endif
3360 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3361 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003362 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003363
Trond Myklebusta65318b2009-03-11 14:10:28 -04003364 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3365 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3366 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003367 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003368 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3369 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003371 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return status;
3375}
3376
Trond Myklebust55a97592006-06-09 09:34:19 -04003377int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
3379 struct nfs4_exception exception = { };
3380 int err;
3381 do {
3382 err = nfs4_handle_exception(server,
3383 _nfs4_server_capabilities(server, fhandle),
3384 &exception);
3385 } while (exception.retry);
3386 return err;
3387}
3388
3389static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3390 struct nfs_fsinfo *info)
3391{
David Quigleyaa9c2662013-05-22 12:50:44 -04003392 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003394 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 };
3396 struct nfs4_lookup_res res = {
3397 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003398 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 .fh = fhandle,
3400 };
3401 struct rpc_message msg = {
3402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3403 .rpc_argp = &args,
3404 .rpc_resp = &res,
3405 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003406
David Quigleyaa9c2662013-05-22 12:50:44 -04003407 bitmask[0] = nfs4_fattr_bitmap[0];
3408 bitmask[1] = nfs4_fattr_bitmap[1];
3409 /*
3410 * Process the label in the upcoming getfattr
3411 */
3412 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3413
Trond Myklebust0e574af2005-10-27 22:12:38 -04003414 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003415 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
3418static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3419 struct nfs_fsinfo *info)
3420{
3421 struct nfs4_exception exception = { };
3422 int err;
3423 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003424 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003425 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003426 switch (err) {
3427 case 0:
3428 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003429 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003430 default:
3431 err = nfs4_handle_exception(server, err, &exception);
3432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return err;
3436}
3437
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003438static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3439 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3440{
Trond Myklebustc2190662013-08-26 19:23:04 -04003441 struct rpc_auth_create_args auth_args = {
3442 .pseudoflavor = flavor,
3443 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003444 struct rpc_auth *auth;
3445 int ret;
3446
Trond Myklebustc2190662013-08-26 19:23:04 -04003447 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003448 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003449 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003450 goto out;
3451 }
3452 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003453out:
3454 return ret;
3455}
3456
Chuck Lever9a744ba2013-03-16 15:56:02 -04003457/*
3458 * Retry pseudoroot lookup with various security flavors. We do this when:
3459 *
3460 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3461 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3462 *
3463 * Returns zero on success, or a negative NFS4ERR value, or a
3464 * negative errno value.
3465 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003466static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003467 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003469 /* Per 3530bis 15.33.5 */
3470 static const rpc_authflavor_t flav_array[] = {
3471 RPC_AUTH_GSS_KRB5P,
3472 RPC_AUTH_GSS_KRB5I,
3473 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003474 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003475 RPC_AUTH_NULL,
3476 };
3477 int status = -EPERM;
3478 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003480 if (server->auth_info.flavor_len > 0) {
3481 /* try each flavor specified by user */
3482 for (i = 0; i < server->auth_info.flavor_len; i++) {
3483 status = nfs4_lookup_root_sec(server, fhandle, info,
3484 server->auth_info.flavors[i]);
3485 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3486 continue;
3487 break;
3488 }
3489 } else {
3490 /* no flavors specified by user, try default list */
3491 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3492 status = nfs4_lookup_root_sec(server, fhandle, info,
3493 flav_array[i]);
3494 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3495 continue;
3496 break;
3497 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003498 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003499
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003500 /*
3501 * -EACCESS could mean that the user doesn't have correct permissions
3502 * to access the mount. It could also mean that we tried to mount
3503 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3504 * existing mount programs don't handle -EACCES very well so it should
3505 * be mapped to -EPERM instead.
3506 */
3507 if (status == -EACCES)
3508 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003509 return status;
3510}
3511
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003512/**
3513 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3514 * @server: initialized nfs_server handle
3515 * @fhandle: we fill in the pseudo-fs root file handle
3516 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003517 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003518 *
3519 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003520 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003521int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003522 struct nfs_fsinfo *info,
3523 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003524{
Andre Przywarac7757072015-04-23 17:17:40 +01003525 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003526
Andre Przywarac7757072015-04-23 17:17:40 +01003527 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003528 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003529
3530 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003531 status = server->nfs_client->cl_mvops->find_root_sec(server,
3532 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 if (status == 0)
3535 status = nfs4_server_capabilities(server, fhandle);
3536 if (status == 0)
3537 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003538
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003539 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Bryan Schumakerbae36242012-05-10 15:07:31 -04003542static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3543 struct nfs_fsinfo *info)
3544{
3545 int error;
3546 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003547 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003548
3549 error = nfs4_server_capabilities(server, mntfh);
3550 if (error < 0) {
3551 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3552 return error;
3553 }
3554
David Quigley14c43f72013-05-22 12:50:43 -04003555 label = nfs4_label_alloc(server, GFP_KERNEL);
3556 if (IS_ERR(label))
3557 return PTR_ERR(label);
3558
David Quigley1775fd32013-05-22 12:50:42 -04003559 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003560 if (error < 0) {
3561 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003562 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003563 }
3564
3565 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3566 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3567 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3568
David Quigley14c43f72013-05-22 12:50:43 -04003569err_free_label:
3570 nfs4_label_free(label);
3571
Bryan Schumakerbae36242012-05-10 15:07:31 -04003572 return error;
3573}
3574
Manoj Naik6b97fd32006-06-09 09:34:29 -04003575/*
3576 * Get locations and (maybe) other attributes of a referral.
3577 * Note that we'll actually follow the referral later when
3578 * we detect fsid mismatch in inode revalidation
3579 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003580static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3581 const struct qstr *name, struct nfs_fattr *fattr,
3582 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003583{
3584 int status = -ENOMEM;
3585 struct page *page = NULL;
3586 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003587
3588 page = alloc_page(GFP_KERNEL);
3589 if (page == NULL)
3590 goto out;
3591 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3592 if (locations == NULL)
3593 goto out;
3594
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003595 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003596 if (status != 0)
3597 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003598
3599 /*
3600 * If the fsid didn't change, this is a migration event, not a
3601 * referral. Cause us to drop into the exception handler, which
3602 * will kick off migration recovery.
3603 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003604 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003605 dprintk("%s: server did not return a different fsid for"
3606 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003607 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003608 goto out;
3609 }
Andy Adamson533eb462011-06-13 18:25:56 -04003610 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3611 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003612
Andy Adamson533eb462011-06-13 18:25:56 -04003613 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003614 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003615 memset(fhandle, 0, sizeof(struct nfs_fh));
3616out:
3617 if (page)
3618 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003619 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003620 return status;
3621}
3622
David Quigley1775fd32013-05-22 12:50:42 -04003623static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3624 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 struct nfs4_getattr_arg args = {
3627 .fh = fhandle,
3628 .bitmask = server->attr_bitmask,
3629 };
3630 struct nfs4_getattr_res res = {
3631 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003632 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 .server = server,
3634 };
3635 struct rpc_message msg = {
3636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3637 .rpc_argp = &args,
3638 .rpc_resp = &res,
3639 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003640
3641 args.bitmask = nfs4_bitmask(server, label);
3642
Trond Myklebust0e574af2005-10-27 22:12:38 -04003643 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003644 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
David Quigley1775fd32013-05-22 12:50:42 -04003647static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3648 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
3650 struct nfs4_exception exception = { };
3651 int err;
3652 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003653 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3654 trace_nfs4_getattr(server, fhandle, fattr, err);
3655 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 &exception);
3657 } while (exception.retry);
3658 return err;
3659}
3660
3661/*
3662 * The file is not closed if it is opened due to the a request to change
3663 * the size of the file. The open call will not be needed once the
3664 * VFS layer lookup-intents are implemented.
3665 *
3666 * Close is called when the inode is destroyed.
3667 * If we haven't opened the file for O_WRONLY, we
3668 * need to in the size_change case to obtain a stateid.
3669 *
3670 * Got race?
3671 * Because OPEN is always done by name in nfsv4, it is
3672 * possible that we opened a different file by the same
3673 * name. We can recognize this race condition, but we
3674 * can't do anything about it besides returning an error.
3675 *
3676 * This will be fixed with VFS changes (lookup-intent).
3677 */
3678static int
3679nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3680 struct iattr *sattr)
3681{
David Howells2b0143b2015-03-17 22:25:59 +00003682 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003683 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003684 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003685 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 int status;
3687
Peng Tao88ac8152014-09-12 11:04:10 +08003688 if (pnfs_ld_layoutret_on_setattr(inode) &&
3689 sattr->ia_valid & ATTR_SIZE &&
3690 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003691 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003692
Trond Myklebust0e574af2005-10-27 22:12:38 -04003693 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
Andy Adamson26699402012-05-30 16:12:24 -04003695 /* Deal with open(O_TRUNC) */
3696 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003697 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003698
3699 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003700 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003701 return 0;
3702
Trond Myklebustd5308382005-11-04 15:33:38 -05003703 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003704 if (sattr->ia_valid & ATTR_FILE) {
3705 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003706
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003707 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003708 if (ctx) {
3709 cred = ctx->cred;
3710 state = ctx->state;
3711 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003712 }
3713
David Quigley14c43f72013-05-22 12:50:43 -04003714 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3715 if (IS_ERR(label))
3716 return PTR_ERR(label);
3717
3718 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003719 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003720 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003721 nfs_setsecurity(inode, fattr, label);
3722 }
David Quigley14c43f72013-05-22 12:50:43 -04003723 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return status;
3725}
3726
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003727static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3728 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003729 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003730{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003731 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003732 int status;
3733 struct nfs4_lookup_arg args = {
3734 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003735 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003736 .name = name,
3737 };
3738 struct nfs4_lookup_res res = {
3739 .server = server,
3740 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003741 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003742 .fh = fhandle,
3743 };
3744 struct rpc_message msg = {
3745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3746 .rpc_argp = &args,
3747 .rpc_resp = &res,
3748 };
3749
David Quigleyaa9c2662013-05-22 12:50:44 -04003750 args.bitmask = nfs4_bitmask(server, label);
3751
David Howells2b3de442006-08-22 20:06:09 -04003752 nfs_fattr_init(fattr);
3753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003755 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 dprintk("NFS reply lookup: %d\n", status);
3757 return status;
3758}
3759
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003760static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003761{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003762 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003763 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003764 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3765 fattr->nlink = 2;
3766}
3767
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003768static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003769 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003770 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003771{
3772 struct nfs4_exception exception = { };
3773 struct rpc_clnt *client = *clnt;
3774 int err;
3775 do {
David Quigley1775fd32013-05-22 12:50:42 -04003776 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003777 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003778 switch (err) {
3779 case -NFS4ERR_BADNAME:
3780 err = -ENOENT;
3781 goto out;
3782 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003783 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003784 if (err == -NFS4ERR_MOVED)
3785 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003786 goto out;
3787 case -NFS4ERR_WRONGSEC:
3788 err = -EPERM;
3789 if (client != *clnt)
3790 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003791 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003792 if (IS_ERR(client))
3793 return PTR_ERR(client);
3794
3795 exception.retry = 1;
3796 break;
3797 default:
3798 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3799 }
3800 } while (exception.retry);
3801
3802out:
3803 if (err == 0)
3804 *clnt = client;
3805 else if (client != *clnt)
3806 rpc_shutdown_client(client);
3807
3808 return err;
3809}
3810
Al Virobeffb8f2016-07-20 16:34:42 -04003811static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003812 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3813 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003815 int status;
3816 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003817
David Quigley1775fd32013-05-22 12:50:42 -04003818 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003819 if (client != NFS_CLIENT(dir)) {
3820 rpc_shutdown_client(client);
3821 nfs_fixup_secinfo_attributes(fattr);
3822 }
3823 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003826struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003827nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003828 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3829{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003830 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003831 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003832
David Quigley1775fd32013-05-22 12:50:42 -04003833 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003834 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003835 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003836 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003837}
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3840{
Trond Myklebust76b32992007-08-10 17:45:11 -04003841 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 struct nfs4_accessargs args = {
3843 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003844 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003846 struct nfs4_accessres res = {
3847 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003848 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 struct rpc_message msg = {
3850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3851 .rpc_argp = &args,
3852 .rpc_resp = &res,
3853 .rpc_cred = entry->cred,
3854 };
3855 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003856 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
3858 /*
3859 * Determine which access bits we want to ask for...
3860 */
3861 if (mode & MAY_READ)
3862 args.access |= NFS4_ACCESS_READ;
3863 if (S_ISDIR(inode->i_mode)) {
3864 if (mode & MAY_WRITE)
3865 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3866 if (mode & MAY_EXEC)
3867 args.access |= NFS4_ACCESS_LOOKUP;
3868 } else {
3869 if (mode & MAY_WRITE)
3870 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3871 if (mode & MAY_EXEC)
3872 args.access |= NFS4_ACCESS_EXECUTE;
3873 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003874
3875 res.fattr = nfs_alloc_fattr();
3876 if (res.fattr == NULL)
3877 return -ENOMEM;
3878
Bryan Schumaker7c513052011-03-24 17:12:24 +00003879 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003881 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003882 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003884 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 return status;
3886}
3887
3888static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3889{
3890 struct nfs4_exception exception = { };
3891 int err;
3892 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003893 err = _nfs4_proc_access(inode, entry);
3894 trace_nfs4_access(inode, err);
3895 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 &exception);
3897 } while (exception.retry);
3898 return err;
3899}
3900
3901/*
3902 * TODO: For the time being, we don't try to get any attributes
3903 * along with any of the zero-copy operations READ, READDIR,
3904 * READLINK, WRITE.
3905 *
3906 * In the case of the first three, we want to put the GETATTR
3907 * after the read-type operation -- this is because it is hard
3908 * to predict the length of a GETATTR response in v4, and thus
3909 * align the READ data correctly. This means that the GETATTR
3910 * may end up partially falling into the page cache, and we should
3911 * shift it into the 'tail' of the xdr_buf before processing.
3912 * To do this efficiently, we need to know the total length
3913 * of data received, which doesn't seem to be available outside
3914 * of the RPC layer.
3915 *
3916 * In the case of WRITE, we also want to put the GETATTR after
3917 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003918 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 *
3920 * Both of these changes to the XDR layer would in fact be quite
3921 * minor, but I decided to leave them for a subsequent patch.
3922 */
3923static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3924 unsigned int pgbase, unsigned int pglen)
3925{
3926 struct nfs4_readlink args = {
3927 .fh = NFS_FH(inode),
3928 .pgbase = pgbase,
3929 .pglen = pglen,
3930 .pages = &page,
3931 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003932 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 struct rpc_message msg = {
3934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3935 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003936 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 };
3938
Bryan Schumaker7c513052011-03-24 17:12:24 +00003939 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 -07003940}
3941
3942static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3943 unsigned int pgbase, unsigned int pglen)
3944{
3945 struct nfs4_exception exception = { };
3946 int err;
3947 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003948 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3949 trace_nfs4_readlink(inode, err);
3950 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 &exception);
3952 } while (exception.retry);
3953 return err;
3954}
3955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003957 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959static int
3960nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003961 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
David Quigleyaa9c2662013-05-22 12:50:44 -04003963 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003964 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 int status = 0;
3967
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003968 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3969 if (IS_ERR(ctx))
3970 return PTR_ERR(ctx);
3971
David Quigleyaa9c2662013-05-22 12:50:44 -04003972 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3973
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003974 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003975 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 if (IS_ERR(state)) {
3977 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003978 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003981 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003982 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return status;
3984}
3985
Al Virobeffb8f2016-07-20 16:34:42 -04003986static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
Trond Myklebust16e42952005-10-27 22:12:44 -04003988 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003989 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003991 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003993 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003994 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003995 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3998 .rpc_argp = &args,
3999 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004001 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004002
Bryan Schumaker7c513052011-03-24 17:12:24 +00004003 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004004 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004005 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 return status;
4007}
4008
Al Virobeffb8f2016-07-20 16:34:42 -04004009static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
4011 struct nfs4_exception exception = { };
4012 int err;
4013 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004014 err = _nfs4_proc_remove(dir, name);
4015 trace_nfs4_remove(dir, name, err);
4016 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 &exception);
4018 } while (exception.retry);
4019 return err;
4020}
4021
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004022static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004024 struct nfs_server *server = NFS_SERVER(dir);
4025 struct nfs_removeargs *args = msg->rpc_argp;
4026 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004028 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004030 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004031
4032 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004035static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4036{
Al Viro884be172016-04-28 23:56:31 -04004037 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004038 &data->args.seq_args,
4039 &data->res.seq_res,
4040 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004043static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004045 struct nfs_unlinkdata *data = task->tk_calldata;
4046 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004047
Trond Myklebust14516c32010-07-31 14:29:06 -04004048 if (!nfs4_sequence_done(task, &res->seq_res))
4049 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004050 if (nfs4_async_handle_error(task, res->server, NULL,
4051 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004052 return 0;
4053 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004054 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
Jeff Laytond3d41522010-09-17 17:31:57 -04004057static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4058{
4059 struct nfs_server *server = NFS_SERVER(dir);
4060 struct nfs_renameargs *arg = msg->rpc_argp;
4061 struct nfs_renameres *res = msg->rpc_resp;
4062
4063 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004064 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004065 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004066}
4067
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004068static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4069{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004070 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4071 &data->args.seq_args,
4072 &data->res.seq_res,
4073 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004074}
4075
4076static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4077 struct inode *new_dir)
4078{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004079 struct nfs_renamedata *data = task->tk_calldata;
4080 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004081
4082 if (!nfs4_sequence_done(task, &res->seq_res))
4083 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004084 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004085 return 0;
4086
4087 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004088 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004089 return 1;
4090}
4091
Al Virobeffb8f2016-07-20 16:34:42 -04004092static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004094 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 struct nfs4_link_arg arg = {
4096 .fh = NFS_FH(inode),
4097 .dir_fh = NFS_FH(dir),
4098 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004099 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004101 struct nfs4_link_res res = {
4102 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004103 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004104 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 struct rpc_message msg = {
4106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4107 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004108 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004110 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Trond Myklebust136f2622010-04-16 16:22:49 -04004112 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004113 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004114 goto out;
4115
David Quigley14c43f72013-05-22 12:50:43 -04004116 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4117 if (IS_ERR(res.label)) {
4118 status = PTR_ERR(res.label);
4119 goto out;
4120 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004121 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004122
Bryan Schumaker7c513052011-03-24 17:12:24 +00004123 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004124 if (!status) {
4125 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004126 status = nfs_post_op_update_inode(inode, res.fattr);
4127 if (!status)
4128 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004129 }
David Quigley14c43f72013-05-22 12:50:43 -04004130
4131
4132 nfs4_label_free(res.label);
4133
Trond Myklebust136f2622010-04-16 16:22:49 -04004134out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004135 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 return status;
4137}
4138
Al Virobeffb8f2016-07-20 16:34:42 -04004139static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140{
4141 struct nfs4_exception exception = { };
4142 int err;
4143 do {
4144 err = nfs4_handle_exception(NFS_SERVER(inode),
4145 _nfs4_proc_link(inode, dir, name),
4146 &exception);
4147 } while (exception.retry);
4148 return err;
4149}
4150
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004151struct nfs4_createdata {
4152 struct rpc_message msg;
4153 struct nfs4_create_arg arg;
4154 struct nfs4_create_res res;
4155 struct nfs_fh fh;
4156 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004157 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004158};
4159
4160static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004161 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004162{
4163 struct nfs4_createdata *data;
4164
4165 data = kzalloc(sizeof(*data), GFP_KERNEL);
4166 if (data != NULL) {
4167 struct nfs_server *server = NFS_SERVER(dir);
4168
David Quigley14c43f72013-05-22 12:50:43 -04004169 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4170 if (IS_ERR(data->label))
4171 goto out_free;
4172
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004173 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4174 data->msg.rpc_argp = &data->arg;
4175 data->msg.rpc_resp = &data->res;
4176 data->arg.dir_fh = NFS_FH(dir);
4177 data->arg.server = server;
4178 data->arg.name = name;
4179 data->arg.attrs = sattr;
4180 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004181 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004182 data->res.server = server;
4183 data->res.fh = &data->fh;
4184 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004185 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004186 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187 }
4188 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004189out_free:
4190 kfree(data);
4191 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192}
4193
4194static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4195{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004196 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004197 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004198 if (status == 0) {
4199 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004200 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201 }
4202 return status;
4203}
4204
4205static void nfs4_free_createdata(struct nfs4_createdata *data)
4206{
David Quigley14c43f72013-05-22 12:50:43 -04004207 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 kfree(data);
4209}
4210
Chuck Lever4f390c12006-08-22 20:06:22 -04004211static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004212 struct page *page, unsigned int len, struct iattr *sattr,
4213 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004215 struct nfs4_createdata *data;
4216 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Chuck Lever94a6d752006-08-22 20:06:23 -04004218 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004219 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004220
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221 status = -ENOMEM;
4222 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4223 if (data == NULL)
4224 goto out;
4225
4226 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4227 data->arg.u.symlink.pages = &page;
4228 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004229 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004231 status = nfs4_do_create(dir, dentry, data);
4232
4233 nfs4_free_createdata(data);
4234out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return status;
4236}
4237
Chuck Lever4f390c12006-08-22 20:06:22 -04004238static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004239 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
4241 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004242 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004244
4245 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004248 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4249 trace_nfs4_symlink(dir, &dentry->d_name, err);
4250 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 &exception);
4252 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004253
4254 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return err;
4256}
4257
4258static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004259 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004261 struct nfs4_createdata *data;
4262 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004264 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4265 if (data == NULL)
4266 goto out;
4267
David Quigley1775fd32013-05-22 12:50:42 -04004268 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004269 status = nfs4_do_create(dir, dentry, data);
4270
4271 nfs4_free_createdata(data);
4272out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return status;
4274}
4275
4276static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4277 struct iattr *sattr)
4278{
4279 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004280 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004282
David Quigleyaa9c2662013-05-22 12:50:44 -04004283 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4284
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004285 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004287 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4288 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4289 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 &exception);
4291 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004292 nfs4_label_release_security(label);
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return err;
4295}
4296
4297static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004298 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
David Howells2b0143b2015-03-17 22:25:59 +00004300 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 struct nfs4_readdir_arg args = {
4302 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004303 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 .pgbase = 0,
4305 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004306 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004307 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 };
4309 struct nfs4_readdir_res res;
4310 struct rpc_message msg = {
4311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4312 .rpc_argp = &args,
4313 .rpc_resp = &res,
4314 .rpc_cred = cred,
4315 };
4316 int status;
4317
Al Viro6de14722013-09-16 10:53:17 -04004318 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4319 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004320 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004321 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004323 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 -05004324 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004325 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004326 status += args.pgbase;
4327 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004328
4329 nfs_invalidate_atime(dir);
4330
Harvey Harrison3110ff82008-05-02 13:42:44 -07004331 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 return status;
4333}
4334
4335static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004336 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
4338 struct nfs4_exception exception = { };
4339 int err;
4340 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004341 err = _nfs4_proc_readdir(dentry, cred, cookie,
4342 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004343 trace_nfs4_readdir(d_inode(dentry), err);
4344 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 &exception);
4346 } while (exception.retry);
4347 return err;
4348}
4349
4350static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004351 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004353 struct nfs4_createdata *data;
4354 int mode = sattr->ia_mode;
4355 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004357 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4358 if (data == NULL)
4359 goto out;
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004362 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004364 data->arg.ftype = NF4BLK;
4365 data->arg.u.device.specdata1 = MAJOR(rdev);
4366 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
4368 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004369 data->arg.ftype = NF4CHR;
4370 data->arg.u.device.specdata1 = MAJOR(rdev);
4371 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004372 } else if (!S_ISSOCK(mode)) {
4373 status = -EINVAL;
4374 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 }
David Quigley1775fd32013-05-22 12:50:42 -04004376
David Quigleyaa9c2662013-05-22 12:50:44 -04004377 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004378 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004379out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004380 nfs4_free_createdata(data);
4381out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return status;
4383}
4384
4385static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4386 struct iattr *sattr, dev_t rdev)
4387{
4388 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004389 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004391
David Quigleyaa9c2662013-05-22 12:50:44 -04004392 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4393
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004394 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004396 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4397 trace_nfs4_mknod(dir, &dentry->d_name, err);
4398 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 &exception);
4400 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004401
4402 nfs4_label_release_security(label);
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 return err;
4405}
4406
4407static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4408 struct nfs_fsstat *fsstat)
4409{
4410 struct nfs4_statfs_arg args = {
4411 .fh = fhandle,
4412 .bitmask = server->attr_bitmask,
4413 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004414 struct nfs4_statfs_res res = {
4415 .fsstat = fsstat,
4416 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 struct rpc_message msg = {
4418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4419 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004420 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 };
4422
Trond Myklebust0e574af2005-10-27 22:12:38 -04004423 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004424 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
4427static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4428{
4429 struct nfs4_exception exception = { };
4430 int err;
4431 do {
4432 err = nfs4_handle_exception(server,
4433 _nfs4_proc_statfs(server, fhandle, fsstat),
4434 &exception);
4435 } while (exception.retry);
4436 return err;
4437}
4438
4439static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4440 struct nfs_fsinfo *fsinfo)
4441{
4442 struct nfs4_fsinfo_arg args = {
4443 .fh = fhandle,
4444 .bitmask = server->attr_bitmask,
4445 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004446 struct nfs4_fsinfo_res res = {
4447 .fsinfo = fsinfo,
4448 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 struct rpc_message msg = {
4450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4451 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004452 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 };
4454
Bryan Schumaker7c513052011-03-24 17:12:24 +00004455 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}
4457
4458static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4459{
4460 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004461 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 int err;
4463
4464 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004465 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004466 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004467 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004468 nfs4_set_lease_period(server->nfs_client,
4469 fsinfo->lease_time * HZ,
4470 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004471 break;
4472 }
4473 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 } while (exception.retry);
4475 return err;
4476}
4477
4478static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4479{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004480 int error;
4481
Trond Myklebust0e574af2005-10-27 22:12:38 -04004482 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004483 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004484 if (error == 0) {
4485 /* block layout checks this! */
4486 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004487 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004488 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004489
4490 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491}
4492
4493static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4494 struct nfs_pathconf *pathconf)
4495{
4496 struct nfs4_pathconf_arg args = {
4497 .fh = fhandle,
4498 .bitmask = server->attr_bitmask,
4499 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004500 struct nfs4_pathconf_res res = {
4501 .pathconf = pathconf,
4502 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 struct rpc_message msg = {
4504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4505 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004506 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 };
4508
4509 /* None of the pathconf attributes are mandatory to implement */
4510 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4511 memset(pathconf, 0, sizeof(*pathconf));
4512 return 0;
4513 }
4514
Trond Myklebust0e574af2005-10-27 22:12:38 -04004515 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004516 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
4519static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4520 struct nfs_pathconf *pathconf)
4521{
4522 struct nfs4_exception exception = { };
4523 int err;
4524
4525 do {
4526 err = nfs4_handle_exception(server,
4527 _nfs4_proc_pathconf(server, fhandle, pathconf),
4528 &exception);
4529 } while (exception.retry);
4530 return err;
4531}
4532
Trond Myklebust5521abf2013-03-16 20:54:34 -04004533int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004534 const struct nfs_open_context *ctx,
4535 const struct nfs_lock_context *l_ctx,
4536 fmode_t fmode)
4537{
4538 const struct nfs_lockowner *lockowner = NULL;
4539
4540 if (l_ctx != NULL)
4541 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004542 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004543}
4544EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4545
Trond Myklebust5521abf2013-03-16 20:54:34 -04004546static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4547 const struct nfs_open_context *ctx,
4548 const struct nfs_lock_context *l_ctx,
4549 fmode_t fmode)
4550{
4551 nfs4_stateid current_stateid;
4552
Trond Myklebuste1253be2014-03-05 08:44:23 -05004553 /* If the current stateid represents a lost lock, then exit */
4554 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4555 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004556 return nfs4_stateid_match(stateid, &current_stateid);
4557}
4558
4559static bool nfs4_error_stateid_expired(int err)
4560{
4561 switch (err) {
4562 case -NFS4ERR_DELEG_REVOKED:
4563 case -NFS4ERR_ADMIN_REVOKED:
4564 case -NFS4ERR_BAD_STATEID:
4565 case -NFS4ERR_STALE_STATEID:
4566 case -NFS4ERR_OLD_STATEID:
4567 case -NFS4ERR_OPENMODE:
4568 case -NFS4ERR_EXPIRED:
4569 return true;
4570 }
4571 return false;
4572}
4573
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004574static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004576 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004578 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004579 if (task->tk_status < 0) {
4580 struct nfs4_exception exception = {
4581 .inode = hdr->inode,
4582 .state = hdr->args.context->state,
4583 .stateid = &hdr->args.stateid,
4584 };
4585 task->tk_status = nfs4_async_handle_exception(task,
4586 server, task->tk_status, &exception);
4587 if (exception.retry) {
4588 rpc_restart_call_prepare(task);
4589 return -EAGAIN;
4590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004594 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596}
4597
Trond Myklebust5521abf2013-03-16 20:54:34 -04004598static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004599 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004600{
4601
4602 if (!nfs4_error_stateid_expired(task->tk_status) ||
4603 nfs4_stateid_is_current(&args->stateid,
4604 args->context,
4605 args->lock_context,
4606 FMODE_READ))
4607 return false;
4608 rpc_restart_call_prepare(task);
4609 return true;
4610}
4611
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004613{
4614
4615 dprintk("--> %s\n", __func__);
4616
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004617 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004618 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004619 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004620 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004621 if (task->tk_status > 0)
4622 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4624 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004625}
4626
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004627static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4628 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004631 if (!hdr->pgio_done_cb)
4632 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004633 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004634 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004637static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4638 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004639{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4641 &hdr->args.seq_args,
4642 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004643 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004644 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004645 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4646 hdr->args.lock_context,
4647 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004648 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004650 return -EIO;
4651 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652}
4653
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004654static int nfs4_write_done_cb(struct rpc_task *task,
4655 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004658
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004659 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004660 if (task->tk_status < 0) {
4661 struct nfs4_exception exception = {
4662 .inode = hdr->inode,
4663 .state = hdr->args.context->state,
4664 .stateid = &hdr->args.stateid,
4665 };
4666 task->tk_status = nfs4_async_handle_exception(task,
4667 NFS_SERVER(inode), task->tk_status,
4668 &exception);
4669 if (exception.retry) {
4670 rpc_restart_call_prepare(task);
4671 return -EAGAIN;
4672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004674 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004676 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004677 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
4680
Trond Myklebust5521abf2013-03-16 20:54:34 -04004681static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004682 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004683{
4684
4685 if (!nfs4_error_stateid_expired(task->tk_status) ||
4686 nfs4_stateid_is_current(&args->stateid,
4687 args->context,
4688 args->lock_context,
4689 FMODE_WRITE))
4690 return false;
4691 rpc_restart_call_prepare(task);
4692 return true;
4693}
4694
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004695static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004696{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004698 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004700 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004701 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4702 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004703}
4704
Trond Myklebust5a37f852012-04-28 14:55:16 -04004705static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004706bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004707{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004708 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004710 return false;
4711 /* Otherwise, request attributes if and only if we don't hold
4712 * a delegation
4713 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004714 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004715}
Fred Isamana69aef12011-03-03 15:13:47 +00004716
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004717static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4718 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004720 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004721
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004722 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4723 hdr->args.bitmask = NULL;
4724 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004725 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004727
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 if (!hdr->pgio_done_cb)
4729 hdr->pgio_done_cb = nfs4_write_done_cb;
4730 hdr->res.server = server;
4731 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004733 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004734 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
Fred Isaman0b7c0152012-04-20 14:47:39 -04004737static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4738{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004739 nfs4_setup_sequence(NFS_SERVER(data->inode),
4740 &data->args.seq_args,
4741 &data->res.seq_res,
4742 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Fred Isaman0b7c0152012-04-20 14:47:39 -04004745static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004748
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004749 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004750 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4751 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004752 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004753 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756}
4757
Fred Isaman0b7c0152012-04-20 14:47:39 -04004758static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004759{
4760 if (!nfs4_sequence_done(task, &data->res.seq_res))
4761 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004762 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004763}
4764
Fred Isaman0b7c0152012-04-20 14:47:39 -04004765static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004767 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004768
Fred Isaman0b7c0152012-04-20 14:47:39 -04004769 if (data->commit_done_cb == NULL)
4770 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004771 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004772 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004773 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774}
4775
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004776struct nfs4_renewdata {
4777 struct nfs_client *client;
4778 unsigned long timestamp;
4779};
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781/*
4782 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4783 * standalone procedure for queueing an asynchronous RENEW.
4784 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004785static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004786{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004787 struct nfs4_renewdata *data = calldata;
4788 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004789
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004790 if (atomic_read(&clp->cl_count) > 1)
4791 nfs4_schedule_state_renewal(clp);
4792 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004793 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004794}
4795
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004796static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004798 struct nfs4_renewdata *data = calldata;
4799 struct nfs_client *clp = data->client;
4800 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004802 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004803 switch (task->tk_status) {
4804 case 0:
4805 break;
4806 case -NFS4ERR_LEASE_MOVED:
4807 nfs4_schedule_lease_moved_recovery(clp);
4808 break;
4809 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004810 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004811 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4812 return;
4813 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004814 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004815 return;
4816 }
4817 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004819 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004822static const struct rpc_call_ops nfs4_renew_ops = {
4823 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004824 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004825};
4826
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004827static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
4829 struct rpc_message msg = {
4830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4831 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004832 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004834 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004836 if (renew_flags == 0)
4837 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004838 if (!atomic_inc_not_zero(&clp->cl_count))
4839 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004840 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004841 if (data == NULL)
4842 return -ENOMEM;
4843 data->client = clp;
4844 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004845 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004846 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004849static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
4851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4853 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004854 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 };
4856 unsigned long now = jiffies;
4857 int status;
4858
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004859 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if (status < 0)
4861 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004862 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return 0;
4864}
4865
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004866static inline int nfs4_server_supports_acls(struct nfs_server *server)
4867{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004868 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004869}
4870
Trond Myklebust21f498c2012-08-24 10:59:25 -04004871/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4872 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004873 * the stack.
4874 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004875#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004876
Neil Hormane9e3d722011-03-04 19:26:03 -05004877static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004878 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004879{
4880 struct page *newpage, **spages;
4881 int rc = 0;
4882 size_t len;
4883 spages = pages;
4884
4885 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004886 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004887 newpage = alloc_page(GFP_KERNEL);
4888
4889 if (newpage == NULL)
4890 goto unwind;
4891 memcpy(page_address(newpage), buf, len);
4892 buf += len;
4893 buflen -= len;
4894 *pages++ = newpage;
4895 rc++;
4896 } while (buflen != 0);
4897
4898 return rc;
4899
4900unwind:
4901 for(; rc > 0; rc--)
4902 __free_page(spages[rc-1]);
4903 return -ENOMEM;
4904}
4905
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004906struct nfs4_cached_acl {
4907 int cached;
4908 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004909 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004910};
4911
4912static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004913{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004914 struct nfs_inode *nfsi = NFS_I(inode);
4915
4916 spin_lock(&inode->i_lock);
4917 kfree(nfsi->nfs4_acl);
4918 nfsi->nfs4_acl = acl;
4919 spin_unlock(&inode->i_lock);
4920}
4921
4922static void nfs4_zap_acl_attr(struct inode *inode)
4923{
4924 nfs4_set_cached_acl(inode, NULL);
4925}
4926
4927static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4928{
4929 struct nfs_inode *nfsi = NFS_I(inode);
4930 struct nfs4_cached_acl *acl;
4931 int ret = -ENOENT;
4932
4933 spin_lock(&inode->i_lock);
4934 acl = nfsi->nfs4_acl;
4935 if (acl == NULL)
4936 goto out;
4937 if (buf == NULL) /* user is just asking for length */
4938 goto out_len;
4939 if (acl->cached == 0)
4940 goto out;
4941 ret = -ERANGE; /* see getxattr(2) man page */
4942 if (acl->len > buflen)
4943 goto out;
4944 memcpy(buf, acl->data, acl->len);
4945out_len:
4946 ret = acl->len;
4947out:
4948 spin_unlock(&inode->i_lock);
4949 return ret;
4950}
4951
Sachin Prabhu5794d212012-04-17 14:36:40 +01004952static 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 +00004953{
4954 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004955 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004956
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004957 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004958 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004959 if (acl == NULL)
4960 goto out;
4961 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004962 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004963 } else {
4964 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4965 if (acl == NULL)
4966 goto out;
4967 acl->cached = 0;
4968 }
4969 acl->len = acl_len;
4970out:
4971 nfs4_set_cached_acl(inode, acl);
4972}
4973
Andy Adamsonbf118a32011-12-07 11:55:27 -05004974/*
4975 * The getxattr API returns the required buffer length when called with a
4976 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4977 * the required buf. On a NULL buf, we send a page of data to the server
4978 * guessing that the ACL request can be serviced by a page. If so, we cache
4979 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4980 * the cache. If not so, we throw away the page, and cache the required
4981 * length. The next getxattr call will then produce another round trip to
4982 * the server, this time with the input buf of the required size.
4983 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004984static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004985{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05004986 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004987 struct nfs_getaclargs args = {
4988 .fh = NFS_FH(inode),
4989 .acl_pages = pages,
4990 .acl_len = buflen,
4991 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004992 struct nfs_getaclres res = {
4993 .acl_len = buflen,
4994 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004995 struct rpc_message msg = {
4996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4997 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004998 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004999 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005000 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005001 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005002
Trond Myklebust21f498c2012-08-24 10:59:25 -04005003 if (npages > ARRAY_SIZE(pages))
5004 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005005
Andy Adamsonbf118a32011-12-07 11:55:27 -05005006 for (i = 0; i < npages; i++) {
5007 pages[i] = alloc_page(GFP_KERNEL);
5008 if (!pages[i])
5009 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005010 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005011
5012 /* for decoding across pages */
5013 res.acl_scratch = alloc_page(GFP_KERNEL);
5014 if (!res.acl_scratch)
5015 goto out_free;
5016
Andy Adamsonbf118a32011-12-07 11:55:27 -05005017 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005018
Peng Taode040be2012-01-10 22:42:47 +08005019 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005020 __func__, buf, buflen, npages, args.acl_len);
5021 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5022 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005023 if (ret)
5024 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005025
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005026 /* Handle the case where the passed-in buffer is too short */
5027 if (res.acl_flags & NFS4_ACL_TRUNC) {
5028 /* Did the user only issue a request for the acl length? */
5029 if (buf == NULL)
5030 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005031 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005032 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005033 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005034 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005035 if (buf) {
5036 if (res.acl_len > buflen) {
5037 ret = -ERANGE;
5038 goto out_free;
5039 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005040 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005041 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005042out_ok:
5043 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005044out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005045 for (i = 0; i < npages; i++)
5046 if (pages[i])
5047 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005048 if (res.acl_scratch)
5049 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005050 return ret;
5051}
5052
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005053static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5054{
5055 struct nfs4_exception exception = { };
5056 ssize_t ret;
5057 do {
5058 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005059 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005060 if (ret >= 0)
5061 break;
5062 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5063 } while (exception.retry);
5064 return ret;
5065}
5066
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005067static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5068{
5069 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005070 int ret;
5071
5072 if (!nfs4_server_supports_acls(server))
5073 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005074 ret = nfs_revalidate_inode(server, inode);
5075 if (ret < 0)
5076 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005077 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5078 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005079 ret = nfs4_read_cached_acl(inode, buf, buflen);
5080 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005081 /* -ENOENT is returned if there is no ACL or if there is an ACL
5082 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005083 return ret;
5084 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005085}
5086
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005087static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005088{
5089 struct nfs_server *server = NFS_SERVER(inode);
5090 struct page *pages[NFS4ACL_MAXPAGES];
5091 struct nfs_setaclargs arg = {
5092 .fh = NFS_FH(inode),
5093 .acl_pages = pages,
5094 .acl_len = buflen,
5095 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005096 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005097 struct rpc_message msg = {
5098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5099 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005100 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005101 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005102 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005103 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005104
5105 if (!nfs4_server_supports_acls(server))
5106 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005107 if (npages > ARRAY_SIZE(pages))
5108 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005109 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005110 if (i < 0)
5111 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005112 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005113 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005114
5115 /*
5116 * Free each page after tx, so the only ref left is
5117 * held by the network stack
5118 */
5119 for (; i > 0; i--)
5120 put_page(pages[i-1]);
5121
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005122 /*
5123 * Acl update can result in inode attribute update.
5124 * so mark the attribute cache invalid.
5125 */
5126 spin_lock(&inode->i_lock);
5127 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5128 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005129 nfs_access_zap_cache(inode);
5130 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005131 return ret;
5132}
5133
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005134static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5135{
5136 struct nfs4_exception exception = { };
5137 int err;
5138 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005139 err = __nfs4_proc_set_acl(inode, buf, buflen);
5140 trace_nfs4_set_acl(inode, err);
5141 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005142 &exception);
5143 } while (exception.retry);
5144 return err;
5145}
5146
David Quigleyaa9c2662013-05-22 12:50:44 -04005147#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5148static int _nfs4_get_security_label(struct inode *inode, void *buf,
5149 size_t buflen)
5150{
5151 struct nfs_server *server = NFS_SERVER(inode);
5152 struct nfs_fattr fattr;
5153 struct nfs4_label label = {0, 0, buflen, buf};
5154
5155 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005156 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005157 .fh = NFS_FH(inode),
5158 .bitmask = bitmask,
5159 };
5160 struct nfs4_getattr_res res = {
5161 .fattr = &fattr,
5162 .label = &label,
5163 .server = server,
5164 };
5165 struct rpc_message msg = {
5166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005167 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005168 .rpc_resp = &res,
5169 };
5170 int ret;
5171
5172 nfs_fattr_init(&fattr);
5173
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005174 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005175 if (ret)
5176 return ret;
5177 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5178 return -ENOENT;
5179 if (buflen < label.len)
5180 return -ERANGE;
5181 return 0;
5182}
5183
5184static int nfs4_get_security_label(struct inode *inode, void *buf,
5185 size_t buflen)
5186{
5187 struct nfs4_exception exception = { };
5188 int err;
5189
5190 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5191 return -EOPNOTSUPP;
5192
5193 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005194 err = _nfs4_get_security_label(inode, buf, buflen);
5195 trace_nfs4_get_security_label(inode, err);
5196 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005197 &exception);
5198 } while (exception.retry);
5199 return err;
5200}
5201
5202static int _nfs4_do_set_security_label(struct inode *inode,
5203 struct nfs4_label *ilabel,
5204 struct nfs_fattr *fattr,
5205 struct nfs4_label *olabel)
5206{
5207
5208 struct iattr sattr = {0};
5209 struct nfs_server *server = NFS_SERVER(inode);
5210 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005211 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005212 .fh = NFS_FH(inode),
5213 .iap = &sattr,
5214 .server = server,
5215 .bitmask = bitmask,
5216 .label = ilabel,
5217 };
5218 struct nfs_setattrres res = {
5219 .fattr = fattr,
5220 .label = olabel,
5221 .server = server,
5222 };
5223 struct rpc_message msg = {
5224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005225 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005226 .rpc_resp = &res,
5227 };
5228 int status;
5229
Jeff Layton12207f62013-11-01 10:49:32 -04005230 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005231
Jeff Layton12207f62013-11-01 10:49:32 -04005232 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005233 if (status)
5234 dprintk("%s failed: %d\n", __func__, status);
5235
5236 return status;
5237}
5238
5239static int nfs4_do_set_security_label(struct inode *inode,
5240 struct nfs4_label *ilabel,
5241 struct nfs_fattr *fattr,
5242 struct nfs4_label *olabel)
5243{
5244 struct nfs4_exception exception = { };
5245 int err;
5246
5247 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005248 err = _nfs4_do_set_security_label(inode, ilabel,
5249 fattr, olabel);
5250 trace_nfs4_set_security_label(inode, err);
5251 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005252 &exception);
5253 } while (exception.retry);
5254 return err;
5255}
5256
5257static int
Al Viro59301222016-05-27 10:19:30 -04005258nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005259{
5260 struct nfs4_label ilabel, *olabel = NULL;
5261 struct nfs_fattr fattr;
5262 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005263 int status;
5264
5265 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5266 return -EOPNOTSUPP;
5267
5268 nfs_fattr_init(&fattr);
5269
5270 ilabel.pi = 0;
5271 ilabel.lfs = 0;
5272 ilabel.label = (char *)buf;
5273 ilabel.len = buflen;
5274
5275 cred = rpc_lookup_cred();
5276 if (IS_ERR(cred))
5277 return PTR_ERR(cred);
5278
5279 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5280 if (IS_ERR(olabel)) {
5281 status = -PTR_ERR(olabel);
5282 goto out;
5283 }
5284
5285 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5286 if (status == 0)
5287 nfs_setsecurity(inode, &fattr, olabel);
5288
5289 nfs4_label_free(olabel);
5290out:
5291 put_rpccred(cred);
5292 return status;
5293}
5294#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5295
5296
Chuck Leverf0920752012-05-21 22:45:41 -04005297static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5298 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005299{
5300 __be32 verf[2];
5301
Chuck Lever2c820d92012-05-21 22:45:33 -04005302 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5303 /* An impossible timestamp guarantees this value
5304 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005305 verf[0] = cpu_to_be32(U32_MAX);
5306 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005307 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005308 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005309 u64 ns = ktime_to_ns(nn->boot_time);
5310
5311 verf[0] = cpu_to_be32(ns >> 32);
5312 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005313 }
Chuck Levercd937102012-03-02 17:14:31 -05005314 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5315}
5316
Jeff Laytona3192682015-06-09 19:43:59 -04005317static int
5318nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005319{
Jeff Laytona3192682015-06-09 19:43:59 -04005320 size_t len;
5321 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005322
Trond Myklebustceb3a162015-01-03 15:16:04 -05005323 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005324 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005325
Jeff Laytona3192682015-06-09 19:43:59 -04005326 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005327 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005328 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5329 1 +
5330 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5331 1;
5332 rcu_read_unlock();
5333
5334 if (len > NFS4_OPAQUE_LIMIT + 1)
5335 return -EINVAL;
5336
5337 /*
5338 * Since this string is allocated at mount time, and held until the
5339 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5340 * about a memory-reclaim deadlock.
5341 */
5342 str = kmalloc(len, GFP_KERNEL);
5343 if (!str)
5344 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005345
Chuck Levere984a552012-09-14 17:24:21 -04005346 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005347 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005348 clp->cl_ipaddr,
5349 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5350 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005351 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005352
Jeff Laytona3192682015-06-09 19:43:59 -04005353 clp->cl_owner_id = str;
5354 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005355}
5356
Jeff Layton873e3852015-06-09 19:44:00 -04005357static int
5358nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005359{
Jeff Layton873e3852015-06-09 19:44:00 -04005360 size_t len;
5361 char *str;
5362
5363 len = 10 + 10 + 1 + 10 + 1 +
5364 strlen(nfs4_client_id_uniquifier) + 1 +
5365 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5366
5367 if (len > NFS4_OPAQUE_LIMIT + 1)
5368 return -EINVAL;
5369
5370 /*
5371 * Since this string is allocated at mount time, and held until the
5372 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5373 * about a memory-reclaim deadlock.
5374 */
5375 str = kmalloc(len, GFP_KERNEL);
5376 if (!str)
5377 return -ENOMEM;
5378
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005379 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005380 clp->rpc_ops->version, clp->cl_minorversion,
5381 nfs4_client_id_uniquifier,
5382 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005383 clp->cl_owner_id = str;
5384 return 0;
5385}
5386
5387static int
5388nfs4_init_uniform_client_string(struct nfs_client *clp)
5389{
Jeff Layton873e3852015-06-09 19:44:00 -04005390 size_t len;
5391 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005392
5393 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005394 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005395
5396 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005397 return nfs4_init_uniquifier_client_string(clp);
5398
5399 len = 10 + 10 + 1 + 10 + 1 +
5400 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5401
5402 if (len > NFS4_OPAQUE_LIMIT + 1)
5403 return -EINVAL;
5404
5405 /*
5406 * Since this string is allocated at mount time, and held until the
5407 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5408 * about a memory-reclaim deadlock.
5409 */
5410 str = kmalloc(len, GFP_KERNEL);
5411 if (!str)
5412 return -ENOMEM;
5413
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005414 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005415 clp->rpc_ops->version, clp->cl_minorversion,
5416 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005417 clp->cl_owner_id = str;
5418 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005419}
5420
Chuck Lever706cb8d2014-03-12 12:51:47 -04005421/*
5422 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5423 * services. Advertise one based on the address family of the
5424 * clientaddr.
5425 */
5426static unsigned int
5427nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5428{
5429 if (strchr(clp->cl_ipaddr, ':') != NULL)
5430 return scnprintf(buf, len, "tcp6");
5431 else
5432 return scnprintf(buf, len, "tcp");
5433}
5434
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005435static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5436{
5437 struct nfs4_setclientid *sc = calldata;
5438
5439 if (task->tk_status == 0)
5440 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5441}
5442
5443static const struct rpc_call_ops nfs4_setclientid_ops = {
5444 .rpc_call_done = nfs4_setclientid_done,
5445};
5446
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005447/**
5448 * nfs4_proc_setclientid - Negotiate client ID
5449 * @clp: state data structure
5450 * @program: RPC program for NFSv4 callback service
5451 * @port: IP port number for NFS4 callback service
5452 * @cred: RPC credential to use for this call
5453 * @res: where to place the result
5454 *
5455 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5456 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005457int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5458 unsigned short port, struct rpc_cred *cred,
5459 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
5461 nfs4_verifier sc_verifier;
5462 struct nfs4_setclientid setclientid = {
5463 .sc_verifier = &sc_verifier,
5464 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005465 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 };
5467 struct rpc_message msg = {
5468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5469 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005470 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005471 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005473 struct rpc_task *task;
5474 struct rpc_task_setup task_setup_data = {
5475 .rpc_client = clp->cl_rpcclient,
5476 .rpc_message = &msg,
5477 .callback_ops = &nfs4_setclientid_ops,
5478 .callback_data = &setclientid,
5479 .flags = RPC_TASK_TIMEOUT,
5480 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005481 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Chuck Leverde734832012-07-11 16:30:50 -04005483 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005484 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005485
5486 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5487 status = nfs4_init_uniform_client_string(clp);
5488 else
Jeff Laytona3192682015-06-09 19:43:59 -04005489 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005490
5491 if (status)
5492 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005493
Chuck Leverde734832012-07-11 16:30:50 -04005494 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005495 setclientid.sc_netid_len =
5496 nfs4_init_callback_netid(clp,
5497 setclientid.sc_netid,
5498 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005499 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005500 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 clp->cl_ipaddr, port >> 8, port & 255);
5502
Jeff Layton3a6bb732015-06-09 19:43:57 -04005503 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005504 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005505 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005506 task = rpc_run_task(&task_setup_data);
5507 if (IS_ERR(task)) {
5508 status = PTR_ERR(task);
5509 goto out;
5510 }
5511 status = task->tk_status;
5512 if (setclientid.sc_cred) {
5513 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5514 put_rpccred(setclientid.sc_cred);
5515 }
5516 rpc_put_task(task);
5517out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005518 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005519 dprintk("NFS reply setclientid: %d\n", status);
5520 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005523/**
5524 * nfs4_proc_setclientid_confirm - Confirm client ID
5525 * @clp: state data structure
5526 * @res: result of a previous SETCLIENTID
5527 * @cred: RPC credential to use for this call
5528 *
5529 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5530 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005531int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005532 struct nfs4_setclientid_res *arg,
5533 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 struct rpc_message msg = {
5536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005537 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005538 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 int status;
5541
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005542 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5543 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5544 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005545 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005546 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005547 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 return status;
5549}
5550
Trond Myklebustfe650402006-01-03 09:55:18 +01005551struct nfs4_delegreturndata {
5552 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005553 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005554 struct nfs_fh fh;
5555 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005556 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005557 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005558 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005559 struct inode *inode;
5560 bool roc;
5561 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005562};
5563
Trond Myklebustfe650402006-01-03 09:55:18 +01005564static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5565{
5566 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005567
Trond Myklebust14516c32010-07-31 14:29:06 -04005568 if (!nfs4_sequence_done(task, &data->res.seq_res))
5569 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005570
Trond Myklebustca8acf82013-08-13 10:36:56 -04005571 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005572 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005573 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005574 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005575 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005576 case -NFS4ERR_ADMIN_REVOKED:
5577 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005578 case -NFS4ERR_EXPIRED:
5579 nfs4_free_revoked_stateid(data->res.server,
5580 data->args.stateid,
5581 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005582 case -NFS4ERR_BAD_STATEID:
5583 case -NFS4ERR_OLD_STATEID:
5584 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005585 task->tk_status = 0;
5586 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005587 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005588 if (nfs4_async_handle_error(task, data->res.server,
5589 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005590 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005591 return;
5592 }
5593 }
5594 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005595 if (data->roc && data->rpc_status == 0)
5596 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005597}
5598
5599static void nfs4_delegreturn_release(void *calldata)
5600{
Peng Tao039b7562014-07-03 13:05:02 +08005601 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005602 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005603
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005604 if (inode) {
5605 if (data->roc)
5606 pnfs_roc_release(inode);
5607 nfs_iput_and_deactive(inode);
5608 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005609 kfree(calldata);
5610}
5611
Andy Adamson938e1012009-04-01 09:22:28 -04005612static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5613{
5614 struct nfs4_delegreturndata *d_data;
5615
5616 d_data = (struct nfs4_delegreturndata *)data;
5617
Peng Tao500d7012015-09-22 11:35:22 +08005618 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5619 return;
5620
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005621 if (d_data->roc)
5622 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005623
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005624 nfs4_setup_sequence(d_data->res.server,
5625 &d_data->args.seq_args,
5626 &d_data->res.seq_res,
5627 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005628}
Andy Adamson938e1012009-04-01 09:22:28 -04005629
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005630static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005631 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005632 .rpc_call_done = nfs4_delegreturn_done,
5633 .rpc_release = nfs4_delegreturn_release,
5634};
5635
Trond Myklebuste6f81072008-01-24 18:14:34 -05005636static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005637{
5638 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005639 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005640 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005641 struct rpc_message msg = {
5642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5643 .rpc_cred = cred,
5644 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005645 struct rpc_task_setup task_setup_data = {
5646 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005647 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005648 .callback_ops = &nfs4_delegreturn_ops,
5649 .flags = RPC_TASK_ASYNC,
5650 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005651 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005652
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005653 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005654 if (data == NULL)
5655 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005656 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005657
5658 nfs4_state_protect(server->nfs_client,
5659 NFS_SP4_MACH_CRED_CLEANUP,
5660 &task_setup_data.rpc_client, &msg);
5661
Trond Myklebustfe650402006-01-03 09:55:18 +01005662 data->args.fhandle = &data->fh;
5663 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005664 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005665 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005666 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005667 data->res.fattr = &data->fattr;
5668 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005669 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005670 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005671 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005672 data->inode = nfs_igrab_and_active(inode);
5673 if (data->inode)
5674 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005675
Trond Myklebustc970aa82007-07-14 15:39:59 -04005676 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005677 msg.rpc_argp = &data->args;
5678 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005679 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005680 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005681 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005682 if (!issync)
5683 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005684 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005685 if (status != 0)
5686 goto out;
5687 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005688 if (status == 0)
5689 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5690 else
5691 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005692out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005693 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005694 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695}
5696
Trond Myklebuste6f81072008-01-24 18:14:34 -05005697int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698{
5699 struct nfs_server *server = NFS_SERVER(inode);
5700 struct nfs4_exception exception = { };
5701 int err;
5702 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005703 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005704 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 switch (err) {
5706 case -NFS4ERR_STALE_STATEID:
5707 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 case 0:
5709 return 0;
5710 }
5711 err = nfs4_handle_exception(server, err, &exception);
5712 } while (exception.retry);
5713 return err;
5714}
5715
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5717{
5718 struct inode *inode = state->inode;
5719 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005720 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005721 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005723 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005725 struct nfs_lockt_res res = {
5726 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 };
5728 struct rpc_message msg = {
5729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5730 .rpc_argp = &arg,
5731 .rpc_resp = &res,
5732 .rpc_cred = state->owner->so_cred,
5733 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 struct nfs4_lock_state *lsp;
5735 int status;
5736
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005737 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005738 status = nfs4_set_lock_state(state, request);
5739 if (status != 0)
5740 goto out;
5741 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005742 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005743 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005744 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005745 switch (status) {
5746 case 0:
5747 request->fl_type = F_UNLCK;
5748 break;
5749 case -NFS4ERR_DENIED:
5750 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005752 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005753 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005754out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 return status;
5756}
5757
5758static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5759{
5760 struct nfs4_exception exception = { };
5761 int err;
5762
5763 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005764 err = _nfs4_proc_getlk(state, cmd, request);
5765 trace_nfs4_get_lock(request, state, cmd, err);
5766 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 &exception);
5768 } while (exception.retry);
5769 return err;
5770}
5771
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005772struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005773 struct nfs_locku_args arg;
5774 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005775 struct nfs4_lock_state *lsp;
5776 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005777 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005778 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005779 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005780};
5781
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005782static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5783 struct nfs_open_context *ctx,
5784 struct nfs4_lock_state *lsp,
5785 struct nfs_seqid *seqid)
5786{
5787 struct nfs4_unlockdata *p;
5788 struct inode *inode = lsp->ls_state->inode;
5789
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005790 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005791 if (p == NULL)
5792 return NULL;
5793 p->arg.fh = NFS_FH(inode);
5794 p->arg.fl = &p->fl;
5795 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005796 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005797 p->lsp = lsp;
5798 atomic_inc(&lsp->ls_count);
5799 /* Ensure we don't close file until we're done freeing locks! */
5800 p->ctx = get_nfs_open_context(ctx);
5801 memcpy(&p->fl, fl, sizeof(p->fl));
5802 p->server = NFS_SERVER(inode);
5803 return p;
5804}
5805
Trond Myklebust06f814a2006-01-03 09:55:07 +01005806static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005807{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005808 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005809 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005810 nfs4_put_lock_state(calldata->lsp);
5811 put_nfs_open_context(calldata->ctx);
5812 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005813}
5814
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005815static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005816{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005817 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005818
Trond Myklebust14516c32010-07-31 14:29:06 -04005819 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5820 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005821 switch (task->tk_status) {
5822 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005823 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005824 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005825 if (nfs4_update_lock_stateid(calldata->lsp,
5826 &calldata->res.stateid))
5827 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005828 case -NFS4ERR_ADMIN_REVOKED:
5829 case -NFS4ERR_EXPIRED:
5830 nfs4_free_revoked_stateid(calldata->server,
5831 &calldata->arg.stateid,
5832 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005833 case -NFS4ERR_BAD_STATEID:
5834 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005835 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005836 if (!nfs4_stateid_match(&calldata->arg.stateid,
5837 &calldata->lsp->ls_stateid))
5838 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005839 break;
5840 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005841 if (nfs4_async_handle_error(task, calldata->server,
5842 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005843 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005844 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005845 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005846}
5847
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005848static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005849{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005850 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005852 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005853 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005854 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005855 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005856 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005857 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005858 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005859 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005860 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005861 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005862 &calldata->res.seq_res,
5863 task) != 0)
5864 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005865 return;
5866out_no_action:
5867 task->tk_action = NULL;
5868out_wait:
5869 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870}
5871
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005872static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005873 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005874 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005875 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005876};
5877
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005878static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5879 struct nfs_open_context *ctx,
5880 struct nfs4_lock_state *lsp,
5881 struct nfs_seqid *seqid)
5882{
5883 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005884 struct rpc_message msg = {
5885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5886 .rpc_cred = ctx->cred,
5887 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005888 struct rpc_task_setup task_setup_data = {
5889 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005890 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005891 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005892 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005893 .flags = RPC_TASK_ASYNC,
5894 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005895
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005896 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5897 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5898
Frank Filz137d6ac2007-07-09 15:32:29 -07005899 /* Ensure this is an unlock - when canceling a lock, the
5900 * canceled lock is passed in, and it won't be an unlock.
5901 */
5902 fl->fl_type = F_UNLCK;
5903
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005904 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5905 if (data == NULL) {
5906 nfs_free_seqid(seqid);
5907 return ERR_PTR(-ENOMEM);
5908 }
5909
Chuck Levera9c92d62013-08-09 12:48:18 -04005910 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005911 msg.rpc_argp = &data->arg;
5912 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005913 task_setup_data.callback_data = data;
5914 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005915}
5916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5918{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005919 struct inode *inode = state->inode;
5920 struct nfs4_state_owner *sp = state->owner;
5921 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005922 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005923 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005924 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005925 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005926 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005927 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
Trond Myklebust9b073572006-06-29 16:38:34 -04005929 status = nfs4_set_lock_state(state, request);
5930 /* Unlock _before_ we do the RPC call */
5931 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005932 /* Exclude nfs_delegation_claim_locks() */
5933 mutex_lock(&sp->so_delegreturn_mutex);
5934 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005935 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005936 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005937 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005938 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005939 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005940 }
5941 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005942 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005943 if (status != 0)
5944 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005945 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005946 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005947 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5948 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005949 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5950 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005951 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005952 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005953 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005954 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005955 status = PTR_ERR(task);
5956 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005957 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005958 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005959 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005960out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005961 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005962 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005963 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964}
5965
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005966struct nfs4_lockdata {
5967 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005968 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005969 struct nfs4_lock_state *lsp;
5970 struct nfs_open_context *ctx;
5971 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005972 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005973 int rpc_status;
5974 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005975 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976};
5977
5978static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005979 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5980 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005981{
5982 struct nfs4_lockdata *p;
5983 struct inode *inode = lsp->ls_state->inode;
5984 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005985 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005986
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005987 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005988 if (p == NULL)
5989 return NULL;
5990
5991 p->arg.fh = NFS_FH(inode);
5992 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005993 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005994 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005995 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005996 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5997 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005998 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005999 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006000 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006001 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006002 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006003 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006005 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006006 atomic_inc(&lsp->ls_count);
6007 p->ctx = get_nfs_open_context(ctx);
6008 memcpy(&p->fl, fl, sizeof(p->fl));
6009 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006010out_free_seqid:
6011 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006012out_free:
6013 kfree(p);
6014 return NULL;
6015}
6016
6017static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6018{
6019 struct nfs4_lockdata *data = calldata;
6020 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Harvey Harrison3110ff82008-05-02 13:42:44 -07006022 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006023 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006024 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006026 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006027 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006028 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006029 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006030 nfs4_stateid_copy(&data->arg.open_stateid,
6031 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006032 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006033 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006034 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006035 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006036 nfs4_stateid_copy(&data->arg.lock_stateid,
6037 &data->lsp->ls_stateid);
6038 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006039 if (!nfs4_valid_open_stateid(state)) {
6040 data->rpc_status = -EBADF;
6041 task->tk_action = NULL;
6042 goto out_release_open_seqid;
6043 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006044 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006045 if (nfs4_setup_sequence(data->server,
6046 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006047 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006048 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006049 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006050out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006051 nfs_release_seqid(data->arg.open_seqid);
6052out_release_lock_seqid:
6053 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006054out_wait:
6055 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006056 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006057}
6058
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006059static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6060{
6061 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006062 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006063
Harvey Harrison3110ff82008-05-02 13:42:44 -07006064 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006065
Trond Myklebust14516c32010-07-31 14:29:06 -04006066 if (!nfs4_sequence_done(task, &data->res.seq_res))
6067 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006068
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006069 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006070 switch (task->tk_status) {
6071 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006072 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006073 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006074 if (data->arg.new_lock) {
6075 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006076 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006077 rpc_restart_call_prepare(task);
6078 break;
6079 }
6080 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006081 if (data->arg.new_lock_owner != 0) {
6082 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6083 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6084 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6085 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6086 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006087 break;
6088 case -NFS4ERR_BAD_STATEID:
6089 case -NFS4ERR_OLD_STATEID:
6090 case -NFS4ERR_STALE_STATEID:
6091 case -NFS4ERR_EXPIRED:
6092 if (data->arg.new_lock_owner != 0) {
6093 if (!nfs4_stateid_match(&data->arg.open_stateid,
6094 &lsp->ls_state->open_stateid))
6095 rpc_restart_call_prepare(task);
6096 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6097 &lsp->ls_stateid))
6098 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006100 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101}
6102
6103static void nfs4_lock_release(void *calldata)
6104{
6105 struct nfs4_lockdata *data = calldata;
6106
Harvey Harrison3110ff82008-05-02 13:42:44 -07006107 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006108 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006109 if (data->cancelled != 0) {
6110 struct rpc_task *task;
6111 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6112 data->arg.lock_seqid);
6113 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006114 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006115 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006116 } else
6117 nfs_free_seqid(data->arg.lock_seqid);
6118 nfs4_put_lock_state(data->lsp);
6119 put_nfs_open_context(data->ctx);
6120 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006121 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006122}
6123
6124static const struct rpc_call_ops nfs4_lock_ops = {
6125 .rpc_call_prepare = nfs4_lock_prepare,
6126 .rpc_call_done = nfs4_lock_done,
6127 .rpc_release = nfs4_lock_release,
6128};
6129
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006130static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6131{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006132 switch (error) {
6133 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006134 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006135 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006136 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006137 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006138 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006139 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006140 break;
6141 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006142 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006143 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006144 };
6145}
6146
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006147static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006148{
6149 struct nfs4_lockdata *data;
6150 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006151 struct rpc_message msg = {
6152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6153 .rpc_cred = state->owner->so_cred,
6154 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006155 struct rpc_task_setup task_setup_data = {
6156 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006157 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006158 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006159 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006160 .flags = RPC_TASK_ASYNC,
6161 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006162 int ret;
6163
Harvey Harrison3110ff82008-05-02 13:42:44 -07006164 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006165 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006166 fl->fl_u.nfs4_fl.owner,
6167 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006168 if (data == NULL)
6169 return -ENOMEM;
6170 if (IS_SETLKW(cmd))
6171 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006172 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006173 msg.rpc_argp = &data->arg;
6174 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006175 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006176 if (recovery_type > NFS_LOCK_NEW) {
6177 if (recovery_type == NFS_LOCK_RECLAIM)
6178 data->arg.reclaim = NFS_LOCK_RECLAIM;
6179 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006180 } else
6181 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006182 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006183 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006184 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185 ret = nfs4_wait_for_completion_rpc_task(task);
6186 if (ret == 0) {
6187 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006188 if (ret)
6189 nfs4_handle_setlk_error(data->server, data->lsp,
6190 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006191 } else
6192 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006193 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006194 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006195 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006196 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197}
6198
6199static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6200{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006201 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006202 struct nfs4_exception exception = {
6203 .inode = state->inode,
6204 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006205 int err;
6206
6207 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006208 /* Cache the lock if possible... */
6209 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6210 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006211 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006212 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006213 break;
6214 nfs4_handle_exception(server, err, &exception);
6215 } while (exception.retry);
6216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217}
6218
6219static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6220{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006221 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006222 struct nfs4_exception exception = {
6223 .inode = state->inode,
6224 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006225 int err;
6226
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006227 err = nfs4_set_lock_state(state, request);
6228 if (err != 0)
6229 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006230 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006231 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6232 return 0;
6233 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006234 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006235 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6236 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006237 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006238 switch (err) {
6239 default:
6240 goto out;
6241 case -NFS4ERR_GRACE:
6242 case -NFS4ERR_DELAY:
6243 nfs4_handle_exception(server, err, &exception);
6244 err = 0;
6245 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006246 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006247out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249}
6250
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006251#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006252static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6253{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006254 struct nfs4_lock_state *lsp;
6255 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006256
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006257 status = nfs4_set_lock_state(state, request);
6258 if (status != 0)
6259 return status;
6260 lsp = request->fl_u.nfs4_fl.owner;
6261 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6262 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6263 return 0;
6264 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006265 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006266}
6267#endif
6268
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6270{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006271 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006272 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006273 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006274 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
Trond Myklebust01c3b862006-06-29 16:38:39 -04006276 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006277 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006278 if (status < 0)
6279 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006280 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006281 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006282 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006283 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006284 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006285 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006286 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006287 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006288 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006289 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006290 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006291 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006292 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006293 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006294out:
6295 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return status;
6297}
6298
6299static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6300{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006301 struct nfs4_exception exception = {
6302 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006303 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006304 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 int err;
6306
6307 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006308 err = _nfs4_proc_setlk(state, cmd, request);
6309 if (err == -NFS4ERR_DENIED)
6310 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006312 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 } while (exception.retry);
6314 return err;
6315}
6316
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006317#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6318#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6319
6320static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006321nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6322 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006323{
6324 int status = -ERESTARTSYS;
6325 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6326
6327 while(!signalled()) {
6328 status = nfs4_proc_setlk(state, cmd, request);
6329 if ((status != -EAGAIN) || IS_SETLK(cmd))
6330 break;
6331 freezable_schedule_timeout_interruptible(timeout);
6332 timeout *= 2;
6333 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6334 status = -ERESTARTSYS;
6335 }
6336 return status;
6337}
6338
Jeff Laytona1d617d82016-09-17 18:17:39 -04006339#ifdef CONFIG_NFS_V4_1
6340struct nfs4_lock_waiter {
6341 struct task_struct *task;
6342 struct inode *inode;
6343 struct nfs_lowner *owner;
6344 bool notified;
6345};
6346
6347static int
6348nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6349{
6350 int ret;
6351 struct cb_notify_lock_args *cbnl = key;
6352 struct nfs4_lock_waiter *waiter = wait->private;
6353 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6354 *wowner = waiter->owner;
6355
6356 /* Only wake if the callback was for the same owner */
6357 if (lowner->clientid != wowner->clientid ||
6358 lowner->id != wowner->id ||
6359 lowner->s_dev != wowner->s_dev)
6360 return 0;
6361
6362 /* Make sure it's for the right inode */
6363 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6364 return 0;
6365
6366 waiter->notified = true;
6367
6368 /* override "private" so we can use default_wake_function */
6369 wait->private = waiter->task;
6370 ret = autoremove_wake_function(wait, mode, flags, key);
6371 wait->private = waiter;
6372 return ret;
6373}
6374
6375static int
6376nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6377{
6378 int status = -ERESTARTSYS;
6379 unsigned long flags;
6380 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6381 struct nfs_server *server = NFS_SERVER(state->inode);
6382 struct nfs_client *clp = server->nfs_client;
6383 wait_queue_head_t *q = &clp->cl_lock_waitq;
6384 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6385 .id = lsp->ls_seqid.owner_id,
6386 .s_dev = server->s_dev };
6387 struct nfs4_lock_waiter waiter = { .task = current,
6388 .inode = state->inode,
6389 .owner = &owner,
6390 .notified = false };
6391 wait_queue_t wait;
6392
6393 /* Don't bother with waitqueue if we don't expect a callback */
6394 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6395 return nfs4_retry_setlk_simple(state, cmd, request);
6396
6397 init_wait(&wait);
6398 wait.private = &waiter;
6399 wait.func = nfs4_wake_lock_waiter;
6400 add_wait_queue(q, &wait);
6401
6402 while(!signalled()) {
6403 status = nfs4_proc_setlk(state, cmd, request);
6404 if ((status != -EAGAIN) || IS_SETLK(cmd))
6405 break;
6406
6407 status = -ERESTARTSYS;
6408 spin_lock_irqsave(&q->lock, flags);
6409 if (waiter.notified) {
6410 spin_unlock_irqrestore(&q->lock, flags);
6411 continue;
6412 }
6413 set_current_state(TASK_INTERRUPTIBLE);
6414 spin_unlock_irqrestore(&q->lock, flags);
6415
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006416 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006417 }
6418
6419 finish_wait(q, &wait);
6420 return status;
6421}
6422#else /* !CONFIG_NFS_V4_1 */
6423static inline int
6424nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6425{
6426 return nfs4_retry_setlk_simple(state, cmd, request);
6427}
6428#endif
6429
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430static int
6431nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6432{
6433 struct nfs_open_context *ctx;
6434 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 int status;
6436
6437 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006438 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 state = ctx->state;
6440
6441 if (request->fl_start < 0 || request->fl_end < 0)
6442 return -EINVAL;
6443
Trond Myklebustd9531262009-07-21 19:22:38 -04006444 if (IS_GETLK(cmd)) {
6445 if (state != NULL)
6446 return nfs4_proc_getlk(state, F_GETLK, request);
6447 return 0;
6448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
6450 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6451 return -EINVAL;
6452
Trond Myklebustd9531262009-07-21 19:22:38 -04006453 if (request->fl_type == F_UNLCK) {
6454 if (state != NULL)
6455 return nfs4_proc_unlck(state, cmd, request);
6456 return 0;
6457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458
Trond Myklebustd9531262009-07-21 19:22:38 -04006459 if (state == NULL)
6460 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006461
6462 if ((request->fl_flags & FL_POSIX) &&
6463 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6464 return -ENOLCK;
6465
Trond Myklebust55725512012-04-18 12:48:35 -04006466 /*
6467 * Don't rely on the VFS having checked the file open mode,
6468 * since it won't do this for flock() locks.
6469 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006470 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006471 case F_RDLCK:
6472 if (!(filp->f_mode & FMODE_READ))
6473 return -EBADF;
6474 break;
6475 case F_WRLCK:
6476 if (!(filp->f_mode & FMODE_WRITE))
6477 return -EBADF;
6478 }
6479
Jeff Layton1ea67db2016-09-17 18:17:37 -04006480 status = nfs4_set_lock_state(state, request);
6481 if (status != 0)
6482 return status;
6483
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006484 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485}
6486
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006487int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006488{
6489 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006490 int err;
6491
6492 err = nfs4_set_lock_state(state, fl);
6493 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006494 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006495 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006496 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006497}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006498
Trond Myklebustcf470c32012-03-07 13:49:12 -05006499struct nfs_release_lockowner_data {
6500 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006501 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006502 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006503 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006504 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006505};
6506
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006507static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6508{
6509 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006510 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006511 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6512 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006513 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006514 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006515}
6516
6517static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6518{
6519 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006520 struct nfs_server *server = data->server;
6521
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006522 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006523
6524 switch (task->tk_status) {
6525 case 0:
6526 renew_lease(server, data->timestamp);
6527 break;
6528 case -NFS4ERR_STALE_CLIENTID:
6529 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006530 nfs4_schedule_lease_recovery(server->nfs_client);
6531 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006532 case -NFS4ERR_LEASE_MOVED:
6533 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006534 if (nfs4_async_handle_error(task, server,
6535 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006536 rpc_restart_call_prepare(task);
6537 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006538}
6539
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006540static void nfs4_release_lockowner_release(void *calldata)
6541{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006542 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006543 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006544 kfree(calldata);
6545}
6546
Trond Myklebust17280172012-03-11 13:11:00 -04006547static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006548 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6549 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006550 .rpc_release = nfs4_release_lockowner_release,
6551};
6552
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006553static void
6554nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006555{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006556 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006557 struct rpc_message msg = {
6558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6559 };
6560
6561 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006562 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006563
Trond Myklebustcf470c32012-03-07 13:49:12 -05006564 data = kmalloc(sizeof(*data), GFP_NOFS);
6565 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006566 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006567 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006568 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006569 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6570 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6571 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006572
Trond Myklebustcf470c32012-03-07 13:49:12 -05006573 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006574 msg.rpc_resp = &data->res;
6575 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006576 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006577}
6578
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006579#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6580
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006581static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006582 struct dentry *unused, struct inode *inode,
6583 const char *key, const void *buf,
6584 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006585{
Al Viro59301222016-05-27 10:19:30 -04006586 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006587}
6588
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006589static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006590 struct dentry *unused, struct inode *inode,
6591 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006592{
Al Virob2968212016-04-10 20:48:24 -04006593 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006594}
6595
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006596static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006597{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006598 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006599}
6600
David Quigleyc9bccef2013-05-22 12:50:45 -04006601#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006602
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006603static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006604 struct dentry *unused, struct inode *inode,
6605 const char *key, const void *buf,
6606 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006607{
6608 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006609 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006610
6611 return -EOPNOTSUPP;
6612}
6613
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006614static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006615 struct dentry *unused, struct inode *inode,
6616 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006617{
6618 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006619 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006620 return -EOPNOTSUPP;
6621}
6622
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006623static ssize_t
6624nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006625{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006626 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006627
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006628 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6629 len = security_inode_listsecurity(inode, list, list_len);
6630 if (list_len && len > list_len)
6631 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006632 }
6633 return len;
6634}
6635
6636static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6637 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006638 .get = nfs4_xattr_get_nfs4_label,
6639 .set = nfs4_xattr_set_nfs4_label,
6640};
David Quigleyc9bccef2013-05-22 12:50:45 -04006641
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006642#else
6643
6644static ssize_t
6645nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6646{
6647 return 0;
6648}
6649
6650#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006651
Andy Adamson533eb462011-06-13 18:25:56 -04006652/*
6653 * nfs_fhget will use either the mounted_on_fileid or the fileid
6654 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006655static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6656{
Andy Adamson533eb462011-06-13 18:25:56 -04006657 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6658 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6659 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006660 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006661 return;
6662
6663 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006664 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006665 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6666 fattr->nlink = 2;
6667}
6668
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006669static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6670 const struct qstr *name,
6671 struct nfs4_fs_locations *fs_locations,
6672 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006673{
6674 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006675 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006676 struct nfs4_fs_locations_arg args = {
6677 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006678 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006679 .page = page,
6680 .bitmask = bitmask,
6681 };
Benny Halevy22958462009-04-01 09:22:02 -04006682 struct nfs4_fs_locations_res res = {
6683 .fs_locations = fs_locations,
6684 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006685 struct rpc_message msg = {
6686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6687 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006688 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006689 };
6690 int status;
6691
Harvey Harrison3110ff82008-05-02 13:42:44 -07006692 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006693
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006694 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6695 bitmask[1] = nfs4_fattr_bitmap[1];
6696
Andy Adamson533eb462011-06-13 18:25:56 -04006697 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6698 * is not supported */
6699 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006700 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006701 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006702 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006703
Trond Myklebustc228fd32007-01-13 02:28:11 -05006704 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006705 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006706 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006707 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006708 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006709 return status;
6710}
6711
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006712int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6713 const struct qstr *name,
6714 struct nfs4_fs_locations *fs_locations,
6715 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006716{
6717 struct nfs4_exception exception = { };
6718 int err;
6719 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006720 err = _nfs4_proc_fs_locations(client, dir, name,
6721 fs_locations, page);
6722 trace_nfs4_get_fs_locations(dir, name, err);
6723 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006724 &exception);
6725 } while (exception.retry);
6726 return err;
6727}
6728
Chuck Leverb03d7352013-10-17 14:12:50 -04006729/*
6730 * This operation also signals the server that this client is
6731 * performing migration recovery. The server can stop returning
6732 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6733 * appended to this compound to identify the client ID which is
6734 * performing recovery.
6735 */
6736static int _nfs40_proc_get_locations(struct inode *inode,
6737 struct nfs4_fs_locations *locations,
6738 struct page *page, struct rpc_cred *cred)
6739{
6740 struct nfs_server *server = NFS_SERVER(inode);
6741 struct rpc_clnt *clnt = server->client;
6742 u32 bitmask[2] = {
6743 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6744 };
6745 struct nfs4_fs_locations_arg args = {
6746 .clientid = server->nfs_client->cl_clientid,
6747 .fh = NFS_FH(inode),
6748 .page = page,
6749 .bitmask = bitmask,
6750 .migration = 1, /* skip LOOKUP */
6751 .renew = 1, /* append RENEW */
6752 };
6753 struct nfs4_fs_locations_res res = {
6754 .fs_locations = locations,
6755 .migration = 1,
6756 .renew = 1,
6757 };
6758 struct rpc_message msg = {
6759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6760 .rpc_argp = &args,
6761 .rpc_resp = &res,
6762 .rpc_cred = cred,
6763 };
6764 unsigned long now = jiffies;
6765 int status;
6766
6767 nfs_fattr_init(&locations->fattr);
6768 locations->server = server;
6769 locations->nlocations = 0;
6770
6771 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6772 nfs4_set_sequence_privileged(&args.seq_args);
6773 status = nfs4_call_sync_sequence(clnt, server, &msg,
6774 &args.seq_args, &res.seq_res);
6775 if (status)
6776 return status;
6777
6778 renew_lease(server, now);
6779 return 0;
6780}
6781
6782#ifdef CONFIG_NFS_V4_1
6783
6784/*
6785 * This operation also signals the server that this client is
6786 * performing migration recovery. The server can stop asserting
6787 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6788 * performing this operation is identified in the SEQUENCE
6789 * operation in this compound.
6790 *
6791 * When the client supports GETATTR(fs_locations_info), it can
6792 * be plumbed in here.
6793 */
6794static int _nfs41_proc_get_locations(struct inode *inode,
6795 struct nfs4_fs_locations *locations,
6796 struct page *page, struct rpc_cred *cred)
6797{
6798 struct nfs_server *server = NFS_SERVER(inode);
6799 struct rpc_clnt *clnt = server->client;
6800 u32 bitmask[2] = {
6801 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6802 };
6803 struct nfs4_fs_locations_arg args = {
6804 .fh = NFS_FH(inode),
6805 .page = page,
6806 .bitmask = bitmask,
6807 .migration = 1, /* skip LOOKUP */
6808 };
6809 struct nfs4_fs_locations_res res = {
6810 .fs_locations = locations,
6811 .migration = 1,
6812 };
6813 struct rpc_message msg = {
6814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6815 .rpc_argp = &args,
6816 .rpc_resp = &res,
6817 .rpc_cred = cred,
6818 };
6819 int status;
6820
6821 nfs_fattr_init(&locations->fattr);
6822 locations->server = server;
6823 locations->nlocations = 0;
6824
6825 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6826 nfs4_set_sequence_privileged(&args.seq_args);
6827 status = nfs4_call_sync_sequence(clnt, server, &msg,
6828 &args.seq_args, &res.seq_res);
6829 if (status == NFS4_OK &&
6830 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6831 status = -NFS4ERR_LEASE_MOVED;
6832 return status;
6833}
6834
6835#endif /* CONFIG_NFS_V4_1 */
6836
6837/**
6838 * nfs4_proc_get_locations - discover locations for a migrated FSID
6839 * @inode: inode on FSID that is migrating
6840 * @locations: result of query
6841 * @page: buffer
6842 * @cred: credential to use for this operation
6843 *
6844 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6845 * operation failed, or a negative errno if a local error occurred.
6846 *
6847 * On success, "locations" is filled in, but if the server has
6848 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6849 * asserted.
6850 *
6851 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6852 * from this client that require migration recovery.
6853 */
6854int nfs4_proc_get_locations(struct inode *inode,
6855 struct nfs4_fs_locations *locations,
6856 struct page *page, struct rpc_cred *cred)
6857{
6858 struct nfs_server *server = NFS_SERVER(inode);
6859 struct nfs_client *clp = server->nfs_client;
6860 const struct nfs4_mig_recovery_ops *ops =
6861 clp->cl_mvops->mig_recovery_ops;
6862 struct nfs4_exception exception = { };
6863 int status;
6864
6865 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6866 (unsigned long long)server->fsid.major,
6867 (unsigned long long)server->fsid.minor,
6868 clp->cl_hostname);
6869 nfs_display_fhandle(NFS_FH(inode), __func__);
6870
6871 do {
6872 status = ops->get_locations(inode, locations, page, cred);
6873 if (status != -NFS4ERR_DELAY)
6874 break;
6875 nfs4_handle_exception(server, status, &exception);
6876 } while (exception.retry);
6877 return status;
6878}
6879
Chuck Lever44c99932013-10-17 14:13:30 -04006880/*
6881 * This operation also signals the server that this client is
6882 * performing "lease moved" recovery. The server can stop
6883 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6884 * is appended to this compound to identify the client ID which is
6885 * performing recovery.
6886 */
6887static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6888{
6889 struct nfs_server *server = NFS_SERVER(inode);
6890 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6891 struct rpc_clnt *clnt = server->client;
6892 struct nfs4_fsid_present_arg args = {
6893 .fh = NFS_FH(inode),
6894 .clientid = clp->cl_clientid,
6895 .renew = 1, /* append RENEW */
6896 };
6897 struct nfs4_fsid_present_res res = {
6898 .renew = 1,
6899 };
6900 struct rpc_message msg = {
6901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6902 .rpc_argp = &args,
6903 .rpc_resp = &res,
6904 .rpc_cred = cred,
6905 };
6906 unsigned long now = jiffies;
6907 int status;
6908
6909 res.fh = nfs_alloc_fhandle();
6910 if (res.fh == NULL)
6911 return -ENOMEM;
6912
6913 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6914 nfs4_set_sequence_privileged(&args.seq_args);
6915 status = nfs4_call_sync_sequence(clnt, server, &msg,
6916 &args.seq_args, &res.seq_res);
6917 nfs_free_fhandle(res.fh);
6918 if (status)
6919 return status;
6920
6921 do_renew_lease(clp, now);
6922 return 0;
6923}
6924
6925#ifdef CONFIG_NFS_V4_1
6926
6927/*
6928 * This operation also signals the server that this client is
6929 * performing "lease moved" recovery. The server can stop asserting
6930 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6931 * this operation is identified in the SEQUENCE operation in this
6932 * compound.
6933 */
6934static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6935{
6936 struct nfs_server *server = NFS_SERVER(inode);
6937 struct rpc_clnt *clnt = server->client;
6938 struct nfs4_fsid_present_arg args = {
6939 .fh = NFS_FH(inode),
6940 };
6941 struct nfs4_fsid_present_res res = {
6942 };
6943 struct rpc_message msg = {
6944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6945 .rpc_argp = &args,
6946 .rpc_resp = &res,
6947 .rpc_cred = cred,
6948 };
6949 int status;
6950
6951 res.fh = nfs_alloc_fhandle();
6952 if (res.fh == NULL)
6953 return -ENOMEM;
6954
6955 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6956 nfs4_set_sequence_privileged(&args.seq_args);
6957 status = nfs4_call_sync_sequence(clnt, server, &msg,
6958 &args.seq_args, &res.seq_res);
6959 nfs_free_fhandle(res.fh);
6960 if (status == NFS4_OK &&
6961 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6962 status = -NFS4ERR_LEASE_MOVED;
6963 return status;
6964}
6965
6966#endif /* CONFIG_NFS_V4_1 */
6967
6968/**
6969 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6970 * @inode: inode on FSID to check
6971 * @cred: credential to use for this operation
6972 *
6973 * Server indicates whether the FSID is present, moved, or not
6974 * recognized. This operation is necessary to clear a LEASE_MOVED
6975 * condition for this client ID.
6976 *
6977 * Returns NFS4_OK if the FSID is present on this server,
6978 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6979 * NFS4ERR code if some error occurred on the server, or a
6980 * negative errno if a local failure occurred.
6981 */
6982int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6983{
6984 struct nfs_server *server = NFS_SERVER(inode);
6985 struct nfs_client *clp = server->nfs_client;
6986 const struct nfs4_mig_recovery_ops *ops =
6987 clp->cl_mvops->mig_recovery_ops;
6988 struct nfs4_exception exception = { };
6989 int status;
6990
6991 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6992 (unsigned long long)server->fsid.major,
6993 (unsigned long long)server->fsid.minor,
6994 clp->cl_hostname);
6995 nfs_display_fhandle(NFS_FH(inode), __func__);
6996
6997 do {
6998 status = ops->fsid_present(inode, cred);
6999 if (status != -NFS4ERR_DELAY)
7000 break;
7001 nfs4_handle_exception(server, status, &exception);
7002 } while (exception.retry);
7003 return status;
7004}
7005
Andy Adamson5ec16a82013-08-08 10:57:55 -04007006/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007007 * If 'use_integrity' is true and the state managment nfs_client
7008 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7009 * and the machine credential as per RFC3530bis and RFC5661 Security
7010 * Considerations sections. Otherwise, just use the user cred with the
7011 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007012 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007013static 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 +00007014{
7015 int status;
7016 struct nfs4_secinfo_arg args = {
7017 .dir_fh = NFS_FH(dir),
7018 .name = name,
7019 };
7020 struct nfs4_secinfo_res res = {
7021 .flavors = flavors,
7022 };
7023 struct rpc_message msg = {
7024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7025 .rpc_argp = &args,
7026 .rpc_resp = &res,
7027 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007028 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007029 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007030
7031 if (use_integrity) {
7032 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007033 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7034 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007035 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007036
7037 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007038
7039 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7040 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7041
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007042 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7043 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007044 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007045
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007046 if (cred)
7047 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007048
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007049 return status;
7050}
7051
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007052int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7053 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007054{
7055 struct nfs4_exception exception = { };
7056 int err;
7057 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007058 err = -NFS4ERR_WRONGSEC;
7059
7060 /* try to use integrity protection with machine cred */
7061 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7062 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7063
7064 /*
7065 * if unable to use integrity protection, or SECINFO with
7066 * integrity protection returns NFS4ERR_WRONGSEC (which is
7067 * disallowed by spec, but exists in deployed servers) use
7068 * the current filesystem's rpc_client and the user cred.
7069 */
7070 if (err == -NFS4ERR_WRONGSEC)
7071 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7072
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007073 trace_nfs4_secinfo(dir, name, err);
7074 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007075 &exception);
7076 } while (exception.retry);
7077 return err;
7078}
7079
Andy Adamson557134a2009-04-01 09:21:53 -04007080#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007081/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007082 * Check the exchange flags returned by the server for invalid flags, having
7083 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7084 * DS flags set.
7085 */
7086static int nfs4_check_cl_exchange_flags(u32 flags)
7087{
7088 if (flags & ~EXCHGID4_FLAG_MASK_R)
7089 goto out_inval;
7090 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7091 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7092 goto out_inval;
7093 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7094 goto out_inval;
7095 return NFS_OK;
7096out_inval:
7097 return -NFS4ERR_INVAL;
7098}
7099
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007100static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007101nfs41_same_server_scope(struct nfs41_server_scope *a,
7102 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007103{
7104 if (a->server_scope_sz == b->server_scope_sz &&
7105 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7106 return true;
7107
7108 return false;
7109}
7110
Andy Adamson02a95de2016-02-05 16:08:37 -05007111static void
7112nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7113{
7114}
7115
7116static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7117 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7118};
7119
Andy Adamson357f54d2010-12-14 10:11:57 -05007120/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007121 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007122 *
7123 * The 4.1 client currently uses the same TCP connection for the
7124 * fore and backchannel.
7125 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007126static
7127int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7128 struct rpc_xprt *xprt,
7129 struct nfs_client *clp,
7130 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007131{
7132 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007133 struct nfs41_bind_conn_to_session_args args = {
7134 .client = clp,
7135 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7136 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007137 struct nfs41_bind_conn_to_session_res res;
7138 struct rpc_message msg = {
7139 .rpc_proc =
7140 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007141 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007142 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007143 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007144 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007145 struct rpc_task_setup task_setup_data = {
7146 .rpc_client = clnt,
7147 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007148 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007149 .rpc_message = &msg,
7150 .flags = RPC_TASK_TIMEOUT,
7151 };
7152 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007153
7154 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007155
Trond Myklebust71a097c2015-02-18 09:27:18 -08007156 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7157 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7158 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007159
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007160 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7161 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7162 args.dir = NFS4_CDFC4_FORE;
7163
7164 task = rpc_run_task(&task_setup_data);
7165 if (!IS_ERR(task)) {
7166 status = task->tk_status;
7167 rpc_put_task(task);
7168 } else
7169 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007170 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007171 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007172 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007173 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7174 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7175 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007176 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007178 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007179 dprintk("NFS: %s: Unexpected direction from server\n",
7180 __func__);
7181 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007182 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007183 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007184 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007185 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7186 __func__);
7187 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007188 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007189 }
7190 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007191out:
7192 dprintk("<-- %s status= %d\n", __func__, status);
7193 return status;
7194}
7195
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007196struct rpc_bind_conn_calldata {
7197 struct nfs_client *clp;
7198 struct rpc_cred *cred;
7199};
7200
7201static int
7202nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7203 struct rpc_xprt *xprt,
7204 void *calldata)
7205{
7206 struct rpc_bind_conn_calldata *p = calldata;
7207
7208 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7209}
7210
7211int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7212{
7213 struct rpc_bind_conn_calldata data = {
7214 .clp = clp,
7215 .cred = cred,
7216 };
7217 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7218 nfs4_proc_bind_conn_to_session_callback, &data);
7219}
7220
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007221/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007222 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7223 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007224 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007225static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7226 .how = SP4_MACH_CRED,
7227 .enforce.u.words = {
7228 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7229 1 << (OP_EXCHANGE_ID - 32) |
7230 1 << (OP_CREATE_SESSION - 32) |
7231 1 << (OP_DESTROY_SESSION - 32) |
7232 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007233 },
7234 .allow.u.words = {
7235 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007236 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007237 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007238 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007239 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007240 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007241 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007242 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007243 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007244 1 << (OP_FREE_STATEID - 32) |
7245 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007246 }
7247};
7248
7249/*
7250 * Select the state protection mode for client `clp' given the server results
7251 * from exchange_id in `sp'.
7252 *
7253 * Returns 0 on success, negative errno otherwise.
7254 */
7255static int nfs4_sp4_select_mode(struct nfs_client *clp,
7256 struct nfs41_state_protection *sp)
7257{
7258 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7259 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7260 1 << (OP_EXCHANGE_ID - 32) |
7261 1 << (OP_CREATE_SESSION - 32) |
7262 1 << (OP_DESTROY_SESSION - 32) |
7263 1 << (OP_DESTROY_CLIENTID - 32)
7264 };
7265 unsigned int i;
7266
7267 if (sp->how == SP4_MACH_CRED) {
7268 /* Print state protect result */
7269 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7270 for (i = 0; i <= LAST_NFS4_OP; i++) {
7271 if (test_bit(i, sp->enforce.u.longs))
7272 dfprintk(MOUNT, " enforce op %d\n", i);
7273 if (test_bit(i, sp->allow.u.longs))
7274 dfprintk(MOUNT, " allow op %d\n", i);
7275 }
7276
7277 /* make sure nothing is on enforce list that isn't supported */
7278 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7279 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7280 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7281 return -EINVAL;
7282 }
7283 }
7284
7285 /*
7286 * Minimal mode - state operations are allowed to use machine
7287 * credential. Note this already happens by default, so the
7288 * client doesn't have to do anything more than the negotiation.
7289 *
7290 * NOTE: we don't care if EXCHANGE_ID is in the list -
7291 * we're already using the machine cred for exchange_id
7292 * and will never use a different cred.
7293 */
7294 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7295 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7296 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7297 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7298 dfprintk(MOUNT, "sp4_mach_cred:\n");
7299 dfprintk(MOUNT, " minimal mode enabled\n");
7300 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7301 } else {
7302 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7303 return -EINVAL;
7304 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007305
7306 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007307 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7308 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007309 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7310 dfprintk(MOUNT, " cleanup mode enabled\n");
7311 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7312 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007313
Andrew Elble99ade3c2015-12-02 09:39:51 -05007314 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7315 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7316 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7317 &clp->cl_sp4_flags);
7318 }
7319
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007320 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7321 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7322 dfprintk(MOUNT, " secinfo mode enabled\n");
7323 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7324 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007325
7326 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7327 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7328 dfprintk(MOUNT, " stateid mode enabled\n");
7329 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7330 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007331
7332 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7333 dfprintk(MOUNT, " write mode enabled\n");
7334 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7335 }
7336
7337 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7338 dfprintk(MOUNT, " commit mode enabled\n");
7339 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7340 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007341 }
7342
7343 return 0;
7344}
7345
Andy Adamson8d89bd72016-09-09 09:22:18 -04007346struct nfs41_exchange_id_data {
7347 struct nfs41_exchange_id_res res;
7348 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007349 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007350 int rpc_status;
7351};
7352
7353static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007354{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007355 struct nfs41_exchange_id_data *cdata =
7356 (struct nfs41_exchange_id_data *)data;
7357 struct nfs_client *clp = cdata->args.client;
7358 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007359
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007360 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007361
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007362 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007363 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007364
7365 if (cdata->xprt && status == 0) {
7366 status = nfs4_detect_session_trunking(clp, &cdata->res,
7367 cdata->xprt);
7368 goto out;
7369 }
7370
Andy Adamson8d89bd72016-09-09 09:22:18 -04007371 if (status == 0)
7372 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007373
Chuck Lever177313f2012-05-21 22:44:58 -04007374 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007375 clp->cl_clientid = cdata->res.clientid;
7376 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007377 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007378 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007379 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007380 &clp->cl_session->session_state);
7381 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007382 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007383
Chuck Leveracdeb692012-05-21 22:46:16 -04007384 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007385 clp->cl_serverowner = cdata->res.server_owner;
7386 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007387
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007388 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007389 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007390 clp->cl_implid = cdata->res.impl_id;
7391 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007392
Chuck Lever177313f2012-05-21 22:44:58 -04007393 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007394 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007395 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007396 dprintk("%s: server_scope mismatch detected\n",
7397 __func__);
7398 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007399 kfree(clp->cl_serverscope);
7400 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007401 }
7402
Chuck Lever177313f2012-05-21 22:44:58 -04007403 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007404 clp->cl_serverscope = cdata->res.server_scope;
7405 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007406 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007407 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007408 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007409 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007410 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007411out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007413 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007414}
7415
7416static void nfs4_exchange_id_release(void *data)
7417{
7418 struct nfs41_exchange_id_data *cdata =
7419 (struct nfs41_exchange_id_data *)data;
7420
Andy Adamsonad0849a2016-09-09 09:22:28 -04007421 if (cdata->xprt) {
7422 xprt_put(cdata->xprt);
7423 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7424 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007425 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007426 kfree(cdata->res.impl_id);
7427 kfree(cdata->res.server_scope);
7428 kfree(cdata->res.server_owner);
7429 kfree(cdata);
7430}
7431
7432static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7433 .rpc_call_done = nfs4_exchange_id_done,
7434 .rpc_release = nfs4_exchange_id_release,
7435};
7436
Benny Halevy99fe60d2009-04-01 09:22:29 -04007437/*
7438 * _nfs4_proc_exchange_id()
7439 *
7440 * Wrapper for EXCHANGE_ID operation.
7441 */
7442static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007443 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007444{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007445 struct rpc_message msg = {
7446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007447 .rpc_cred = cred,
7448 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007449 struct rpc_task_setup task_setup_data = {
7450 .rpc_client = clp->cl_rpcclient,
7451 .callback_ops = &nfs4_exchange_id_call_ops,
7452 .rpc_message = &msg,
7453 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7454 };
7455 struct nfs41_exchange_id_data *calldata;
7456 struct rpc_task *task;
7457 int status = -EIO;
7458
7459 if (!atomic_inc_not_zero(&clp->cl_count))
7460 goto out;
7461
7462 status = -ENOMEM;
7463 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7464 if (!calldata)
7465 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007466
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007467 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007468
7469 status = nfs4_init_uniform_client_string(clp);
7470 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007472
7473 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7474 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7475 clp->cl_owner_id);
7476
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7478 GFP_NOFS);
7479 status = -ENOMEM;
7480 if (unlikely(calldata->res.server_owner == NULL))
7481 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007482
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007486 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007487
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7489 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007490 goto out_server_scope;
7491
7492 switch (sp4_how) {
7493 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007495 break;
7496
7497 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007498 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007499 break;
7500
7501 default:
7502 /* unsupported! */
7503 WARN_ON_ONCE(1);
7504 status = -EINVAL;
7505 goto out_impl_id;
7506 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007507 if (xprt) {
7508 calldata->xprt = xprt;
7509 task_setup_data.rpc_xprt = xprt;
7510 task_setup_data.flags =
7511 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007512 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7513 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007514 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 calldata->args.client = clp;
7516#ifdef CONFIG_NFS_V4_1_MIGRATION
7517 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7518 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7519 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7520#else
7521 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7522 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7523#endif
7524 msg.rpc_argp = &calldata->args;
7525 msg.rpc_resp = &calldata->res;
7526 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007527
Andy Adamson8d89bd72016-09-09 09:22:18 -04007528 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007529 if (IS_ERR(task))
7530 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007531
Andy Adamsonad0849a2016-09-09 09:22:28 -04007532 if (!xprt) {
7533 status = rpc_wait_for_completion_task(task);
7534 if (!status)
7535 status = calldata->rpc_status;
7536 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007537 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007538
Andy Adamson8d89bd72016-09-09 09:22:18 -04007539 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007540out:
Chuck Lever177313f2012-05-21 22:44:58 -04007541 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007542 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007543 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007544 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007545 clp->cl_implid->date.seconds,
7546 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007547 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007548 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007549
7550out_impl_id:
7551 kfree(calldata->res.impl_id);
7552out_server_scope:
7553 kfree(calldata->res.server_scope);
7554out_server_owner:
7555 kfree(calldata->res.server_owner);
7556out_calldata:
7557 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007558 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007559 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007560}
7561
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007562/*
7563 * nfs4_proc_exchange_id()
7564 *
7565 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7566 *
7567 * Since the clientid has expired, all compounds using sessions
7568 * associated with the stale clientid will be returning
7569 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7570 * be in some phase of session reset.
7571 *
7572 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7573 */
7574int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7575{
7576 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7577 int status;
7578
7579 /* try SP4_MACH_CRED if krb5i/p */
7580 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7581 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007582 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007583 if (!status)
7584 return 0;
7585 }
7586
7587 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007588 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007589}
7590
Andy Adamson04fa2c62016-09-09 09:22:29 -04007591/**
7592 * nfs4_test_session_trunk
7593 *
7594 * This is an add_xprt_test() test function called from
7595 * rpc_clnt_setup_test_and_add_xprt.
7596 *
7597 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7598 * and is dereferrenced in nfs4_exchange_id_release
7599 *
7600 * Upon success, add the new transport to the rpc_clnt
7601 *
7602 * @clnt: struct rpc_clnt to get new transport
7603 * @xprt: the rpc_xprt to test
7604 * @data: call data for _nfs4_proc_exchange_id.
7605 */
7606int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7607 void *data)
7608{
7609 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7610 u32 sp4_how;
7611
7612 dprintk("--> %s try %s\n", __func__,
7613 xprt->address_strings[RPC_DISPLAY_ADDR]);
7614
7615 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7616
7617 /* Test connection for session trunking. Async exchange_id call */
7618 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7619}
7620EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7621
Trond Myklebust66245532012-05-25 17:18:09 -04007622static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7623 struct rpc_cred *cred)
7624{
7625 struct rpc_message msg = {
7626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7627 .rpc_argp = clp,
7628 .rpc_cred = cred,
7629 };
7630 int status;
7631
7632 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007633 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007634 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007635 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007636 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7637 return status;
7638}
7639
7640static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7641 struct rpc_cred *cred)
7642{
7643 unsigned int loop;
7644 int ret;
7645
7646 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7647 ret = _nfs4_proc_destroy_clientid(clp, cred);
7648 switch (ret) {
7649 case -NFS4ERR_DELAY:
7650 case -NFS4ERR_CLIENTID_BUSY:
7651 ssleep(1);
7652 break;
7653 default:
7654 return ret;
7655 }
7656 }
7657 return 0;
7658}
7659
7660int nfs4_destroy_clientid(struct nfs_client *clp)
7661{
7662 struct rpc_cred *cred;
7663 int ret = 0;
7664
7665 if (clp->cl_mvops->minor_version < 1)
7666 goto out;
7667 if (clp->cl_exchange_flags == 0)
7668 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007669 if (clp->cl_preserve_clid)
7670 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007671 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007672 ret = nfs4_proc_destroy_clientid(clp, cred);
7673 if (cred)
7674 put_rpccred(cred);
7675 switch (ret) {
7676 case 0:
7677 case -NFS4ERR_STALE_CLIENTID:
7678 clp->cl_exchange_flags = 0;
7679 }
7680out:
7681 return ret;
7682}
7683
Andy Adamson2050f0c2009-04-01 09:22:30 -04007684struct nfs4_get_lease_time_data {
7685 struct nfs4_get_lease_time_args *args;
7686 struct nfs4_get_lease_time_res *res;
7687 struct nfs_client *clp;
7688};
7689
7690static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7691 void *calldata)
7692{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007693 struct nfs4_get_lease_time_data *data =
7694 (struct nfs4_get_lease_time_data *)calldata;
7695
7696 dprintk("--> %s\n", __func__);
7697 /* just setup sequence, do not trigger session recovery
7698 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007699 nfs41_setup_sequence(data->clp->cl_session,
7700 &data->args->la_seq_args,
7701 &data->res->lr_seq_res,
7702 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007703 dprintk("<-- %s\n", __func__);
7704}
7705
7706/*
7707 * Called from nfs4_state_manager thread for session setup, so don't recover
7708 * from sequence operation or clientid errors.
7709 */
7710static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7711{
7712 struct nfs4_get_lease_time_data *data =
7713 (struct nfs4_get_lease_time_data *)calldata;
7714
7715 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007716 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7717 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007718 switch (task->tk_status) {
7719 case -NFS4ERR_DELAY:
7720 case -NFS4ERR_GRACE:
7721 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7722 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7723 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007724 /* fall through */
7725 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007726 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007727 return;
7728 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007729 dprintk("<-- %s\n", __func__);
7730}
7731
Trond Myklebust17280172012-03-11 13:11:00 -04007732static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007733 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7734 .rpc_call_done = nfs4_get_lease_time_done,
7735};
7736
7737int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7738{
7739 struct rpc_task *task;
7740 struct nfs4_get_lease_time_args args;
7741 struct nfs4_get_lease_time_res res = {
7742 .lr_fsinfo = fsinfo,
7743 };
7744 struct nfs4_get_lease_time_data data = {
7745 .args = &args,
7746 .res = &res,
7747 .clp = clp,
7748 };
7749 struct rpc_message msg = {
7750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7751 .rpc_argp = &args,
7752 .rpc_resp = &res,
7753 };
7754 struct rpc_task_setup task_setup = {
7755 .rpc_client = clp->cl_rpcclient,
7756 .rpc_message = &msg,
7757 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007758 .callback_data = &data,
7759 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007760 };
7761 int status;
7762
Chuck Levera9c92d62013-08-09 12:48:18 -04007763 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007764 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007765 dprintk("--> %s\n", __func__);
7766 task = rpc_run_task(&task_setup);
7767
7768 if (IS_ERR(task))
7769 status = PTR_ERR(task);
7770 else {
7771 status = task->tk_status;
7772 rpc_put_task(task);
7773 }
7774 dprintk("<-- %s return %d\n", __func__, status);
7775
7776 return status;
7777}
7778
Andy Adamsonfc931582009-04-01 09:22:31 -04007779/*
7780 * Initialize the values to be used by the client in CREATE_SESSION
7781 * If nfs4_init_session set the fore channel request and response sizes,
7782 * use them.
7783 *
7784 * Set the back channel max_resp_sz_cached to zero to force the client to
7785 * always set csa_cachethis to FALSE because the current implementation
7786 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7787 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007788static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7789 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007790{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007791 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007792 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007793
Andy Adamson18aad3d2013-06-26 12:21:49 -04007794 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7795 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7796
Andy Adamsonfc931582009-04-01 09:22:31 -04007797 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007798 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7799 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007800 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007801 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007802
7803 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007804 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007805 __func__,
7806 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007807 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007808
7809 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007810 args->bc_attrs.max_rqst_sz = max_bc_payload;
7811 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007812 args->bc_attrs.max_resp_sz_cached = 0;
7813 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007814 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007815
7816 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7817 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7818 __func__,
7819 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7820 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7821 args->bc_attrs.max_reqs);
7822}
7823
Trond Myklebust79969dd2015-02-18 11:30:18 -08007824static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7825 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007826{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007827 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007828 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007829
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007830 if (rcvd->max_resp_sz > sent->max_resp_sz)
7831 return -EINVAL;
7832 /*
7833 * Our requested max_ops is the minimum we need; we're not
7834 * prepared to break up compounds into smaller pieces than that.
7835 * So, no point even trying to continue if the server won't
7836 * cooperate:
7837 */
7838 if (rcvd->max_ops < sent->max_ops)
7839 return -EINVAL;
7840 if (rcvd->max_reqs == 0)
7841 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007842 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7843 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007844 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007845}
7846
Trond Myklebust79969dd2015-02-18 11:30:18 -08007847static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7848 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007849{
7850 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007851 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007852
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007853 if (!(res->flags & SESSION4_BACK_CHAN))
7854 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007855 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7856 return -EINVAL;
7857 if (rcvd->max_resp_sz < sent->max_resp_sz)
7858 return -EINVAL;
7859 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7860 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007861 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007862 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007863 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007864 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007865out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007866 return 0;
7867}
Andy Adamson8d353012009-04-01 09:22:32 -04007868
Andy Adamson8d353012009-04-01 09:22:32 -04007869static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007870 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007871{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007872 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007873
Trond Myklebust79969dd2015-02-18 11:30:18 -08007874 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007875 if (ret)
7876 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007877 return nfs4_verify_back_channel_attrs(args, res);
7878}
7879
7880static void nfs4_update_session(struct nfs4_session *session,
7881 struct nfs41_create_session_res *res)
7882{
7883 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007884 /* Mark client id and session as being confirmed */
7885 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7886 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007887 session->flags = res->flags;
7888 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007889 if (res->flags & SESSION4_BACK_CHAN)
7890 memcpy(&session->bc_attrs, &res->bc_attrs,
7891 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007892}
7893
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007894static int _nfs4_proc_create_session(struct nfs_client *clp,
7895 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007896{
7897 struct nfs4_session *session = clp->cl_session;
7898 struct nfs41_create_session_args args = {
7899 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007900 .clientid = clp->cl_clientid,
7901 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007902 .cb_program = NFS4_CALLBACK,
7903 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007904 struct nfs41_create_session_res res;
7905
Andy Adamsonfc931582009-04-01 09:22:31 -04007906 struct rpc_message msg = {
7907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7908 .rpc_argp = &args,
7909 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007910 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007911 };
7912 int status;
7913
Chuck Lever6b26cc82016-05-02 14:40:40 -04007914 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007915 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007916
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007917 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007918 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007919
Trond Myklebustb519d402016-09-11 14:50:01 -04007920 switch (status) {
7921 case -NFS4ERR_STALE_CLIENTID:
7922 case -NFS4ERR_DELAY:
7923 case -ETIMEDOUT:
7924 case -EACCES:
7925 case -EAGAIN:
7926 goto out;
7927 };
7928
7929 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007930 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007931 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007932 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007933 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007934 if (status)
7935 goto out;
7936 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007937 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007938out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007939 return status;
7940}
7941
7942/*
7943 * Issues a CREATE_SESSION operation to the server.
7944 * It is the responsibility of the caller to verify the session is
7945 * expired before calling this routine.
7946 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007947int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007948{
7949 int status;
7950 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007951 struct nfs4_session *session = clp->cl_session;
7952
7953 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7954
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007955 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007956 if (status)
7957 goto out;
7958
Andy Adamsonaacd5532011-11-09 13:58:21 -05007959 /* Init or reset the session slot tables */
7960 status = nfs4_setup_session_slot_tables(session);
7961 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007962 if (status)
7963 goto out;
7964
7965 ptr = (unsigned *)&session->sess_id.data[0];
7966 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7967 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007968out:
7969 dprintk("<-- %s\n", __func__);
7970 return status;
7971}
7972
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007973/*
7974 * Issue the over-the-wire RPC DESTROY_SESSION.
7975 * The caller must serialize access to this routine.
7976 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007977int nfs4_proc_destroy_session(struct nfs4_session *session,
7978 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007979{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007980 struct rpc_message msg = {
7981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7982 .rpc_argp = session,
7983 .rpc_cred = cred,
7984 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007985 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007986
7987 dprintk("--> nfs4_proc_destroy_session\n");
7988
7989 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007990 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7991 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007992
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007993 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007994 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007995
7996 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007997 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007998 "Session has been destroyed regardless...\n", status);
7999
8000 dprintk("<-- nfs4_proc_destroy_session\n");
8001 return status;
8002}
8003
Trond Myklebust7b38c362012-05-23 13:23:31 -04008004/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008005 * Renew the cl_session lease.
8006 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008007struct nfs4_sequence_data {
8008 struct nfs_client *clp;
8009 struct nfs4_sequence_args args;
8010 struct nfs4_sequence_res res;
8011};
8012
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008013static void nfs41_sequence_release(void *data)
8014{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008015 struct nfs4_sequence_data *calldata = data;
8016 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008017
Alexandros Batsakis71358402010-02-05 03:45:05 -08008018 if (atomic_read(&clp->cl_count) > 1)
8019 nfs4_schedule_state_renewal(clp);
8020 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008021 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008022}
8023
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008024static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8025{
8026 switch(task->tk_status) {
8027 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008028 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8029 return -EAGAIN;
8030 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008031 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008032 }
8033 return 0;
8034}
8035
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008036static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008037{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008038 struct nfs4_sequence_data *calldata = data;
8039 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008040
Trond Myklebust14516c32010-07-31 14:29:06 -04008041 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8042 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008043
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008044 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008045 if (task->tk_status < 0) {
8046 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008047 if (atomic_read(&clp->cl_count) == 1)
8048 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008049
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008050 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8051 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008052 return;
8053 }
8054 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008055 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008056out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008057 dprintk("<-- %s\n", __func__);
8058}
8059
8060static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8061{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008062 struct nfs4_sequence_data *calldata = data;
8063 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008064 struct nfs4_sequence_args *args;
8065 struct nfs4_sequence_res *res;
8066
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008067 args = task->tk_msg.rpc_argp;
8068 res = task->tk_msg.rpc_resp;
8069
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008070 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008071}
8072
8073static const struct rpc_call_ops nfs41_sequence_ops = {
8074 .rpc_call_done = nfs41_sequence_call_done,
8075 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008076 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077};
8078
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008079static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8080 struct rpc_cred *cred,
8081 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008082{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008083 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084 struct rpc_message msg = {
8085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8086 .rpc_cred = cred,
8087 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008088 struct rpc_task_setup task_setup_data = {
8089 .rpc_client = clp->cl_rpcclient,
8090 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008091 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008092 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008093 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008094
Alexandros Batsakis71358402010-02-05 03:45:05 -08008095 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008096 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008097 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008098 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008099 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008100 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008101 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008102 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008103 if (is_privileged)
8104 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008105 msg.rpc_argp = &calldata->args;
8106 msg.rpc_resp = &calldata->res;
8107 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008108 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008109
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008110 return rpc_run_task(&task_setup_data);
8111}
8112
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008113static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008114{
8115 struct rpc_task *task;
8116 int ret = 0;
8117
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008118 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008119 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008120 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008121 if (IS_ERR(task))
8122 ret = PTR_ERR(task);
8123 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008124 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008125 dprintk("<-- %s status=%d\n", __func__, ret);
8126 return ret;
8127}
8128
8129static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8130{
8131 struct rpc_task *task;
8132 int ret;
8133
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008134 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008135 if (IS_ERR(task)) {
8136 ret = PTR_ERR(task);
8137 goto out;
8138 }
8139 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008140 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008141 ret = task->tk_status;
8142 rpc_put_task(task);
8143out:
8144 dprintk("<-- %s status=%d\n", __func__, ret);
8145 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008146}
8147
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008148struct nfs4_reclaim_complete_data {
8149 struct nfs_client *clp;
8150 struct nfs41_reclaim_complete_args arg;
8151 struct nfs41_reclaim_complete_res res;
8152};
8153
8154static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8155{
8156 struct nfs4_reclaim_complete_data *calldata = data;
8157
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008158 nfs41_setup_sequence(calldata->clp->cl_session,
8159 &calldata->arg.seq_args,
8160 &calldata->res.seq_res,
8161 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008162}
8163
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008164static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8165{
8166 switch(task->tk_status) {
8167 case 0:
8168 case -NFS4ERR_COMPLETE_ALREADY:
8169 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8170 break;
8171 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008172 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008173 /* fall through */
8174 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008175 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008176 case -NFS4ERR_BADSESSION:
8177 case -NFS4ERR_DEADSESSION:
8178 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8179 nfs4_schedule_session_recovery(clp->cl_session,
8180 task->tk_status);
8181 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008182 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008183 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008184 }
8185 return 0;
8186}
8187
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008188static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8189{
8190 struct nfs4_reclaim_complete_data *calldata = data;
8191 struct nfs_client *clp = calldata->clp;
8192 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8193
8194 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008195 if (!nfs41_sequence_done(task, res))
8196 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008197
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008198 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008199 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8200 rpc_restart_call_prepare(task);
8201 return;
8202 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008203 dprintk("<-- %s\n", __func__);
8204}
8205
8206static void nfs4_free_reclaim_complete_data(void *data)
8207{
8208 struct nfs4_reclaim_complete_data *calldata = data;
8209
8210 kfree(calldata);
8211}
8212
8213static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8214 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8215 .rpc_call_done = nfs4_reclaim_complete_done,
8216 .rpc_release = nfs4_free_reclaim_complete_data,
8217};
8218
8219/*
8220 * Issue a global reclaim complete.
8221 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008222static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8223 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008224{
8225 struct nfs4_reclaim_complete_data *calldata;
8226 struct rpc_task *task;
8227 struct rpc_message msg = {
8228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008229 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008230 };
8231 struct rpc_task_setup task_setup_data = {
8232 .rpc_client = clp->cl_rpcclient,
8233 .rpc_message = &msg,
8234 .callback_ops = &nfs4_reclaim_complete_call_ops,
8235 .flags = RPC_TASK_ASYNC,
8236 };
8237 int status = -ENOMEM;
8238
8239 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008240 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008241 if (calldata == NULL)
8242 goto out;
8243 calldata->clp = clp;
8244 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008245
Chuck Levera9c92d62013-08-09 12:48:18 -04008246 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008247 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008248 msg.rpc_argp = &calldata->arg;
8249 msg.rpc_resp = &calldata->res;
8250 task_setup_data.callback_data = calldata;
8251 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008252 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008253 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008254 goto out;
8255 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008256 status = nfs4_wait_for_completion_rpc_task(task);
8257 if (status == 0)
8258 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008259 rpc_put_task(task);
8260out:
8261 dprintk("<-- %s status=%d\n", __func__, status);
8262 return status;
8263}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008264
8265static void
8266nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8267{
8268 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008269 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008270 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008271
8272 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008273 nfs41_setup_sequence(session, &lgp->args.seq_args,
8274 &lgp->res.seq_res, task);
8275 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008276}
8277
8278static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8279{
8280 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008281
8282 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008283 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008284 dprintk("<-- %s\n", __func__);
8285}
8286
8287static int
8288nfs4_layoutget_handle_exception(struct rpc_task *task,
8289 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8290{
Trond Myklebustee314c22012-10-01 17:25:48 -07008291 struct inode *inode = lgp->args.inode;
8292 struct nfs_server *server = NFS_SERVER(inode);
8293 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008294 int nfs4err = task->tk_status;
8295 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008296 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008297
Boaz Harroshed7e5422014-01-22 20:34:54 +02008298 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008299
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008300 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008301 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008302 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008303
8304 /*
8305 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8306 * on the file. set tk_status to -ENODATA to tell upper layer to
8307 * retry go inband.
8308 */
8309 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008310 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008311 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008312 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008313 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8314 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8315 */
8316 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008317 status = -EOVERFLOW;
8318 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008319 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008320 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008321 * (or clients) writing to the same RAID stripe except when
8322 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008323 *
8324 * Treat it like we would RECALLCONFLICT -- we retry for a little
8325 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008326 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008327 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008328 if (lgp->args.minlength == 0) {
8329 status = -EOVERFLOW;
8330 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008331 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008332 status = -EBUSY;
8333 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008334 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008335 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008336 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008337 case -NFS4ERR_DELEG_REVOKED:
8338 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008339 case -NFS4ERR_EXPIRED:
8340 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008341 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008342 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008343 lo = NFS_I(inode)->layout;
8344 /* If the open stateid was bad, then recover it. */
8345 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8346 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008347 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008348 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008349 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008350 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008351 break;
8352 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008353
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008354 /*
8355 * Mark the bad layout state as invalid, then retry
8356 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008357 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008358 spin_unlock(&inode->i_lock);
8359 pnfs_free_lseg_list(&head);
8360 status = -EAGAIN;
8361 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008362 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008363
Trond Myklebust0a702352017-01-23 22:44:12 -05008364 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008365 err = nfs4_handle_exception(server, nfs4err, exception);
8366 if (!status) {
8367 if (exception->retry)
8368 status = -EAGAIN;
8369 else
8370 status = err;
8371 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008372out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008373 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008374 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008375}
8376
Idan Kedar85541162012-08-02 11:47:10 +03008377static size_t max_response_pages(struct nfs_server *server)
8378{
8379 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8380 return nfs_page_array_len(0, max_resp_sz);
8381}
8382
8383static void nfs4_free_pages(struct page **pages, size_t size)
8384{
8385 int i;
8386
8387 if (!pages)
8388 return;
8389
8390 for (i = 0; i < size; i++) {
8391 if (!pages[i])
8392 break;
8393 __free_page(pages[i]);
8394 }
8395 kfree(pages);
8396}
8397
8398static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8399{
8400 struct page **pages;
8401 int i;
8402
8403 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8404 if (!pages) {
8405 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8406 return NULL;
8407 }
8408
8409 for (i = 0; i < size; i++) {
8410 pages[i] = alloc_page(gfp_flags);
8411 if (!pages[i]) {
8412 dprintk("%s: failed to allocate page\n", __func__);
8413 nfs4_free_pages(pages, size);
8414 return NULL;
8415 }
8416 }
8417
8418 return pages;
8419}
8420
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008421static void nfs4_layoutget_release(void *calldata)
8422{
8423 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008424 struct inode *inode = lgp->args.inode;
8425 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008426 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008427
8428 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008429 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008430 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008431 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008432 put_nfs_open_context(lgp->args.ctx);
8433 kfree(calldata);
8434 dprintk("<-- %s\n", __func__);
8435}
8436
8437static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8438 .rpc_call_prepare = nfs4_layoutget_prepare,
8439 .rpc_call_done = nfs4_layoutget_done,
8440 .rpc_release = nfs4_layoutget_release,
8441};
8442
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008443struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008444nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008445{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008446 struct inode *inode = lgp->args.inode;
8447 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008448 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008449 struct rpc_task *task;
8450 struct rpc_message msg = {
8451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8452 .rpc_argp = &lgp->args,
8453 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008454 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008455 };
8456 struct rpc_task_setup task_setup_data = {
8457 .rpc_client = server->client,
8458 .rpc_message = &msg,
8459 .callback_ops = &nfs4_layoutget_call_ops,
8460 .callback_data = lgp,
8461 .flags = RPC_TASK_ASYNC,
8462 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008463 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008464 struct nfs4_exception exception = {
8465 .inode = inode,
8466 .timeout = *timeout,
8467 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008468 int status = 0;
8469
8470 dprintk("--> %s\n", __func__);
8471
Peng Tao4bd5a982014-11-17 11:05:17 +08008472 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8473 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8474
Idan Kedar85541162012-08-02 11:47:10 +03008475 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8476 if (!lgp->args.layout.pages) {
8477 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008478 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008479 }
8480 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8481
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008482 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008483 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008484 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008485
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008486 task = rpc_run_task(&task_setup_data);
8487 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008488 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008489 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008490 if (status == 0) {
8491 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8492 *timeout = exception.timeout;
8493 }
8494
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008495 trace_nfs4_layoutget(lgp->args.ctx,
8496 &lgp->args.range,
8497 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008498 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008499 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008500
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008501 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8502 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008503 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008504 rpc_put_task(task);
8505 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008506 if (status)
8507 return ERR_PTR(status);
8508 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008509}
8510
Benny Halevycbe82602011-05-22 19:52:37 +03008511static void
8512nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8513{
8514 struct nfs4_layoutreturn *lrp = calldata;
8515
8516 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008517 nfs41_setup_sequence(lrp->clp->cl_session,
8518 &lrp->args.seq_args,
8519 &lrp->res.seq_res,
8520 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008521}
8522
8523static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8524{
8525 struct nfs4_layoutreturn *lrp = calldata;
8526 struct nfs_server *server;
8527
8528 dprintk("--> %s\n", __func__);
8529
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008530 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008531 return;
8532
8533 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008534 switch (task->tk_status) {
8535 default:
8536 task->tk_status = 0;
8537 case 0:
8538 break;
8539 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008540 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008541 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008542 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008543 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008544 return;
8545 }
Benny Halevycbe82602011-05-22 19:52:37 +03008546 dprintk("<-- %s\n", __func__);
8547}
8548
8549static void nfs4_layoutreturn_release(void *calldata)
8550{
8551 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008552 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008553 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008554
8555 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008556 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008557 if (lrp->res.lrs_present) {
8558 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8559 &lrp->args.range,
8560 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008561 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008562 } else
8563 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008564 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008565 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008566 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008567 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008568 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008569 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008570 kfree(calldata);
8571 dprintk("<-- %s\n", __func__);
8572}
8573
8574static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8575 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8576 .rpc_call_done = nfs4_layoutreturn_done,
8577 .rpc_release = nfs4_layoutreturn_release,
8578};
8579
Peng Tao6c166052014-11-17 09:30:40 +08008580int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008581{
8582 struct rpc_task *task;
8583 struct rpc_message msg = {
8584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8585 .rpc_argp = &lrp->args,
8586 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008587 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008588 };
8589 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008590 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008591 .rpc_message = &msg,
8592 .callback_ops = &nfs4_layoutreturn_call_ops,
8593 .callback_data = lrp,
8594 };
Peng Tao6c166052014-11-17 09:30:40 +08008595 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008596
Andrew Elble99ade3c2015-12-02 09:39:51 -05008597 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8598 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8599 &task_setup_data.rpc_client, &msg);
8600
Benny Halevycbe82602011-05-22 19:52:37 +03008601 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008602 if (!sync) {
8603 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8604 if (!lrp->inode) {
8605 nfs4_layoutreturn_release(lrp);
8606 return -EAGAIN;
8607 }
8608 task_setup_data.flags |= RPC_TASK_ASYNC;
8609 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008610 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008611 task = rpc_run_task(&task_setup_data);
8612 if (IS_ERR(task))
8613 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008614 if (sync)
8615 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008616 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008617 dprintk("<-- %s status=%d\n", __func__, status);
8618 rpc_put_task(task);
8619 return status;
8620}
8621
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008622static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008623_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8624 struct pnfs_device *pdev,
8625 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008626{
8627 struct nfs4_getdeviceinfo_args args = {
8628 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008629 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8630 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008631 };
8632 struct nfs4_getdeviceinfo_res res = {
8633 .pdev = pdev,
8634 };
8635 struct rpc_message msg = {
8636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8637 .rpc_argp = &args,
8638 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008639 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008640 };
8641 int status;
8642
8643 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008644 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008645 if (res.notification & ~args.notify_types)
8646 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008647 if (res.notification != args.notify_types)
8648 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008649
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008650 dprintk("<-- %s status=%d\n", __func__, status);
8651
8652 return status;
8653}
8654
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008655int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8656 struct pnfs_device *pdev,
8657 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008658{
8659 struct nfs4_exception exception = { };
8660 int err;
8661
8662 do {
8663 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008664 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008665 &exception);
8666 } while (exception.retry);
8667 return err;
8668}
8669EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8670
Andy Adamson863a3c62011-03-23 13:27:54 +00008671static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8672{
8673 struct nfs4_layoutcommit_data *data = calldata;
8674 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008675 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008676
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008677 nfs41_setup_sequence(session,
8678 &data->args.seq_args,
8679 &data->res.seq_res,
8680 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008681}
8682
8683static void
8684nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8685{
8686 struct nfs4_layoutcommit_data *data = calldata;
8687 struct nfs_server *server = NFS_SERVER(data->args.inode);
8688
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008689 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008690 return;
8691
8692 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008693 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8694 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8695 case -NFS4ERR_BADLAYOUT: /* no layout */
8696 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008697 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008698 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008699 break;
8700 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008701 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008702 rpc_restart_call_prepare(task);
8703 return;
8704 }
8705 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008706}
8707
8708static void nfs4_layoutcommit_release(void *calldata)
8709{
8710 struct nfs4_layoutcommit_data *data = calldata;
8711
Andy Adamsondb29c082011-07-30 20:52:38 -04008712 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008713 nfs_post_op_update_inode_force_wcc(data->args.inode,
8714 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008715 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008716 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008717 kfree(data);
8718}
8719
8720static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8721 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8722 .rpc_call_done = nfs4_layoutcommit_done,
8723 .rpc_release = nfs4_layoutcommit_release,
8724};
8725
8726int
Andy Adamsonef311532011-03-12 02:58:10 -05008727nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008728{
8729 struct rpc_message msg = {
8730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8731 .rpc_argp = &data->args,
8732 .rpc_resp = &data->res,
8733 .rpc_cred = data->cred,
8734 };
8735 struct rpc_task_setup task_setup_data = {
8736 .task = &data->task,
8737 .rpc_client = NFS_CLIENT(data->args.inode),
8738 .rpc_message = &msg,
8739 .callback_ops = &nfs4_layoutcommit_ops,
8740 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008741 };
8742 struct rpc_task *task;
8743 int status = 0;
8744
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008745 dprintk("NFS: initiating layoutcommit call. sync %d "
8746 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008747 data->args.lastbytewritten,
8748 data->args.inode->i_ino);
8749
Trond Myklebust472e2592015-02-05 16:50:30 -05008750 if (!sync) {
8751 data->inode = nfs_igrab_and_active(data->args.inode);
8752 if (data->inode == NULL) {
8753 nfs4_layoutcommit_release(data);
8754 return -EAGAIN;
8755 }
8756 task_setup_data.flags = RPC_TASK_ASYNC;
8757 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008758 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008759 task = rpc_run_task(&task_setup_data);
8760 if (IS_ERR(task))
8761 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008762 if (sync)
8763 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008764 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008765 dprintk("%s: status %d\n", __func__, status);
8766 rpc_put_task(task);
8767 return status;
8768}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008769
Andy Adamson97431202013-08-08 10:57:56 -04008770/**
8771 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8772 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8773 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008774static int
8775_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008776 struct nfs_fsinfo *info,
8777 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008778{
8779 struct nfs41_secinfo_no_name_args args = {
8780 .style = SECINFO_STYLE_CURRENT_FH,
8781 };
8782 struct nfs4_secinfo_res res = {
8783 .flavors = flavors,
8784 };
8785 struct rpc_message msg = {
8786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8787 .rpc_argp = &args,
8788 .rpc_resp = &res,
8789 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008790 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008791 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008792 int status;
8793
8794 if (use_integrity) {
8795 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008796 cred = nfs4_get_clid_cred(server->nfs_client);
8797 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008798 }
8799
8800 dprintk("--> %s\n", __func__);
8801 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8802 &res.seq_res, 0);
8803 dprintk("<-- %s status=%d\n", __func__, status);
8804
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008805 if (cred)
8806 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008807
8808 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008809}
8810
8811static int
8812nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8813 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8814{
8815 struct nfs4_exception exception = { };
8816 int err;
8817 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008818 /* first try using integrity protection */
8819 err = -NFS4ERR_WRONGSEC;
8820
8821 /* try to use integrity protection with machine cred */
8822 if (_nfs4_is_integrity_protected(server->nfs_client))
8823 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8824 flavors, true);
8825
8826 /*
8827 * if unable to use integrity protection, or SECINFO with
8828 * integrity protection returns NFS4ERR_WRONGSEC (which is
8829 * disallowed by spec, but exists in deployed servers) use
8830 * the current filesystem's rpc_client and the user cred.
8831 */
8832 if (err == -NFS4ERR_WRONGSEC)
8833 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8834 flavors, false);
8835
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008836 switch (err) {
8837 case 0:
8838 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008839 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008840 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008841 default:
8842 err = nfs4_handle_exception(server, err, &exception);
8843 }
8844 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008845out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008846 return err;
8847}
8848
8849static int
8850nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8851 struct nfs_fsinfo *info)
8852{
8853 int err;
8854 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008855 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008856 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008857 struct nfs4_secinfo4 *secinfo;
8858 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008859
8860 page = alloc_page(GFP_KERNEL);
8861 if (!page) {
8862 err = -ENOMEM;
8863 goto out;
8864 }
8865
8866 flavors = page_address(page);
8867 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8868
8869 /*
8870 * Fall back on "guess and check" method if
8871 * the server doesn't support SECINFO_NO_NAME
8872 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008873 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008874 err = nfs4_find_root_sec(server, fhandle, info);
8875 goto out_freepage;
8876 }
8877 if (err)
8878 goto out_freepage;
8879
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008880 for (i = 0; i < flavors->num_flavors; i++) {
8881 secinfo = &flavors->flavors[i];
8882
8883 switch (secinfo->flavor) {
8884 case RPC_AUTH_NULL:
8885 case RPC_AUTH_UNIX:
8886 case RPC_AUTH_GSS:
8887 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8888 &secinfo->flavor_info);
8889 break;
8890 default:
8891 flavor = RPC_AUTH_MAXFLAVOR;
8892 break;
8893 }
8894
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008895 if (!nfs_auth_info_match(&server->auth_info, flavor))
8896 flavor = RPC_AUTH_MAXFLAVOR;
8897
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008898 if (flavor != RPC_AUTH_MAXFLAVOR) {
8899 err = nfs4_lookup_root_sec(server, fhandle,
8900 info, flavor);
8901 if (!err)
8902 break;
8903 }
8904 }
8905
8906 if (flavor == RPC_AUTH_MAXFLAVOR)
8907 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008908
8909out_freepage:
8910 put_page(page);
8911 if (err == -EACCES)
8912 return -EPERM;
8913out:
8914 return err;
8915}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008916
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008917static int _nfs41_test_stateid(struct nfs_server *server,
8918 nfs4_stateid *stateid,
8919 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008920{
8921 int status;
8922 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008923 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008924 };
8925 struct nfs41_test_stateid_res res;
8926 struct rpc_message msg = {
8927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8928 .rpc_argp = &args,
8929 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008930 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008931 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008932 struct rpc_clnt *rpc_client = server->client;
8933
8934 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8935 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008936
Chuck Lever38527b12012-07-11 16:30:23 -04008937 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008938 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008939 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008940 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008941 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008942 if (status != NFS_OK) {
8943 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008944 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008945 }
8946 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008947 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008948}
8949
Trond Myklebust43912bb2016-09-22 13:38:56 -04008950static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8951 int err, struct nfs4_exception *exception)
8952{
8953 exception->retry = 0;
8954 switch(err) {
8955 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008956 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008957 nfs4_handle_exception(server, err, exception);
8958 break;
8959 case -NFS4ERR_BADSESSION:
8960 case -NFS4ERR_BADSLOT:
8961 case -NFS4ERR_BAD_HIGH_SLOT:
8962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8963 case -NFS4ERR_DEADSESSION:
8964 nfs4_do_handle_exception(server, err, exception);
8965 }
8966}
8967
Chuck Lever38527b12012-07-11 16:30:23 -04008968/**
8969 * nfs41_test_stateid - perform a TEST_STATEID operation
8970 *
8971 * @server: server / transport on which to perform the operation
8972 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008973 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008974 *
8975 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8976 * Otherwise a negative NFS4ERR value is returned if the operation
8977 * failed or the state ID is not currently valid.
8978 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008979static int nfs41_test_stateid(struct nfs_server *server,
8980 nfs4_stateid *stateid,
8981 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008982{
8983 struct nfs4_exception exception = { };
8984 int err;
8985 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008986 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008987 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008988 } while (exception.retry);
8989 return err;
8990}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008991
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008992struct nfs_free_stateid_data {
8993 struct nfs_server *server;
8994 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008995 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008996};
8997
8998static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8999{
9000 struct nfs_free_stateid_data *data = calldata;
9001 nfs41_setup_sequence(nfs4_get_session(data->server),
9002 &data->args.seq_args,
9003 &data->res.seq_res,
9004 task);
9005}
9006
9007static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9008{
9009 struct nfs_free_stateid_data *data = calldata;
9010
9011 nfs41_sequence_done(task, &data->res.seq_res);
9012
9013 switch (task->tk_status) {
9014 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009015 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009016 rpc_restart_call_prepare(task);
9017 }
9018}
9019
9020static void nfs41_free_stateid_release(void *calldata)
9021{
9022 kfree(calldata);
9023}
9024
Trond Myklebust17f26b12013-08-21 15:48:42 -04009025static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009026 .rpc_call_prepare = nfs41_free_stateid_prepare,
9027 .rpc_call_done = nfs41_free_stateid_done,
9028 .rpc_release = nfs41_free_stateid_release,
9029};
9030
9031static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009032 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009033 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009034 bool privileged)
9035{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009036 struct rpc_message msg = {
9037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009038 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009039 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009040 struct rpc_task_setup task_setup = {
9041 .rpc_client = server->client,
9042 .rpc_message = &msg,
9043 .callback_ops = &nfs41_free_stateid_ops,
9044 .flags = RPC_TASK_ASYNC,
9045 };
9046 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009047
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009048 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9049 &task_setup.rpc_client, &msg);
9050
Chuck Lever38527b12012-07-11 16:30:23 -04009051 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009052 data = kmalloc(sizeof(*data), GFP_NOFS);
9053 if (!data)
9054 return ERR_PTR(-ENOMEM);
9055 data->server = server;
9056 nfs4_stateid_copy(&data->args.stateid, stateid);
9057
9058 task_setup.callback_data = data;
9059
9060 msg.rpc_argp = &data->args;
9061 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009062 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009063 if (privileged)
9064 nfs4_set_sequence_privileged(&data->args.seq_args);
9065
9066 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009067}
9068
Chuck Lever38527b12012-07-11 16:30:23 -04009069/**
9070 * nfs41_free_stateid - perform a FREE_STATEID operation
9071 *
9072 * @server: server / transport on which to perform the operation
9073 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009074 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009075 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009076 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009077 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009078 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009079static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009080 const nfs4_stateid *stateid,
9081 struct rpc_cred *cred,
9082 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009083{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009084 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009085
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009086 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009087 if (IS_ERR(task))
9088 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009089 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009090 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009091}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009092
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009093static void
9094nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009095{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009096 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009097
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009098 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009099 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009100}
9101
Trond Myklebust36281ca2012-03-04 18:13:56 -05009102static bool nfs41_match_stateid(const nfs4_stateid *s1,
9103 const nfs4_stateid *s2)
9104{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009105 if (s1->type != s2->type)
9106 return false;
9107
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009108 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009109 return false;
9110
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009111 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009112 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009113 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009114 return true;
9115
9116 return false;
9117}
9118
Andy Adamson557134a2009-04-01 09:21:53 -04009119#endif /* CONFIG_NFS_V4_1 */
9120
Trond Myklebust36281ca2012-03-04 18:13:56 -05009121static bool nfs4_match_stateid(const nfs4_stateid *s1,
9122 const nfs4_stateid *s2)
9123{
Trond Myklebustf597c532012-03-04 18:13:56 -05009124 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009125}
9126
9127
Trond Myklebust17280172012-03-11 13:11:00 -04009128static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009129 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009130 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131 .recover_open = nfs4_open_reclaim,
9132 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009133 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009134 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009135};
9136
Andy Adamson591d71c2009-04-01 09:22:47 -04009137#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009138static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009139 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9140 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9141 .recover_open = nfs4_open_reclaim,
9142 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009143 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009144 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009145 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009146};
9147#endif /* CONFIG_NFS_V4_1 */
9148
Trond Myklebust17280172012-03-11 13:11:00 -04009149static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009150 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009151 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009152 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009153 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009154 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155};
9156
Andy Adamson591d71c2009-04-01 09:22:47 -04009157#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009158static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009159 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9160 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009161 .recover_open = nfs41_open_expired,
9162 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009163 .establish_clid = nfs41_init_clientid,
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_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009168 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009169 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009170 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009171};
9172
9173#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009174static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009175 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009176 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009177 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009178};
9179#endif
9180
Chuck Leverec011fe2013-10-17 14:12:39 -04009181static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009182 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009183 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009184};
9185
9186#if defined(CONFIG_NFS_V4_1)
9187static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009188 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009189 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009190};
9191#endif /* CONFIG_NFS_V4_1 */
9192
Trond Myklebust97dc1352010-06-16 09:52:26 -04009193static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9194 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009195 .init_caps = NFS_CAP_READDIRPLUS
9196 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009197 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009198 .init_client = nfs40_init_client,
9199 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009200 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009201 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009202 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009203 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009204 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009205 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009206 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9207 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9208 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009209 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009210};
9211
9212#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009213static struct nfs_seqid *
9214nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9215{
9216 return NULL;
9217}
9218
Trond Myklebust97dc1352010-06-16 09:52:26 -04009219static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9220 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009221 .init_caps = NFS_CAP_READDIRPLUS
9222 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009223 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009224 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009225 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009226 .init_client = nfs41_init_client,
9227 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009228 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009229 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009230 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009231 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009232 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009233 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009234 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009235 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9236 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9237 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009238 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009239};
9240#endif
9241
Steve Dickson42c2c422013-05-22 12:50:38 -04009242#if defined(CONFIG_NFS_V4_2)
9243static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9244 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009245 .init_caps = NFS_CAP_READDIRPLUS
9246 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009247 | NFS_CAP_POSIX_LOCK
9248 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009249 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009250 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009251 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009252 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009253 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009254 | NFS_CAP_LAYOUTSTATS
9255 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009256 .init_client = nfs41_init_client,
9257 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009258 .match_stateid = nfs41_match_stateid,
9259 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009260 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009261 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009262 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009263 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009264 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009265 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9266 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9267 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009268 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009269};
9270#endif
9271
Trond Myklebust97dc1352010-06-16 09:52:26 -04009272const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9273 [0] = &nfs_v4_0_minor_ops,
9274#if defined(CONFIG_NFS_V4_1)
9275 [1] = &nfs_v4_1_minor_ops,
9276#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009277#if defined(CONFIG_NFS_V4_2)
9278 [2] = &nfs_v4_2_minor_ops,
9279#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009280};
9281
Trond Myklebust13997822016-07-24 17:10:52 -04009282static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009283{
9284 ssize_t error, error2;
9285
9286 error = generic_listxattr(dentry, list, size);
9287 if (error < 0)
9288 return error;
9289 if (list) {
9290 list += error;
9291 size -= error;
9292 }
9293
9294 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9295 if (error2 < 0)
9296 return error2;
9297 return error + error2;
9298}
9299
Trond Myklebust17f26b12013-08-21 15:48:42 -04009300static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009301 .create = nfs_create,
9302 .lookup = nfs_lookup,
9303 .atomic_open = nfs_atomic_open,
9304 .link = nfs_link,
9305 .unlink = nfs_unlink,
9306 .symlink = nfs_symlink,
9307 .mkdir = nfs_mkdir,
9308 .rmdir = nfs_rmdir,
9309 .mknod = nfs_mknod,
9310 .rename = nfs_rename,
9311 .permission = nfs_permission,
9312 .getattr = nfs_getattr,
9313 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009314 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009315};
9316
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009317static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009318 .permission = nfs_permission,
9319 .getattr = nfs_getattr,
9320 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009321 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009322};
9323
David Howells509de812006-08-22 20:06:11 -04009324const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 .version = 4, /* protocol version */
9326 .dentry_ops = &nfs4_dentry_operations,
9327 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009328 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009329 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009331 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009332 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333 .getattr = nfs4_proc_getattr,
9334 .setattr = nfs4_proc_setattr,
9335 .lookup = nfs4_proc_lookup,
9336 .access = nfs4_proc_access,
9337 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 .create = nfs4_proc_create,
9339 .remove = nfs4_proc_remove,
9340 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009341 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009343 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009344 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009345 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346 .link = nfs4_proc_link,
9347 .symlink = nfs4_proc_symlink,
9348 .mkdir = nfs4_proc_mkdir,
9349 .rmdir = nfs4_proc_remove,
9350 .readdir = nfs4_proc_readdir,
9351 .mknod = nfs4_proc_mknod,
9352 .statfs = nfs4_proc_statfs,
9353 .fsinfo = nfs4_proc_fsinfo,
9354 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009355 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009357 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009358 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009359 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009361 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009363 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009364 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009366 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009367 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009368 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009369 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009370 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009371 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009372 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009373 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009374 .create_server = nfs4_create_server,
9375 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376};
9377
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009378static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009379 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009380 .list = nfs4_xattr_list_nfs4_acl,
9381 .get = nfs4_xattr_get_nfs4_acl,
9382 .set = nfs4_xattr_set_nfs4_acl,
9383};
9384
9385const struct xattr_handler *nfs4_xattr_handlers[] = {
9386 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009387#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9388 &nfs4_xattr_nfs4_label_handler,
9389#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009390 NULL
9391};
9392
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393/*
9394 * Local variables:
9395 * c-basic-offset: 8
9396 * End:
9397 */