blob: ea29c608be893588941f6c713b420b516b214341 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040096 struct nfs4_state *state, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
228 | FATTR4_WORD0_CHANGE
229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500258 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500263 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD1_OWNER_GROUP
265 | FATTR4_WORD1_RAWDEV
266 | FATTR4_WORD1_SPACE_USED
267 | FATTR4_WORD1_TIME_ACCESS
268 | FATTR4_WORD1_TIME_METADATA
269 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400270 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400271};
272
Al Virobc4785c2006-10-19 23:28:51 -0700273static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct nfs4_readdir_arg *readdir)
275{
Al Viro0dbb4c62006-10-19 23:28:49 -0700276 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000279 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
281 return;
282 }
283
284 readdir->cookie = 0;
285 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
286 if (cookie == 2)
287 return;
288
289 /*
290 * NFSv4 servers do not return entries for '.' and '..'
291 * Therefore, we fake these entries here. We let '.'
292 * have cookie 0 and '..' have cookie 1. Note that
293 * when talking to the server, we always send cookie 0
294 * instead of 1 or 2.
295 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800296 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (cookie == 0) {
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_one; /* cookie, second word */
302 *p++ = xdr_one; /* entry len */
303 memcpy(p, ".\0\0\0", 4); /* entry */
304 p++;
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000308 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 *p++ = xdr_one; /* next */
312 *p++ = xdr_zero; /* cookie, first word */
313 *p++ = xdr_two; /* cookie, second word */
314 *p++ = xdr_two; /* entry len */
315 memcpy(p, "..\0\0", 4); /* entry */
316 p++;
317 *p++ = xdr_one; /* bitmap length */
318 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
319 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000320 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 readdir->pgbase = (char *)p - (char *)start;
323 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800324 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Trond Myklebust26d36302016-09-22 13:39:05 -0400327static void nfs4_test_and_free_stateid(struct nfs_server *server,
328 nfs4_stateid *stateid,
329 struct rpc_cred *cred)
330{
331 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
332
333 ops->test_and_free_expired(server, stateid, cred);
334}
335
336static void __nfs4_free_revoked_stateid(struct nfs_server *server,
337 nfs4_stateid *stateid,
338 struct rpc_cred *cred)
339{
340 stateid->type = NFS4_REVOKED_STATEID_TYPE;
341 nfs4_test_and_free_stateid(server, stateid, cred);
342}
343
344static void nfs4_free_revoked_stateid(struct nfs_server *server,
345 const nfs4_stateid *stateid,
346 struct rpc_cred *cred)
347{
348 nfs4_stateid tmp;
349
350 nfs4_stateid_copy(&tmp, stateid);
351 __nfs4_free_revoked_stateid(server, &tmp, cred);
352}
353
NeilBrown8478eaa2014-09-18 16:09:27 +1000354static long nfs4_update_delay(long *timeout)
355{
356 long ret;
357 if (!timeout)
358 return NFS4_POLL_RETRY_MAX;
359 if (*timeout <= 0)
360 *timeout = NFS4_POLL_RETRY_MIN;
361 if (*timeout > NFS4_POLL_RETRY_MAX)
362 *timeout = NFS4_POLL_RETRY_MAX;
363 ret = *timeout;
364 *timeout <<= 1;
365 return ret;
366}
367
Trond Myklebust65de8722008-12-23 15:21:44 -0500368static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
369{
370 int res = 0;
371
372 might_sleep();
373
NeilBrown8478eaa2014-09-18 16:09:27 +1000374 freezable_schedule_timeout_killable_unsafe(
375 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500376 if (fatal_signal_pending(current))
377 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 return res;
379}
380
381/* This is the error handling routine for processes that are allowed
382 * to sleep.
383 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400384static int nfs4_do_handle_exception(struct nfs_server *server,
385 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500386{
387 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500388 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400389 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500390 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500391 int ret = errorcode;
392
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400393 exception->delay = 0;
394 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400396
397 if (stateid == NULL && state != NULL)
398 stateid = &state->stateid;
399
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 switch(errorcode) {
401 case 0:
402 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400403 case -NFS4ERR_DELEG_REVOKED:
404 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400405 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 if (inode != NULL && stateid != NULL) {
408 nfs_inode_find_state_and_recover(inode,
409 stateid);
410 goto wait_on_recovery;
411 }
412 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400413 if (inode) {
414 int err;
415
416 err = nfs_async_inode_return_delegation(inode,
417 stateid);
418 if (err == 0)
419 goto wait_on_recovery;
420 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
421 exception->retry = 1;
422 break;
423 }
424 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 if (state == NULL)
426 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400427 ret = nfs4_schedule_stateid_recovery(server, state);
428 if (ret < 0)
429 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500430 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500431 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500433 nfs4_schedule_lease_recovery(clp);
434 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400435 case -NFS4ERR_MOVED:
436 ret = nfs4_schedule_migration_recovery(server);
437 if (ret < 0)
438 break;
439 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400440 case -NFS4ERR_LEASE_MOVED:
441 nfs4_schedule_lease_moved_recovery(clp);
442 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500443#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400444 case -NFS4ERR_BADSESSION:
445 case -NFS4ERR_BADSLOT:
446 case -NFS4ERR_BAD_HIGH_SLOT:
447 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
448 case -NFS4ERR_DEADSESSION:
449 case -NFS4ERR_SEQ_FALSE_RETRY:
450 case -NFS4ERR_SEQ_MISORDERED:
451 dprintk("%s ERROR: %d Reset session\n", __func__,
452 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400453 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500457 if (exception->timeout > HZ) {
458 /* We have retried a decent amount, time to
459 * fail
460 */
461 ret = -EBUSY;
462 break;
463 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400465 nfs_inc_server_stats(server, NFSIOS_DELAY);
466 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400467 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400468 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400469 exception->delay = 1;
470 return 0;
471
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400472 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_OLD_STATEID:
474 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800475 break;
476 case -NFS4ERR_BADOWNER:
477 /* The following works around a Linux server bug! */
478 case -NFS4ERR_BADNAME:
479 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
480 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
481 exception->retry = 1;
482 printk(KERN_WARNING "NFS: v4 server %s "
483 "does not accept raw "
484 "uid/gids. "
485 "Reenabling the idmapper.\n",
486 server->nfs_client->cl_hostname);
487 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500488 }
489 /* We failed to handle the error */
490 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500491wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400492 exception->recovering = 1;
493 return 0;
494}
495
496/* This is the error handling routine for processes that are allowed
497 * to sleep.
498 */
499int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
500{
501 struct nfs_client *clp = server->nfs_client;
502 int ret;
503
504 ret = nfs4_do_handle_exception(server, errorcode, exception);
505 if (exception->delay) {
506 ret = nfs4_delay(server->client, &exception->timeout);
507 goto out_retry;
508 }
509 if (exception->recovering) {
510 ret = nfs4_wait_clnt_recover(clp);
511 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
512 return -EIO;
513 goto out_retry;
514 }
515 return ret;
516out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500520}
521
Trond Myklebust037fc982015-09-20 15:51:00 -0400522static int
523nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
524 int errorcode, struct nfs4_exception *exception)
525{
526 struct nfs_client *clp = server->nfs_client;
527 int ret;
528
529 ret = nfs4_do_handle_exception(server, errorcode, exception);
530 if (exception->delay) {
531 rpc_delay(task, nfs4_update_delay(&exception->timeout));
532 goto out_retry;
533 }
534 if (exception->recovering) {
535 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
536 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
537 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
538 goto out_retry;
539 }
540 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
541 ret = -EIO;
542 return ret;
543out_retry:
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500544 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400545 exception->retry = 1;
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500546 /*
547 * For NFS4ERR_MOVED, the client transport will need to
548 * be recomputed after migration recovery has completed.
549 */
550 if (errorcode == -NFS4ERR_MOVED)
551 rpc_task_release_transport(task);
552 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
585
586 if (flavor == RPC_AUTH_GSS_KRB5I ||
587 flavor == RPC_AUTH_GSS_KRB5P)
588 return true;
589
590 return false;
591}
Trond Myklebust65de8722008-12-23 15:21:44 -0500592
Trond Myklebust452e9352010-07-31 14:29:06 -0400593static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&clp->cl_lock);
596 if (time_before(clp->cl_last_renewal,timestamp))
597 clp->cl_last_renewal = timestamp;
598 spin_unlock(&clp->cl_lock);
599}
600
Trond Myklebust452e9352010-07-31 14:29:06 -0400601static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
602{
Trond Myklebustbe824162015-07-05 14:50:46 -0400603 struct nfs_client *clp = server->nfs_client;
604
605 if (!nfs4_has_session(clp))
606 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400607}
608
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400609struct nfs4_call_sync_data {
610 const struct nfs_server *seq_server;
611 struct nfs4_sequence_args *seq_args;
612 struct nfs4_sequence_res *seq_res;
613};
614
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800615void nfs4_init_sequence(struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400617{
618 args->sa_slot = NULL;
619 args->sa_cache_this = cache_reply;
620 args->sa_privileged = 0;
621
622 res->sr_slot = NULL;
623}
624
625static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
626{
627 args->sa_privileged = 1;
628}
629
Peng Taocb04ad22014-06-11 05:24:15 +0800630int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
631 struct nfs4_sequence_args *args,
632 struct nfs4_sequence_res *res,
633 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400634{
Chuck Lever3bd23842013-08-09 12:49:19 -0400635 struct nfs4_slot *slot;
636
637 /* slot already allocated? */
638 if (res->sr_slot != NULL)
639 goto out_start;
640
641 spin_lock(&tbl->slot_tbl_lock);
642 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
643 goto out_sleep;
644
645 slot = nfs4_alloc_slot(tbl);
646 if (IS_ERR(slot)) {
647 if (slot == ERR_PTR(-ENOMEM))
648 task->tk_timeout = HZ >> 2;
649 goto out_sleep;
650 }
651 spin_unlock(&tbl->slot_tbl_lock);
652
Trond Myklebust0a014a42016-09-22 13:38:51 -0400653 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400654 args->sa_slot = slot;
655 res->sr_slot = slot;
656
657out_start:
658 rpc_call_start(task);
659 return 0;
660
661out_sleep:
662 if (args->sa_privileged)
663 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
664 NULL, RPC_PRIORITY_PRIVILEGED);
665 else
666 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
667 spin_unlock(&tbl->slot_tbl_lock);
668 return -EAGAIN;
669}
Peng Taocb04ad22014-06-11 05:24:15 +0800670EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400671
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400672static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400673{
674 struct nfs4_slot *slot = res->sr_slot;
675 struct nfs4_slot_table *tbl;
676
Chuck Lever3bd23842013-08-09 12:49:19 -0400677 tbl = slot->table;
678 spin_lock(&tbl->slot_tbl_lock);
679 if (!nfs41_wake_and_assign_slot(tbl, slot))
680 nfs4_free_slot(tbl, slot);
681 spin_unlock(&tbl->slot_tbl_lock);
682
683 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400684}
685
686static int nfs40_sequence_done(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
688{
689 if (res->sr_slot != NULL)
690 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400691 return 1;
692}
693
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694#if defined(CONFIG_NFS_V4_1)
695
Trond Myklebustd185a332010-06-16 09:52:25 -0400696static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400699 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500701 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500705
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400706 /* Bump the slot sequence number */
707 if (slot->seq_done)
708 slot->seq_nr++;
709 slot->seq_done = 0;
710
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500711 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500712 /* Be nice to the server: try to ensure that the last transmitted
713 * value for highest_user_slotid <= target_highest_slotid
714 */
715 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
716 send_new_highest_used_slotid = true;
717
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719 send_new_highest_used_slotid = false;
720 goto out_unlock;
721 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500722 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723
724 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
725 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500726out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500727 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400728 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500729 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400730 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400731 if (waitqueue_active(&tbl->slot_waitq))
732 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400733}
734
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400735static int nfs41_sequence_process(struct rpc_task *task,
736 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400737{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500738 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500741 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500742 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400743
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500744 if (slot == NULL)
745 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400746 /* don't increment the sequence number if the task wasn't sent */
747 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400748 goto out;
749
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500750 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500751
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 if (slot->interrupted) {
753 slot->interrupted = 0;
754 interrupted = true;
755 }
756
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400757 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500758 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400759 switch (res->sr_status) {
760 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400761 /* If previous op on slot was interrupted and we reused
762 * the seq# and got a reply from the cache, then retry
763 */
764 if (task->tk_status == -EREMOTEIO && interrupted) {
765 ++slot->seq_nr;
766 goto retry_nowait;
767 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400768 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400769 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500770 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500771 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500772 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400773 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
774 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500775 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500777 case 1:
778 /*
779 * sr_status remains 1 if an RPC level error occurred.
780 * The server may or may not have processed the sequence
781 * operation..
782 * Mark the slot as having hosted an interrupted RPC call.
783 */
784 slot->interrupted = 1;
785 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400786 case -NFS4ERR_DELAY:
787 /* The server detected a resend of the RPC call and
788 * returned NFS4ERR_DELAY as per Section 2.10.6.2
789 * of RFC5661.
790 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500791 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400792 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500793 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500794 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400795 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500796 case -NFS4ERR_BADSLOT:
797 /*
798 * The slot id we used was probably retired. Try again
799 * using a different slot id.
800 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500801 goto retry_nowait;
802 case -NFS4ERR_SEQ_MISORDERED:
803 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500804 * Was the last operation on this sequence interrupted?
805 * If so, retry after bumping the sequence number.
806 */
807 if (interrupted) {
808 ++slot->seq_nr;
809 goto retry_nowait;
810 }
811 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500812 * Could this slot have been previously retired?
813 * If so, then the server may be expecting seq_nr = 1!
814 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500815 if (slot->seq_nr != 1) {
816 slot->seq_nr = 1;
817 goto retry_nowait;
818 }
819 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500820 case -NFS4ERR_SEQ_FALSE_RETRY:
821 ++slot->seq_nr;
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168a2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168a2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001112 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1115 struct iattr attrs;
1116 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001118 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001119 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int cancelled;
1122};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001124struct nfs4_open_createattrs {
1125 struct nfs4_label *label;
1126 struct iattr *sattr;
1127 const __u32 verf[2];
1128};
1129
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001130static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1131 int err, struct nfs4_exception *exception)
1132{
1133 if (err != -EINVAL)
1134 return false;
1135 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1136 return false;
1137 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1138 exception->retry = 1;
1139 return true;
1140}
1141
Trond Myklebust6ae37332015-01-30 14:21:14 -05001142static u32
1143nfs4_map_atomic_open_share(struct nfs_server *server,
1144 fmode_t fmode, int openflags)
1145{
1146 u32 res = 0;
1147
1148 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1149 case FMODE_READ:
1150 res = NFS4_SHARE_ACCESS_READ;
1151 break;
1152 case FMODE_WRITE:
1153 res = NFS4_SHARE_ACCESS_WRITE;
1154 break;
1155 case FMODE_READ|FMODE_WRITE:
1156 res = NFS4_SHARE_ACCESS_BOTH;
1157 }
1158 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1159 goto out;
1160 /* Want no delegation if we're using O_DIRECT */
1161 if (openflags & O_DIRECT)
1162 res |= NFS4_SHARE_WANT_NO_DELEG;
1163out:
1164 return res;
1165}
1166
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001167static enum open_claim_type4
1168nfs4_map_atomic_open_claim(struct nfs_server *server,
1169 enum open_claim_type4 claim)
1170{
1171 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1172 return claim;
1173 switch (claim) {
1174 default:
1175 return claim;
1176 case NFS4_OPEN_CLAIM_FH:
1177 return NFS4_OPEN_CLAIM_NULL;
1178 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1179 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1180 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1182 }
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186{
1187 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001188 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->o_res.seqid = p->o_arg.seqid;
1190 p->c_res.seqid = p->c_arg.seqid;
1191 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001192 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001194 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195}
1196
Al Viro82a2c1b2011-06-22 18:30:55 -04001197static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001199 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001200 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001201 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202{
Al Viro82a2c1b2011-06-22 18:30:55 -04001203 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001204 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001207 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208 struct nfs4_opendata *p;
1209
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 if (p == NULL)
1212 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001213
1214 p->f_label = nfs4_label_alloc(server, gfp_mask);
1215 if (IS_ERR(p->f_label))
1216 goto err_free_p;
1217
Kinglong Meea49c2692015-07-27 15:31:38 +08001218 p->a_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->a_label))
1220 goto err_free_f;
1221
Trond Myklebust63f5f792015-01-23 19:19:25 -05001222 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1223 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001224 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001225 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001226 nfs_sb_active(dentry->d_sb);
1227 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 p->dir = parent;
1229 p->owner = sp;
1230 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001231 p->o_arg.open_flags = flags;
1232 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001233 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1234 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001235 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1236 * will return permission denied for all bits until close */
1237 if (!(flags & O_EXCL)) {
1238 /* ask server to check for all possible rights as results
1239 * are cached */
1240 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1241 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001242 }
David Howells7539bba2006-08-22 20:06:09 -04001243 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001244 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1245 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001246 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001248 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001249 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001250 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001251 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1252 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001253 case NFS4_OPEN_CLAIM_NULL:
1254 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1255 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1256 p->o_arg.fh = NFS_FH(dir);
1257 break;
1258 case NFS4_OPEN_CLAIM_PREVIOUS:
1259 case NFS4_OPEN_CLAIM_FH:
1260 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1261 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001262 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001263 }
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001264 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001266 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001267
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001268 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001269 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001271 p->c_arg.fh = &p->o_res.fh;
1272 p->c_arg.stateid = &p->o_res.stateid;
1273 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001274 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001277
1278err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001279 nfs4_label_free(p->a_label);
1280err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001281 nfs4_label_free(p->f_label);
1282err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 kfree(p);
1284err:
1285 dput(parent);
1286 return NULL;
1287}
1288
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001291 struct nfs4_opendata *p = container_of(kref,
1292 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001293 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001294
1295 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001296 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001297 if (p->state != NULL)
1298 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001299 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001300
Kinglong Meea49c2692015-07-27 15:31:38 +08001301 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001302 nfs4_label_free(p->f_label);
1303
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001305 dput(p->dentry);
1306 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001307 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001308 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 kfree(p);
1310}
1311
1312static void nfs4_opendata_put(struct nfs4_opendata *p)
1313{
1314 if (p != NULL)
1315 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316}
1317
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1319{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001320 int ret;
1321
Trond Myklebust06f814a2006-01-03 09:55:07 +01001322 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001323 return ret;
1324}
1325
Trond Myklebust24311f82015-09-20 10:50:17 -04001326static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1327 fmode_t fmode)
1328{
1329 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1330 case FMODE_READ|FMODE_WRITE:
1331 return state->n_rdwr != 0;
1332 case FMODE_WRITE:
1333 return state->n_wronly != 0;
1334 case FMODE_READ:
1335 return state->n_rdonly != 0;
1336 }
1337 WARN_ON_ONCE(1);
1338 return false;
1339}
1340
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001342{
1343 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344
Trond Myklebust536e43d2012-01-17 22:04:26 -05001345 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 goto out;
1347 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001348 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001349 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1350 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001351 break;
1352 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001353 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1354 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001355 break;
1356 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001357 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1358 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 return ret;
1362}
1363
Trond Myklebust2a606182015-08-19 22:30:00 -05001364static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1365 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001367 if (delegation == NULL)
1368 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001369 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001371 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1372 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001373 switch (claim) {
1374 case NFS4_OPEN_CLAIM_NULL:
1375 case NFS4_OPEN_CLAIM_FH:
1376 break;
1377 case NFS4_OPEN_CLAIM_PREVIOUS:
1378 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1379 break;
1380 default:
1381 return 0;
1382 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001383 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384 return 1;
1385}
1386
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001388{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001389 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001390 case FMODE_WRITE:
1391 state->n_wronly++;
1392 break;
1393 case FMODE_READ:
1394 state->n_rdonly++;
1395 break;
1396 case FMODE_READ|FMODE_WRITE:
1397 state->n_rdwr++;
1398 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001399 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001400}
1401
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001402#ifdef CONFIG_NFS_V4_1
1403static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1404{
1405 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1406 return true;
1407 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1408 return true;
1409 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1410 return true;
1411 return false;
1412}
1413#endif /* CONFIG_NFS_V4_1 */
1414
Trond Myklebust4f14c192014-02-12 19:15:06 -05001415static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001416{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001417 struct nfs_client *clp = state->owner->so_server->nfs_client;
1418 bool need_recover = false;
1419
1420 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1421 need_recover = true;
1422 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1423 need_recover = true;
1424 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1425 need_recover = true;
1426 if (need_recover)
1427 nfs4_state_mark_reclaim_nograce(clp, state);
1428}
1429
Trond Myklebuste999e802014-02-10 18:20:47 -05001430static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001431 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001432{
1433 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1434 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001436 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001438 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001439 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001440 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1441 return true;
1442 return false;
1443}
1444
Trond Myklebustf95549c2015-01-23 18:06:09 -05001445static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1446{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001447 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1448 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001449 if (state->n_wronly)
1450 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1451 if (state->n_rdonly)
1452 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1453 if (state->n_rdwr)
1454 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001455 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001456}
1457
Trond Myklebust226056c2014-02-11 10:41:07 -05001458static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1459 nfs4_stateid *stateid, fmode_t fmode)
1460{
1461 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1462 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1463 case FMODE_WRITE:
1464 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1465 break;
1466 case FMODE_READ:
1467 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1468 break;
1469 case 0:
1470 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1471 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1472 clear_bit(NFS_OPEN_STATE, &state->flags);
1473 }
1474 if (stateid == NULL)
1475 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001476 /* Handle OPEN+OPEN_DOWNGRADE races */
1477 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1478 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001479 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001480 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001481 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001482 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001483 nfs4_stateid_copy(&state->stateid, stateid);
1484 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001485}
1486
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001487static void nfs_clear_open_stateid(struct nfs4_state *state,
1488 nfs4_stateid *arg_stateid,
1489 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001490{
1491 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001492 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1493 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1494 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001495 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001496 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1497 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001498}
1499
Trond Myklebust1393d962016-09-22 13:39:13 -04001500static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1501 const nfs4_stateid *stateid, fmode_t fmode,
1502 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001503{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001504 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001505 case FMODE_READ:
1506 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1507 break;
1508 case FMODE_WRITE:
1509 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1510 break;
1511 case FMODE_READ|FMODE_WRITE:
1512 set_bit(NFS_O_RDWR_STATE, &state->flags);
1513 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001514 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001515 return;
1516 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1517 nfs4_stateid_copy(&state->stateid, stateid);
1518 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001519}
1520
Trond Myklebust1393d962016-09-22 13:39:13 -04001521static void __update_open_stateid(struct nfs4_state *state,
1522 const nfs4_stateid *open_stateid,
1523 const nfs4_stateid *deleg_stateid,
1524 fmode_t fmode,
1525 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001527 /*
1528 * Protect the call to nfs4_state_set_mode_locked and
1529 * serialise the stateid update
1530 */
Andrew Elble361cad32015-12-02 09:20:57 -05001531 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001532 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001533 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001534 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001535 set_bit(NFS_DELEGATED_STATE, &state->flags);
1536 }
1537 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001538 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001539 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001540 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001541 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Trond Myklebust1393d962016-09-22 13:39:13 -04001544static int update_open_stateid(struct nfs4_state *state,
1545 const nfs4_stateid *open_stateid,
1546 const nfs4_stateid *delegation,
1547 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001548{
Trond Myklebust1393d962016-09-22 13:39:13 -04001549 struct nfs_server *server = NFS_SERVER(state->inode);
1550 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001551 struct nfs_inode *nfsi = NFS_I(state->inode);
1552 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001553 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001554 int ret = 0;
1555
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001557
1558 rcu_read_lock();
1559 deleg_cur = rcu_dereference(nfsi->delegation);
1560 if (deleg_cur == NULL)
1561 goto no_delegation;
1562
1563 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001564 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001565 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001566 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001567 goto no_delegation_unlock;
1568
1569 if (delegation == NULL)
1570 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001571 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001572 goto no_delegation_unlock;
1573
Trond Myklebustb7391f42008-12-23 15:21:52 -05001574 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001575 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1576 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001577 ret = 1;
1578no_delegation_unlock:
1579 spin_unlock(&deleg_cur->lock);
1580no_delegation:
1581 rcu_read_unlock();
1582
1583 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001584 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001585 ret = 1;
1586 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001587 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001588 nfs4_schedule_state_manager(clp);
1589 if (freeme.type != 0)
1590 nfs4_test_and_free_stateid(server, &freeme,
1591 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001592
1593 return ret;
1594}
1595
Trond Myklebust39071e62015-01-24 15:07:56 -05001596static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1597 const nfs4_stateid *stateid)
1598{
1599 struct nfs4_state *state = lsp->ls_state;
1600 bool ret = false;
1601
1602 spin_lock(&state->state_lock);
1603 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1604 goto out_noupdate;
1605 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1606 goto out_noupdate;
1607 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1608 ret = true;
1609out_noupdate:
1610 spin_unlock(&state->state_lock);
1611 return ret;
1612}
Trond Myklebust34310432008-12-23 15:21:38 -05001613
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001614static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001615{
1616 struct nfs_delegation *delegation;
1617
1618 rcu_read_lock();
1619 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001620 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001621 rcu_read_unlock();
1622 return;
1623 }
1624 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001625 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001626}
1627
Trond Myklebust6ee41262007-07-08 14:11:36 -04001628static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001629{
1630 struct nfs4_state *state = opendata->state;
1631 struct nfs_inode *nfsi = NFS_I(state->inode);
1632 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001633 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001635 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636 nfs4_stateid stateid;
1637 int ret = -EAGAIN;
1638
Trond Myklebustaac00a82007-07-05 19:02:21 -04001639 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001640 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001641 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001642 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001643 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001644 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001645 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001646 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001647 rcu_read_lock();
1648 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001649 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001650 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001651 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001652 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001654 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001656 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001657 if (!opendata->is_recover) {
1658 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1659 if (ret != 0)
1660 goto out;
1661 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001662 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001663
1664 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001665 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001666 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001667 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668out:
1669 return ERR_PTR(ret);
1670out_return_state:
1671 atomic_inc(&state->count);
1672 return state;
1673}
1674
Andy Adamsone23008e2012-10-02 21:07:32 -04001675static void
1676nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1677{
1678 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1679 struct nfs_delegation *delegation;
1680 int delegation_flags = 0;
1681
1682 rcu_read_lock();
1683 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1684 if (delegation)
1685 delegation_flags = delegation->flags;
1686 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001687 switch (data->o_arg.claim) {
1688 default:
1689 break;
1690 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1691 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001692 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1693 "returning a delegation for "
1694 "OPEN(CLAIM_DELEGATE_CUR)\n",
1695 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001696 return;
1697 }
1698 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001699 nfs_inode_set_delegation(state->inode,
1700 data->owner->so_cred,
1701 &data->o_res);
1702 else
1703 nfs_inode_reclaim_delegation(state->inode,
1704 data->owner->so_cred,
1705 &data->o_res);
1706}
1707
1708/*
1709 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1710 * and update the nfs4_state.
1711 */
1712static struct nfs4_state *
1713_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1714{
1715 struct inode *inode = data->state->inode;
1716 struct nfs4_state *state = data->state;
1717 int ret;
1718
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001719 if (!data->rpc_done) {
1720 if (data->rpc_status) {
1721 ret = data->rpc_status;
1722 goto err;
1723 }
1724 /* cached opens have already been processed */
1725 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001726 }
1727
Andy Adamsone23008e2012-10-02 21:07:32 -04001728 ret = nfs_refresh_inode(inode, &data->f_attr);
1729 if (ret)
1730 goto err;
1731
1732 if (data->o_res.delegation_type != 0)
1733 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001734update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001735 update_open_stateid(state, &data->o_res.stateid, NULL,
1736 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001737 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001738
1739 return state;
1740err:
1741 return ERR_PTR(ret);
1742
1743}
1744
1745static struct nfs4_state *
1746_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747{
1748 struct inode *inode;
1749 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001750 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001751
Trond Myklebustaac00a82007-07-05 19:02:21 -04001752 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001753 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001754 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001755 goto out;
1756 }
1757
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001758 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001761 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001762 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001763 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 goto err;
1765 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 state = nfs4_get_open_state(inode, data->owner);
1767 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001768 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001769 if (data->o_res.delegation_type != 0)
1770 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001771 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001772 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001774out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001775 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001777err_put_inode:
1778 iput(inode);
1779err:
1780 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781}
1782
Andy Adamsone23008e2012-10-02 21:07:32 -04001783static struct nfs4_state *
1784nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1785{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001786 struct nfs4_state *ret;
1787
Andy Adamsone23008e2012-10-02 21:07:32 -04001788 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001789 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1790 else
1791 ret = _nfs4_opendata_to_nfs4_state(data);
1792 nfs4_sequence_free_slot(&data->o_res.seq_res);
1793 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001794}
1795
Trond Myklebust864472e2006-01-03 09:55:15 +01001796static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1797{
1798 struct nfs_inode *nfsi = NFS_I(state->inode);
1799 struct nfs_open_context *ctx;
1800
1801 spin_lock(&state->inode->i_lock);
1802 list_for_each_entry(ctx, &nfsi->open_files, list) {
1803 if (ctx->state != state)
1804 continue;
1805 get_nfs_open_context(ctx);
1806 spin_unlock(&state->inode->i_lock);
1807 return ctx;
1808 }
1809 spin_unlock(&state->inode->i_lock);
1810 return ERR_PTR(-ENOENT);
1811}
1812
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001813static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1814 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001815{
1816 struct nfs4_opendata *opendata;
1817
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001818 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001819 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001820 if (opendata == NULL)
1821 return ERR_PTR(-ENOMEM);
1822 opendata->state = state;
1823 atomic_inc(&state->count);
1824 return opendata;
1825}
1826
Trond Myklebust24311f82015-09-20 10:50:17 -04001827static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1828 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001829{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001830 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001831 int ret;
1832
Trond Myklebust24311f82015-09-20 10:50:17 -04001833 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001834 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 opendata->o_arg.open_flags = 0;
1836 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001837 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1838 NFS_SB(opendata->dentry->d_sb),
1839 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001840 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1841 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1842 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001843 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001844 if (ret != 0)
1845 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001846 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001847 if (IS_ERR(newstate))
1848 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001849 if (newstate != opendata->state)
1850 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001851 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001852 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001853}
1854
1855static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1856{
Trond Myklebust864472e2006-01-03 09:55:15 +01001857 int ret;
1858
Trond Myklebust4f14c192014-02-12 19:15:06 -05001859 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1860 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1861 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1862 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001864 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001865 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001866 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001867 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1868 if (ret != 0)
1869 return ret;
1870 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1871 if (ret != 0)
1872 return ret;
1873 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1874 if (ret != 0)
1875 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001876 /*
1877 * We may have performed cached opens for all three recoveries.
1878 * Check if we need to update the current stateid.
1879 */
1880 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001881 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001882 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001883 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001884 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001885 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001886 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001887 return 0;
1888}
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/*
1891 * OPEN_RECLAIM:
1892 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001894static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001896 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001897 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001898 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 int status;
1900
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001901 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1902 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001903 if (IS_ERR(opendata))
1904 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001905 rcu_read_lock();
1906 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001907 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001908 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001909 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001910 opendata->o_arg.u.delegation_type = delegation_type;
1911 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001912 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return status;
1914}
1915
Trond Myklebust539cd032007-06-05 11:46:42 -04001916static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 struct nfs_server *server = NFS_SERVER(state->inode);
1919 struct nfs4_exception exception = { };
1920 int err;
1921 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001922 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001923 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001924 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1925 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001926 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001927 break;
1928 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 } while (exception.retry);
1930 return err;
1931}
1932
Trond Myklebust864472e2006-01-03 09:55:15 +01001933static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1934{
1935 struct nfs_open_context *ctx;
1936 int ret;
1937
1938 ctx = nfs4_state_find_open_context(state);
1939 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001940 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001941 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 put_nfs_open_context(ctx);
1943 return ret;
1944}
1945
NeilBrown70f254a2017-12-13 09:57:09 +11001946static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001948 switch (err) {
1949 default:
1950 printk(KERN_ERR "NFS: %s: unhandled error "
1951 "%d.\n", __func__, err);
1952 case 0:
1953 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001954 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001955 case -ESTALE:
1956 break;
1957 case -NFS4ERR_BADSESSION:
1958 case -NFS4ERR_BADSLOT:
1959 case -NFS4ERR_BAD_HIGH_SLOT:
1960 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1961 case -NFS4ERR_DEADSESSION:
1962 set_bit(NFS_DELEGATED_STATE, &state->flags);
1963 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1964 return -EAGAIN;
1965 case -NFS4ERR_STALE_CLIENTID:
1966 case -NFS4ERR_STALE_STATEID:
1967 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001968 /* Don't recall a delegation if it was lost */
1969 nfs4_schedule_lease_recovery(server->nfs_client);
1970 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001971 case -NFS4ERR_MOVED:
1972 nfs4_schedule_migration_recovery(server);
1973 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001974 case -NFS4ERR_LEASE_MOVED:
1975 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1976 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001977 case -NFS4ERR_DELEG_REVOKED:
1978 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001979 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001980 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001981 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001982 nfs_inode_find_state_and_recover(state->inode,
1983 stateid);
1984 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001985 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001986 case -NFS4ERR_DELAY:
1987 case -NFS4ERR_GRACE:
1988 set_bit(NFS_DELEGATED_STATE, &state->flags);
1989 ssleep(1);
1990 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001991 case -ENOMEM:
1992 case -NFS4ERR_DENIED:
NeilBrown70f254a2017-12-13 09:57:09 +11001993 if (fl) {
1994 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
1995 if (lsp)
1996 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1997 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001998 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return err;
2001}
2002
Trond Myklebust24311f82015-09-20 10:50:17 -04002003int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2004 struct nfs4_state *state, const nfs4_stateid *stateid,
2005 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002006{
2007 struct nfs_server *server = NFS_SERVER(state->inode);
2008 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002009 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002010
2011 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2012 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2013 if (IS_ERR(opendata))
2014 return PTR_ERR(opendata);
2015 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002016 write_seqlock(&state->seqlock);
2017 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2018 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002019 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2020 switch (type & (FMODE_READ|FMODE_WRITE)) {
2021 case FMODE_READ|FMODE_WRITE:
2022 case FMODE_WRITE:
2023 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2024 if (err)
2025 break;
2026 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2027 if (err)
2028 break;
2029 case FMODE_READ:
2030 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2031 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002032 nfs4_opendata_put(opendata);
NeilBrown70f254a2017-12-13 09:57:09 +11002033 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002034}
2035
Chuck Leverbe05c862013-08-09 12:49:47 -04002036static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2037{
2038 struct nfs4_opendata *data = calldata;
2039
Peng Taocb04ad22014-06-11 05:24:15 +08002040 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2041 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002042}
2043
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002044static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2045{
2046 struct nfs4_opendata *data = calldata;
2047
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002048 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002049
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002050 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002051 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002052 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002053 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002054 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002055 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002056 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002057}
2058
2059static void nfs4_open_confirm_release(void *calldata)
2060{
2061 struct nfs4_opendata *data = calldata;
2062 struct nfs4_state *state = NULL;
2063
2064 /* If this request hasn't been cancelled, do nothing */
2065 if (data->cancelled == 0)
2066 goto out_free;
2067 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002068 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002069 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002070 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002071 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002072 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002074 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002075}
2076
2077static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002078 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002079 .rpc_call_done = nfs4_open_confirm_done,
2080 .rpc_release = nfs4_open_confirm_release,
2081};
2082
2083/*
2084 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2085 */
2086static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2087{
David Howells2b0143b2015-03-17 22:25:59 +00002088 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002089 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002090 struct rpc_message msg = {
2091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2092 .rpc_argp = &data->c_arg,
2093 .rpc_resp = &data->c_res,
2094 .rpc_cred = data->owner->so_cred,
2095 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002096 struct rpc_task_setup task_setup_data = {
2097 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002098 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002099 .callback_ops = &nfs4_open_confirm_ops,
2100 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002101 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002102 .flags = RPC_TASK_ASYNC,
2103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int status;
2105
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002106 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002107 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002108 data->rpc_done = 0;
2109 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002110 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002111 if (data->is_recover)
2112 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002113 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002114 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002115 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002116 status = nfs4_wait_for_completion_rpc_task(task);
2117 if (status != 0) {
2118 data->cancelled = 1;
2119 smp_wmb();
2120 } else
2121 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002122 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return status;
2124}
2125
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002126static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002128 struct nfs4_opendata *data = calldata;
2129 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002130 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002131 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002134 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002135 /*
2136 * Check if we still need to send an OPEN call, or if we can use
2137 * a delegation instead.
2138 */
2139 if (data->state != NULL) {
2140 struct nfs_delegation *delegation;
2141
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002142 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002143 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002144 rcu_read_lock();
2145 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002146 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002147 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002148 rcu_read_unlock();
2149 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002150 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002151 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002152 switch (claim) {
2153 default:
2154 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002155 case NFS4_OPEN_CLAIM_PREVIOUS:
2156 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2157 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002158 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002159 case NFS4_OPEN_CLAIM_FH:
2160 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002161 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2162 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002163 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002164 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002165 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002166 &data->o_res.seq_res,
2167 task) != 0)
2168 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002169
2170 /* Set the create mode (note dependency on the session type) */
2171 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2172 if (data->o_arg.open_flags & O_EXCL) {
2173 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2174 if (nfs4_has_persistent_session(clp))
2175 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2176 else if (clp->cl_mvops->minor_version > 0)
2177 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2178 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002179 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002180unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002181 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002182 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002183out_no_action:
2184 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002185out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002186 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002187}
2188
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189static void nfs4_open_done(struct rpc_task *task, void *calldata)
2190{
2191 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002193 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002194
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002195 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002196 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002197
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002199 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2200 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002201 case S_IFREG:
2202 break;
2203 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002205 break;
2206 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002208 break;
2209 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002211 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002212 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002213 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002214 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2215 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002216 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002217 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002219
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002220static void nfs4_open_release(void *calldata)
2221{
2222 struct nfs4_opendata *data = calldata;
2223 struct nfs4_state *state = NULL;
2224
2225 /* If this request hasn't been cancelled, do nothing */
2226 if (data->cancelled == 0)
2227 goto out_free;
2228 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002229 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002230 goto out_free;
2231 /* In case we need an open_confirm, no cleanup! */
2232 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2233 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002234 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002235 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002236 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002238 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239}
2240
2241static const struct rpc_call_ops nfs4_open_ops = {
2242 .rpc_call_prepare = nfs4_open_prepare,
2243 .rpc_call_done = nfs4_open_done,
2244 .rpc_release = nfs4_open_release,
2245};
2246
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002247static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248{
David Howells2b0143b2015-03-17 22:25:59 +00002249 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002250 struct nfs_server *server = NFS_SERVER(dir);
2251 struct nfs_openargs *o_arg = &data->o_arg;
2252 struct nfs_openres *o_res = &data->o_res;
2253 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002254 struct rpc_message msg = {
2255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2256 .rpc_argp = o_arg,
2257 .rpc_resp = o_res,
2258 .rpc_cred = data->owner->so_cred,
2259 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002260 struct rpc_task_setup task_setup_data = {
2261 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002262 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 .callback_ops = &nfs4_open_ops,
2264 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002265 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002266 .flags = RPC_TASK_ASYNC,
2267 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002268 int status;
2269
Chuck Levera9c92d62013-08-09 12:48:18 -04002270 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002271 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002272 data->rpc_done = 0;
2273 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002274 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002275 data->is_recover = 0;
2276 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002277 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002278 data->is_recover = 1;
2279 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002280 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002281 if (IS_ERR(task))
2282 return PTR_ERR(task);
2283 status = nfs4_wait_for_completion_rpc_task(task);
2284 if (status != 0) {
2285 data->cancelled = 1;
2286 smp_wmb();
2287 } else
2288 status = data->rpc_status;
2289 rpc_put_task(task);
2290
2291 return status;
2292}
2293
2294static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2295{
David Howells2b0143b2015-03-17 22:25:59 +00002296 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002297 struct nfs_openres *o_res = &data->o_res;
2298 int status;
2299
2300 status = nfs4_run_open_task(data, 1);
2301 if (status != 0 || !data->rpc_done)
2302 return status;
2303
Trond Myklebust6926afd2012-01-07 13:22:46 -05002304 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2305
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002306 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2307 status = _nfs4_proc_open_confirm(data);
2308 if (status != 0)
2309 return status;
2310 }
2311
2312 return status;
2313}
2314
Trond Myklebustf3792d62014-07-10 08:54:32 -04002315/*
2316 * Additional permission checks in order to distinguish between an
2317 * open for read, and an open for execute. This works around the
2318 * fact that NFSv4 OPEN treats read and execute permissions as being
2319 * the same.
2320 * Note that in the non-execute case, we want to turn off permission
2321 * checking if we just created a new file (POSIX open() semantics).
2322 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002323static int nfs4_opendata_access(struct rpc_cred *cred,
2324 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002325 struct nfs4_state *state, fmode_t fmode,
2326 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002327{
2328 struct nfs_access_entry cache;
2329 u32 mask;
2330
2331 /* access call failed or for some reason the server doesn't
2332 * support any access modes -- defer access call until later */
2333 if (opendata->o_res.access_supported == 0)
2334 return 0;
2335
2336 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002337 /*
2338 * Use openflags to check for exec, because fmode won't
2339 * always have FMODE_EXEC set when file open for exec.
2340 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002341 if (openflags & __FMODE_EXEC) {
2342 /* ONLY check for exec rights */
2343 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002344 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002345 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002346
2347 cache.cred = cred;
2348 cache.jiffies = jiffies;
2349 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2350 nfs_access_add_cache(state->inode, &cache);
2351
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002352 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002353 return 0;
2354
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002355 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002356}
2357
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002358/*
2359 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2360 */
2361static int _nfs4_proc_open(struct nfs4_opendata *data)
2362{
David Howells2b0143b2015-03-17 22:25:59 +00002363 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002364 struct nfs_server *server = NFS_SERVER(dir);
2365 struct nfs_openargs *o_arg = &data->o_arg;
2366 struct nfs_openres *o_res = &data->o_res;
2367 int status;
2368
2369 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002370 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002371 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002372 if (status != 0) {
2373 if (status == -NFS4ERR_BADNAME &&
2374 !(o_arg->open_flags & O_CREAT))
2375 return -ENOENT;
2376 return status;
2377 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002378
Trond Myklebust6926afd2012-01-07 13:22:46 -05002379 nfs_fattr_map_and_free_names(server, &data->f_attr);
2380
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002381 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002382 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002383 if (o_arg->open_flags & O_EXCL)
2384 data->file_created = 1;
2385 else if (o_res->cinfo.before != o_res->cinfo.after)
2386 data->file_created = 1;
2387 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002388 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2389 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002391 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002393 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002395 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2396 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002397 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002398 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Andy Adamsond83217c2011-03-01 01:34:17 +00002402static int nfs4_recover_expired_lease(struct nfs_server *server)
2403{
2404 return nfs4_client_recover_expired_lease(server->nfs_client);
2405}
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407/*
2408 * OPEN_EXPIRED:
2409 * reclaim state on the server after a network partition.
2410 * Assumes caller holds the appropriate lock
2411 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002412static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002414 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002415 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002417 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002418 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002419 if (IS_ERR(opendata))
2420 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002421 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002422 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002423 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002424 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002425 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002428static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002429{
Trond Myklebust539cd032007-06-05 11:46:42 -04002430 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002431 struct nfs4_exception exception = { };
2432 int err;
2433
2434 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002435 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002436 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002437 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2438 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002439 switch (err) {
2440 default:
2441 goto out;
2442 case -NFS4ERR_GRACE:
2443 case -NFS4ERR_DELAY:
2444 nfs4_handle_exception(server, err, &exception);
2445 err = 0;
2446 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002447 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002448out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002449 return err;
2450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002455 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Trond Myklebust864472e2006-01-03 09:55:15 +01002457 ctx = nfs4_state_find_open_context(state);
2458 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002459 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002460 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002461 put_nfs_open_context(ctx);
2462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
Trond Myklebust41020b62016-09-22 13:38:58 -04002465static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2466 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002467{
Trond Myklebust41020b62016-09-22 13:38:58 -04002468 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002469 write_seqlock(&state->seqlock);
2470 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2471 write_sequnlock(&state->seqlock);
2472 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2473}
2474
2475static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2476{
2477 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002478 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002479}
2480
2481static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2482{
2483 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2484 nfs40_clear_delegation_stateid(state);
2485 return nfs4_open_expired(sp, state);
2486}
2487
Trond Myklebust45870d62016-09-22 13:38:59 -04002488static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2489 nfs4_stateid *stateid,
2490 struct rpc_cred *cred)
2491{
2492 return -NFS4ERR_BAD_STATEID;
2493}
2494
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002495#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002496static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2497 nfs4_stateid *stateid,
2498 struct rpc_cred *cred)
2499{
2500 int status;
2501
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002502 switch (stateid->type) {
2503 default:
2504 break;
2505 case NFS4_INVALID_STATEID_TYPE:
2506 case NFS4_SPECIAL_STATEID_TYPE:
2507 return -NFS4ERR_BAD_STATEID;
2508 case NFS4_REVOKED_STATEID_TYPE:
2509 goto out_free;
2510 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002511
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002512 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002513 switch (status) {
2514 case -NFS4ERR_EXPIRED:
2515 case -NFS4ERR_ADMIN_REVOKED:
2516 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002517 break;
2518 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002519 return status;
2520 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002521out_free:
2522 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002523 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002524 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002525}
2526
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002527static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002528{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002529 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002530 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002531 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002532 struct rpc_cred *cred;
2533 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002534
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002535 /* Get the delegation credential for use by test/free_stateid */
2536 rcu_read_lock();
2537 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002538 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002539 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540 return;
2541 }
2542
2543 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaf412412018-09-05 14:07:14 -04002544 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002545 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002546 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002547 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002548 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002549
Trond Myklebustaf412412018-09-05 14:07:14 -04002550 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2551 &delegation->flags)) {
2552 rcu_read_unlock();
2553 return;
2554 }
2555
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002556 cred = get_rpccred(delegation->cred);
2557 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002558 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002559 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002560 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002561 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002562
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002563 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002564}
2565
2566/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002567 * nfs41_check_expired_locks - possibly free a lock stateid
2568 *
2569 * @state: NFSv4 state for an inode
2570 *
2571 * Returns NFS_OK if recovery for this stateid is now finished.
2572 * Otherwise a negative NFS4ERR value is returned.
2573 */
2574static int nfs41_check_expired_locks(struct nfs4_state *state)
2575{
2576 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002577 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002578 struct nfs_server *server = NFS_SERVER(state->inode);
2579
2580 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2581 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002582
2583 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002584 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2585 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2586 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2587
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002588 atomic_inc(&lsp->ls_count);
2589 spin_unlock(&state->state_lock);
2590
2591 nfs4_put_lock_state(prev);
2592 prev = lsp;
2593
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002594 status = nfs41_test_and_free_expired_stateid(server,
2595 &lsp->ls_stateid,
2596 cred);
2597 trace_nfs4_test_lock_stateid(state, lsp, status);
2598 if (status == -NFS4ERR_EXPIRED ||
2599 status == -NFS4ERR_BAD_STATEID) {
2600 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002601 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002602 if (!recover_lost_locks)
2603 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2604 } else if (status != NFS_OK) {
2605 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002606 nfs4_put_lock_state(prev);
2607 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002608 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002609 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002610 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002611 }
2612 spin_unlock(&state->state_lock);
2613 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002614out:
2615 return ret;
2616}
2617
2618/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002619 * nfs41_check_open_stateid - possibly free an open stateid
2620 *
2621 * @state: NFSv4 state for an inode
2622 *
2623 * Returns NFS_OK if recovery for this stateid is now finished.
2624 * Otherwise a negative NFS4ERR value is returned.
2625 */
2626static int nfs41_check_open_stateid(struct nfs4_state *state)
2627{
2628 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002629 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002630 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002631 int status;
2632
Trond Myklebustb134fc42016-09-22 13:39:16 -04002633 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2635 if (nfs4_have_delegation(state->inode, state->state))
2636 return NFS_OK;
2637 return -NFS4ERR_OPENMODE;
2638 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002639 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002640 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002641 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002642 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002643 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002644 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2645 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2646 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002647 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002648 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002649 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002650 if (status != NFS_OK)
2651 return status;
2652 if (nfs_open_stateid_recover_openmode(state))
2653 return -NFS4ERR_OPENMODE;
2654 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002655}
2656
2657static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2658{
Chuck Levereb64cf92012-07-11 16:30:05 -04002659 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002660
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002661 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002662 status = nfs41_check_expired_locks(state);
2663 if (status != NFS_OK)
2664 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002665 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002666 if (status != NFS_OK)
2667 status = nfs4_open_expired(sp, state);
2668 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002669}
2670#endif
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002673 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2674 * fields corresponding to attributes that were used to store the verifier.
2675 * Make sure we clobber those fields in the later setattr call
2676 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002677static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2678 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002679{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002680 const u32 *attrset = opendata->o_res.attrset;
2681
2682 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002683 !(sattr->ia_valid & ATTR_ATIME_SET))
2684 sattr->ia_valid |= ATTR_ATIME;
2685
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002686 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002687 !(sattr->ia_valid & ATTR_MTIME_SET))
2688 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002689
2690 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddington56379492017-01-24 11:34:20 -05002691 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2692 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002693 sattr->ia_valid &= ~ATTR_MODE;
2694
2695 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2696 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002697}
2698
Trond Myklebustc21443c2013-02-07 14:26:21 -05002699static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2700 fmode_t fmode,
2701 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002702 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002703{
2704 struct nfs4_state_owner *sp = opendata->owner;
2705 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002706 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002707 struct nfs4_state *state;
2708 unsigned int seq;
2709 int ret;
2710
2711 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2712
2713 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002714 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715 goto out;
2716
Trond Myklebustb05c4602018-05-22 11:17:16 -04002717 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002718 ret = PTR_ERR(state);
2719 if (IS_ERR(state))
2720 goto out;
Trond Myklebust04653392017-02-08 11:29:46 -05002721 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002722 if (server->caps & NFS_CAP_POSIX_LOCK)
2723 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002724 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2725 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002726
Trond Myklebust275bb302013-05-29 13:11:28 -04002727 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002728 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002729 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002730 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002731 alias = d_exact_alias(dentry, state->inode);
2732 if (!alias)
2733 alias = d_splice_alias(igrab(state->inode), dentry);
2734 /* d_splice_alias() can't fail here - it's a non-directory */
2735 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002736 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002737 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002738 }
2739 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002740 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002741 }
2742
Trond Myklebustc21443c2013-02-07 14:26:21 -05002743 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2744 if (ret != 0)
2745 goto out;
2746
David Howells2b0143b2015-03-17 22:25:59 +00002747 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002748 nfs_inode_attach_open_context(ctx);
2749 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2750 nfs4_schedule_stateid_recovery(server, state);
2751 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002752out:
Olga Kornievskaia22a87112019-03-19 12:12:13 -04002753 if (!opendata->cancelled)
2754 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002755 return ret;
2756}
2757
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002758/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002759 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 */
Andy Adamson82be4172012-05-23 05:02:35 -04002761static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002762 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002763 int flags,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002764 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002765 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 struct nfs4_state_owner *sp;
2768 struct nfs4_state *state = NULL;
2769 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002770 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002771 struct dentry *dentry = ctx->dentry;
2772 struct rpc_cred *cred = ctx->cred;
2773 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2774 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002775 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002776 struct iattr *sattr = c->sattr;
2777 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002778 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002779 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002783 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2784 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2786 goto out_err;
2787 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002788 status = nfs4_recover_expired_lease(server);
2789 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002790 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002791 if (d_really_is_positive(dentry))
2792 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002793 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002794 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002795 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002796 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2797 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002798 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002799 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
David Quigley14c43f72013-05-22 12:50:43 -04002801 if (label) {
2802 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2803 if (IS_ERR(olabel)) {
2804 status = PTR_ERR(olabel);
2805 goto err_opendata_put;
2806 }
2807 }
2808
Trond Myklebuste911b812014-03-26 13:24:37 -07002809 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2810 if (!opendata->f_attr.mdsthreshold) {
2811 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2812 if (!opendata->f_attr.mdsthreshold)
2813 goto err_free_label;
2814 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002815 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002816 }
David Howells2b0143b2015-03-17 22:25:59 +00002817 if (d_really_is_positive(dentry))
2818 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002819
Trond Myklebust3efb9722013-05-29 13:17:04 -04002820 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002821 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002822 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002823 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002824
NeilBrownefcbc042015-07-30 13:00:56 +10002825 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002826 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002827 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002828 /*
2829 * send create attributes which was not set by open
2830 * with an extra setattr.
2831 */
2832 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2833 nfs_fattr_init(opendata->o_res.f_attr);
2834 status = nfs4_do_setattr(state->inode, cred,
2835 opendata->o_res.f_attr, sattr,
2836 state, label, olabel);
2837 if (status == 0) {
2838 nfs_setattr_update_inode(state->inode, sattr,
2839 opendata->o_res.f_attr);
2840 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2841 }
David Quigley1775fd32013-05-22 12:50:42 -04002842 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002843 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002844 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002845 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002846
Trond Myklebuste911b812014-03-26 13:24:37 -07002847 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002848 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002849 opendata->f_attr.mdsthreshold = NULL;
2850 }
Andy Adamson82be4172012-05-23 05:02:35 -04002851
David Quigley14c43f72013-05-22 12:50:43 -04002852 nfs4_label_free(olabel);
2853
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002854 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002857err_free_label:
2858 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002859err_opendata_put:
2860 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002861err_put_state_owner:
2862 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return status;
2865}
2866
2867
Andy Adamson82be4172012-05-23 05:02:35 -04002868static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002869 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002870 int flags,
2871 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002872 struct nfs4_label *label,
2873 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002875 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 struct nfs4_exception exception = { };
2877 struct nfs4_state *res;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002878 struct nfs4_open_createattrs c = {
2879 .label = label,
2880 .sattr = sattr,
2881 .verf = {
2882 [0] = (__u32)jiffies,
2883 [1] = (__u32)current->pid,
2884 },
2885 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 int status;
2887
2888 do {
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002889 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002890 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002891 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (status == 0)
2893 break;
2894 /* NOTE: BAD_SEQID means the server and client disagree about the
2895 * book-keeping w.r.t. state-changing operations
2896 * (OPEN/CLOSE/LOCK/LOCKU...)
2897 * It is actually a sign of a bug on the client or on the server.
2898 *
2899 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002900 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 * have unhashed the old state_owner for us, and that we can
2902 * therefore safely retry using a new one. We should still warn
2903 * the user though...
2904 */
2905 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002906 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002907 " returned a bad sequence-id error!\n",
2908 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 exception.retry = 1;
2910 continue;
2911 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002912 /*
2913 * BAD_STATEID on OPEN means that the server cancelled our
2914 * state before it received the OPEN_CONFIRM.
2915 * Recover by retrying the request as per the discussion
2916 * on Page 181 of RFC3530.
2917 */
2918 if (status == -NFS4ERR_BAD_STATEID) {
2919 exception.retry = 1;
2920 continue;
2921 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002922 if (status == -EAGAIN) {
2923 /* We must have found a delegation */
2924 exception.retry = 1;
2925 continue;
2926 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002927 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2928 continue;
2929 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 status, &exception));
2931 } while (exception.retry);
2932 return res;
2933}
2934
Trond Myklebust8487c472016-06-26 08:44:35 -04002935static int _nfs4_do_setattr(struct inode *inode,
2936 struct nfs_setattrargs *arg,
2937 struct nfs_setattrres *res,
2938 struct rpc_cred *cred,
2939 struct nfs4_state *state)
2940{
2941 struct nfs_server *server = NFS_SERVER(inode);
2942 struct rpc_message msg = {
2943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2944 .rpc_argp = arg,
2945 .rpc_resp = res,
2946 .rpc_cred = cred,
2947 };
2948 struct rpc_cred *delegation_cred = NULL;
2949 unsigned long timestamp = jiffies;
2950 fmode_t fmode;
2951 bool truncate;
2952 int status;
2953
2954 nfs_fattr_init(res->fattr);
2955
2956 /* Servers should only apply open mode checks for file size changes */
2957 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2958 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2959
2960 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2961 /* Use that stateid */
2962 } else if (truncate && state != NULL) {
2963 struct nfs_lockowner lockowner = {
2964 .l_owner = current->files,
2965 .l_pid = current->tgid,
2966 };
2967 if (!nfs4_valid_open_stateid(state))
2968 return -EBADF;
2969 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2970 &arg->stateid, &delegation_cred) == -EIO)
2971 return -EBADF;
2972 } else
2973 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2974 if (delegation_cred)
2975 msg.rpc_cred = delegation_cred;
2976
2977 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2978
2979 put_rpccred(delegation_cred);
2980 if (status == 0 && state != NULL)
2981 renew_lease(server, timestamp);
2982 trace_nfs4_setattr(inode, &arg->stateid, status);
2983 return status;
2984}
2985
2986static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2987 struct nfs_fattr *fattr, struct iattr *sattr,
2988 struct nfs4_state *state, struct nfs4_label *ilabel,
2989 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002991 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002993 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .iap = sattr,
2995 .server = server,
2996 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002997 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
2999 struct nfs_setattrres res = {
3000 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003001 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 .server = server,
3003 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003004 struct nfs4_exception exception = {
3005 .state = state,
3006 .inode = inode,
3007 .stateid = &arg.stateid,
3008 };
3009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
David Quigleyaa9c2662013-05-22 12:50:44 -04003011 arg.bitmask = nfs4_bitmask(server, ilabel);
3012 if (ilabel)
3013 arg.bitmask = nfs4_bitmask(server, olabel);
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003016 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003017 switch (err) {
3018 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003019 if (!(sattr->ia_valid & ATTR_SIZE)) {
3020 pr_warn_once("NFSv4: server %s is incorrectly "
3021 "applying open mode checks to "
3022 "a SETATTR that is not "
3023 "changing file size.\n",
3024 server->nfs_client->cl_hostname);
3025 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003026 if (state && !(state->state & FMODE_WRITE)) {
3027 err = -EBADF;
3028 if (sattr->ia_valid & ATTR_OPEN)
3029 err = -EACCES;
3030 goto out;
3031 }
3032 }
3033 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003035out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return err;
3037}
3038
Peng Tao500d7012015-09-22 11:35:22 +08003039static bool
3040nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3041{
3042 if (inode == NULL || !nfs_have_layout(inode))
3043 return false;
3044
3045 return pnfs_wait_on_layoutreturn(inode, task);
3046}
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048struct nfs4_closedata {
3049 struct inode *inode;
3050 struct nfs4_state *state;
3051 struct nfs_closeargs arg;
3052 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003053 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003054 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003055 bool roc;
3056 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057};
3058
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003059static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003060{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003061 struct nfs4_closedata *calldata = data;
3062 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003063 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003064
Fred Isamanf7e89172011-01-06 11:36:32 +00003065 if (calldata->roc)
3066 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003067 nfs4_put_open_state(calldata->state);
3068 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003069 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003070 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003071 kfree(calldata);
3072}
3073
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003074static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003076 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003079 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Chuck Levera3ca5652012-03-01 17:00:40 -05003081 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003082 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3083 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003084 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 /* hmm. we are done with the inode, and in the process of freeing
3086 * the state_owner. we keep this around to process errors
3087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 switch (task->tk_status) {
3089 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003090 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003091 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003092 pnfs_roc_set_barrier(state->inode,
3093 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003094 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003095 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003096 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003098 case -NFS4ERR_EXPIRED:
3099 nfs4_free_revoked_stateid(server,
3100 &calldata->arg.stateid,
3101 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003102 case -NFS4ERR_OLD_STATEID:
3103 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003104 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003105 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003106 rpc_restart_call_prepare(task);
3107 goto out_release;
3108 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003109 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003112 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003113 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003114 goto out_release;
3115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003117 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3118 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003119out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003120 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003121 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003122 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003125static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003127 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003128 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003129 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003130 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003131 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003132
Chuck Levera3ca5652012-03-01 17:00:40 -05003133 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003134 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003135 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003136
Trond Myklebust88069f72009-12-08 08:33:16 -05003137 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003138 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003139 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3140 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3141 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003142 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003143 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003144 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003145 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003146 if (state->n_rdonly == 0)
3147 call_close |= is_rdonly;
3148 else if (is_rdonly)
3149 calldata->arg.fmode |= FMODE_READ;
3150 if (state->n_wronly == 0)
3151 call_close |= is_wronly;
3152 else if (is_wronly)
3153 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003154 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3155 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003156 } else if (is_rdwr)
3157 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3158
Trond Myklebust5cc78612016-11-14 11:19:56 -05003159 if (!nfs4_valid_open_stateid(state) ||
3160 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003161 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003162 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003163
3164 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003165 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003166 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003167 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003168
Peng Tao500d7012015-09-22 11:35:22 +08003169 if (nfs4_wait_on_layoutreturn(inode, task)) {
3170 nfs_release_seqid(calldata->arg.seqid);
3171 goto out_wait;
3172 }
3173
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003174 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003175 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003176 if (calldata->roc)
3177 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3178
Trond Myklebust6ae37332015-01-30 14:21:14 -05003179 calldata->arg.share_access =
3180 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3181 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003182
Trond Myklebust516a6af2005-10-27 22:12:41 -04003183 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003184 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003185 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003186 &calldata->arg.seq_args,
3187 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003188 task) != 0)
3189 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003190 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003191 return;
3192out_no_action:
3193 task->tk_action = NULL;
3194out_wait:
3195 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003198static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003199 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003200 .rpc_call_done = nfs4_close_done,
3201 .rpc_release = nfs4_free_closedata,
3202};
3203
Peng Taofe08c542014-07-03 13:05:01 +08003204static bool nfs4_roc(struct inode *inode)
3205{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003206 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003207 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003208 return pnfs_roc(inode);
3209}
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211/*
3212 * It is possible for data to be read/written from a mem-mapped file
3213 * after the sys_close call (which hits the vfs layer as a flush).
3214 * This means that we can't safely call nfsv4 close on a file until
3215 * the inode is cleared. This in turn means that we are not good
3216 * NFSv4 citizens - we do not indicate to the server to update the file's
3217 * share state even when we are done with one of the three share
3218 * stateid's in the inode.
3219 *
3220 * NOTE: Caller must be holding the sp->so_owner semaphore!
3221 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003222int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003224 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003225 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003227 struct nfs4_state_owner *sp = state->owner;
3228 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003229 struct rpc_message msg = {
3230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3231 .rpc_cred = state->owner->so_cred,
3232 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003233 struct rpc_task_setup task_setup_data = {
3234 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003235 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003236 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003237 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003238 .flags = RPC_TASK_ASYNC,
3239 };
Trond Myklebust95121352005-10-18 14:20:12 -07003240 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003242 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3243 &task_setup_data.rpc_client, &msg);
3244
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003245 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003247 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003248 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003249 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003251 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003253 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3254 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003255 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003256 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003257 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003258 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003259 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003260 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003261 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003262 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003263 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003264
Trond Myklebust1174dd12010-12-21 10:52:24 -05003265 msg.rpc_argp = &calldata->arg;
3266 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003267 task_setup_data.callback_data = calldata;
3268 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003269 if (IS_ERR(task))
3270 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003271 status = 0;
3272 if (wait)
3273 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003274 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003275 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003276out_free_calldata:
3277 kfree(calldata);
3278out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003279 nfs4_put_open_state(state);
3280 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
Trond Myklebust2b484292010-09-17 10:56:51 -04003284static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003285nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3286 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003289 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3290
3291 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Trond Myklebust565277f2007-10-15 18:17:53 -04003293 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003294 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003295
3296 nfs4_label_release_security(label);
3297
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003298 if (IS_ERR(state))
3299 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003300 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Trond Myklebust1185a552009-12-03 15:54:02 -05003303static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003304{
3305 if (ctx->state == NULL)
3306 return;
3307 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003308 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003309 else
Al Viro643168c2011-06-22 18:20:23 -04003310 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003311}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Trond Myklebustb944dba2013-11-04 15:20:20 -05003313#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3314#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003315#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3318{
Kinglong Mee8c612822015-08-26 21:12:58 +08003319 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003320 struct nfs4_server_caps_arg args = {
3321 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003322 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003323 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 struct nfs4_server_caps_res res = {};
3325 struct rpc_message msg = {
3326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003327 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 .rpc_resp = &res,
3329 };
3330 int status;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003331 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Kinglong Mee8c612822015-08-26 21:12:58 +08003333 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3334 FATTR4_WORD0_FH_EXPIRE_TYPE |
3335 FATTR4_WORD0_LINK_SUPPORT |
3336 FATTR4_WORD0_SYMLINK_SUPPORT |
3337 FATTR4_WORD0_ACLSUPPORT;
3338 if (minorversion)
3339 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3340
Bryan Schumaker7c513052011-03-24 17:12:24 +00003341 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003343 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003344 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003345 case 0:
3346 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3347 res.attr_bitmask[2] = 0;
3348 break;
3349 case 1:
3350 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3351 break;
3352 case 2:
3353 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003356 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3357 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3358 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3359 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003360 NFS_CAP_CTIME|NFS_CAP_MTIME|
3361 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003362 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3363 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 server->caps |= NFS_CAP_ACLS;
3365 if (res.has_links != 0)
3366 server->caps |= NFS_CAP_HARDLINKS;
3367 if (res.has_symlinks != 0)
3368 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003369 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3370 server->caps |= NFS_CAP_FILEID;
3371 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3372 server->caps |= NFS_CAP_MODE;
3373 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3374 server->caps |= NFS_CAP_NLINK;
3375 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3376 server->caps |= NFS_CAP_OWNER;
3377 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3378 server->caps |= NFS_CAP_OWNER_GROUP;
3379 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3380 server->caps |= NFS_CAP_ATIME;
3381 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3382 server->caps |= NFS_CAP_CTIME;
3383 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3384 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003385#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3386 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3387 server->caps |= NFS_CAP_SECURITY_LABEL;
3388#endif
3389 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3390 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003391 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003392
Trond Myklebusta65318b2009-03-11 14:10:28 -04003393 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3394 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3395 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003396 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003397
3398 /* Avoid a regression due to buggy server */
3399 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3400 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003401 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3402 sizeof(server->exclcreat_bitmask));
Trond Myklebustfca22d52017-05-09 15:47:15 -04003403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003405 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return status;
3409}
3410
Trond Myklebust55a97592006-06-09 09:34:19 -04003411int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412{
3413 struct nfs4_exception exception = { };
3414 int err;
3415 do {
3416 err = nfs4_handle_exception(server,
3417 _nfs4_server_capabilities(server, fhandle),
3418 &exception);
3419 } while (exception.retry);
3420 return err;
3421}
3422
3423static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3424 struct nfs_fsinfo *info)
3425{
David Quigleyaa9c2662013-05-22 12:50:44 -04003426 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003428 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 };
3430 struct nfs4_lookup_res res = {
3431 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003432 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 .fh = fhandle,
3434 };
3435 struct rpc_message msg = {
3436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3437 .rpc_argp = &args,
3438 .rpc_resp = &res,
3439 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003440
David Quigleyaa9c2662013-05-22 12:50:44 -04003441 bitmask[0] = nfs4_fattr_bitmap[0];
3442 bitmask[1] = nfs4_fattr_bitmap[1];
3443 /*
3444 * Process the label in the upcoming getfattr
3445 */
3446 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3447
Trond Myklebust0e574af2005-10-27 22:12:38 -04003448 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003449 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450}
3451
3452static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3453 struct nfs_fsinfo *info)
3454{
3455 struct nfs4_exception exception = { };
3456 int err;
3457 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003458 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003459 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003460 switch (err) {
3461 case 0:
3462 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003463 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003464 default:
3465 err = nfs4_handle_exception(server, err, &exception);
3466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return err;
3470}
3471
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003472static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3473 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3474{
Trond Myklebustc2190662013-08-26 19:23:04 -04003475 struct rpc_auth_create_args auth_args = {
3476 .pseudoflavor = flavor,
3477 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003478 struct rpc_auth *auth;
3479 int ret;
3480
Trond Myklebustc2190662013-08-26 19:23:04 -04003481 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003482 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003483 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003484 goto out;
3485 }
3486 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003487out:
3488 return ret;
3489}
3490
Chuck Lever9a744ba2013-03-16 15:56:02 -04003491/*
3492 * Retry pseudoroot lookup with various security flavors. We do this when:
3493 *
3494 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3495 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3496 *
3497 * Returns zero on success, or a negative NFS4ERR value, or a
3498 * negative errno value.
3499 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003500static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003501 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003503 /* Per 3530bis 15.33.5 */
3504 static const rpc_authflavor_t flav_array[] = {
3505 RPC_AUTH_GSS_KRB5P,
3506 RPC_AUTH_GSS_KRB5I,
3507 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003508 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003509 RPC_AUTH_NULL,
3510 };
3511 int status = -EPERM;
3512 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003514 if (server->auth_info.flavor_len > 0) {
3515 /* try each flavor specified by user */
3516 for (i = 0; i < server->auth_info.flavor_len; i++) {
3517 status = nfs4_lookup_root_sec(server, fhandle, info,
3518 server->auth_info.flavors[i]);
3519 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3520 continue;
3521 break;
3522 }
3523 } else {
3524 /* no flavors specified by user, try default list */
3525 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3526 status = nfs4_lookup_root_sec(server, fhandle, info,
3527 flav_array[i]);
3528 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3529 continue;
3530 break;
3531 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003532 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003533
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003534 /*
3535 * -EACCESS could mean that the user doesn't have correct permissions
3536 * to access the mount. It could also mean that we tried to mount
3537 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3538 * existing mount programs don't handle -EACCES very well so it should
3539 * be mapped to -EPERM instead.
3540 */
3541 if (status == -EACCES)
3542 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003543 return status;
3544}
3545
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003546/**
3547 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3548 * @server: initialized nfs_server handle
3549 * @fhandle: we fill in the pseudo-fs root file handle
3550 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003551 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003552 *
3553 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003554 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003555int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003556 struct nfs_fsinfo *info,
3557 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003558{
Andre Przywarac7757072015-04-23 17:17:40 +01003559 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003560
Andre Przywarac7757072015-04-23 17:17:40 +01003561 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003562 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003563
3564 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003565 status = server->nfs_client->cl_mvops->find_root_sec(server,
3566 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if (status == 0)
3569 status = nfs4_server_capabilities(server, fhandle);
3570 if (status == 0)
3571 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003572
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003573 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Bryan Schumakerbae36242012-05-10 15:07:31 -04003576static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3577 struct nfs_fsinfo *info)
3578{
3579 int error;
3580 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003581 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003582
3583 error = nfs4_server_capabilities(server, mntfh);
3584 if (error < 0) {
3585 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3586 return error;
3587 }
3588
David Quigley14c43f72013-05-22 12:50:43 -04003589 label = nfs4_label_alloc(server, GFP_KERNEL);
3590 if (IS_ERR(label))
3591 return PTR_ERR(label);
3592
David Quigley1775fd32013-05-22 12:50:42 -04003593 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003594 if (error < 0) {
3595 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003596 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003597 }
3598
3599 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3600 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3601 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3602
David Quigley14c43f72013-05-22 12:50:43 -04003603err_free_label:
3604 nfs4_label_free(label);
3605
Bryan Schumakerbae36242012-05-10 15:07:31 -04003606 return error;
3607}
3608
Manoj Naik6b97fd32006-06-09 09:34:29 -04003609/*
3610 * Get locations and (maybe) other attributes of a referral.
3611 * Note that we'll actually follow the referral later when
3612 * we detect fsid mismatch in inode revalidation
3613 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003614static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3615 const struct qstr *name, struct nfs_fattr *fattr,
3616 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003617{
3618 int status = -ENOMEM;
3619 struct page *page = NULL;
3620 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003621
3622 page = alloc_page(GFP_KERNEL);
3623 if (page == NULL)
3624 goto out;
3625 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3626 if (locations == NULL)
3627 goto out;
3628
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003629 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003630 if (status != 0)
3631 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003632
3633 /*
3634 * If the fsid didn't change, this is a migration event, not a
3635 * referral. Cause us to drop into the exception handler, which
3636 * will kick off migration recovery.
3637 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003638 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003639 dprintk("%s: server did not return a different fsid for"
3640 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003641 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003642 goto out;
3643 }
Andy Adamson533eb462011-06-13 18:25:56 -04003644 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3645 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003646
Andy Adamson533eb462011-06-13 18:25:56 -04003647 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003648 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003649 memset(fhandle, 0, sizeof(struct nfs_fh));
3650out:
3651 if (page)
3652 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003653 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003654 return status;
3655}
3656
David Quigley1775fd32013-05-22 12:50:42 -04003657static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3658 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659{
3660 struct nfs4_getattr_arg args = {
3661 .fh = fhandle,
3662 .bitmask = server->attr_bitmask,
3663 };
3664 struct nfs4_getattr_res res = {
3665 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003666 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 .server = server,
3668 };
3669 struct rpc_message msg = {
3670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3671 .rpc_argp = &args,
3672 .rpc_resp = &res,
3673 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003674
3675 args.bitmask = nfs4_bitmask(server, label);
3676
Trond Myklebust0e574af2005-10-27 22:12:38 -04003677 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003678 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
David Quigley1775fd32013-05-22 12:50:42 -04003681static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3682 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
3684 struct nfs4_exception exception = { };
3685 int err;
3686 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003687 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3688 trace_nfs4_getattr(server, fhandle, fattr, err);
3689 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 &exception);
3691 } while (exception.retry);
3692 return err;
3693}
3694
3695/*
3696 * The file is not closed if it is opened due to the a request to change
3697 * the size of the file. The open call will not be needed once the
3698 * VFS layer lookup-intents are implemented.
3699 *
3700 * Close is called when the inode is destroyed.
3701 * If we haven't opened the file for O_WRONLY, we
3702 * need to in the size_change case to obtain a stateid.
3703 *
3704 * Got race?
3705 * Because OPEN is always done by name in nfsv4, it is
3706 * possible that we opened a different file by the same
3707 * name. We can recognize this race condition, but we
3708 * can't do anything about it besides returning an error.
3709 *
3710 * This will be fixed with VFS changes (lookup-intent).
3711 */
3712static int
3713nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3714 struct iattr *sattr)
3715{
David Howells2b0143b2015-03-17 22:25:59 +00003716 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003717 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003718 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003719 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 int status;
3721
Peng Tao88ac8152014-09-12 11:04:10 +08003722 if (pnfs_ld_layoutret_on_setattr(inode) &&
3723 sattr->ia_valid & ATTR_SIZE &&
3724 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003725 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003726
Trond Myklebust0e574af2005-10-27 22:12:38 -04003727 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Andy Adamson26699402012-05-30 16:12:24 -04003729 /* Deal with open(O_TRUNC) */
3730 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003731 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003732
3733 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003734 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003735 return 0;
3736
Trond Myklebustd5308382005-11-04 15:33:38 -05003737 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003738 if (sattr->ia_valid & ATTR_FILE) {
3739 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003740
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003741 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003742 if (ctx) {
3743 cred = ctx->cred;
3744 state = ctx->state;
3745 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003746 }
3747
David Quigley14c43f72013-05-22 12:50:43 -04003748 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3749 if (IS_ERR(label))
3750 return PTR_ERR(label);
3751
3752 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003753 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003754 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003755 nfs_setsecurity(inode, fattr, label);
3756 }
David Quigley14c43f72013-05-22 12:50:43 -04003757 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 return status;
3759}
3760
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003761static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3762 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003763 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003764{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003765 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003766 int status;
3767 struct nfs4_lookup_arg args = {
3768 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003769 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003770 .name = name,
3771 };
3772 struct nfs4_lookup_res res = {
3773 .server = server,
3774 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003775 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003776 .fh = fhandle,
3777 };
3778 struct rpc_message msg = {
3779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3780 .rpc_argp = &args,
3781 .rpc_resp = &res,
3782 };
3783
David Quigleyaa9c2662013-05-22 12:50:44 -04003784 args.bitmask = nfs4_bitmask(server, label);
3785
David Howells2b3de442006-08-22 20:06:09 -04003786 nfs_fattr_init(fattr);
3787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003789 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 dprintk("NFS reply lookup: %d\n", status);
3791 return status;
3792}
3793
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003794static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003795{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003796 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003797 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003798 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3799 fattr->nlink = 2;
3800}
3801
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003802static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003803 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003804 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003805{
3806 struct nfs4_exception exception = { };
3807 struct rpc_clnt *client = *clnt;
3808 int err;
3809 do {
David Quigley1775fd32013-05-22 12:50:42 -04003810 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003811 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003812 switch (err) {
3813 case -NFS4ERR_BADNAME:
3814 err = -ENOENT;
3815 goto out;
3816 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003817 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003818 if (err == -NFS4ERR_MOVED)
3819 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003820 goto out;
3821 case -NFS4ERR_WRONGSEC:
3822 err = -EPERM;
3823 if (client != *clnt)
3824 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003825 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003826 if (IS_ERR(client))
3827 return PTR_ERR(client);
3828
3829 exception.retry = 1;
3830 break;
3831 default:
3832 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3833 }
3834 } while (exception.retry);
3835
3836out:
3837 if (err == 0)
3838 *clnt = client;
3839 else if (client != *clnt)
3840 rpc_shutdown_client(client);
3841
3842 return err;
3843}
3844
Al Virobeffb8f2016-07-20 16:34:42 -04003845static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003846 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3847 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003849 int status;
3850 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003851
David Quigley1775fd32013-05-22 12:50:42 -04003852 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003853 if (client != NFS_CLIENT(dir)) {
3854 rpc_shutdown_client(client);
3855 nfs_fixup_secinfo_attributes(fattr);
3856 }
3857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
3859
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003860struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003861nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003862 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3863{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003864 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003865 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003866
David Quigley1775fd32013-05-22 12:50:42 -04003867 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003868 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003869 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003870 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003871}
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3874{
Trond Myklebust76b32992007-08-10 17:45:11 -04003875 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 struct nfs4_accessargs args = {
3877 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003878 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003880 struct nfs4_accessres res = {
3881 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003882 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct rpc_message msg = {
3884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3885 .rpc_argp = &args,
3886 .rpc_resp = &res,
3887 .rpc_cred = entry->cred,
3888 };
3889 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003890 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 /*
3893 * Determine which access bits we want to ask for...
3894 */
3895 if (mode & MAY_READ)
3896 args.access |= NFS4_ACCESS_READ;
3897 if (S_ISDIR(inode->i_mode)) {
3898 if (mode & MAY_WRITE)
3899 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3900 if (mode & MAY_EXEC)
3901 args.access |= NFS4_ACCESS_LOOKUP;
3902 } else {
3903 if (mode & MAY_WRITE)
3904 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3905 if (mode & MAY_EXEC)
3906 args.access |= NFS4_ACCESS_EXECUTE;
3907 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003908
3909 res.fattr = nfs_alloc_fattr();
3910 if (res.fattr == NULL)
3911 return -ENOMEM;
3912
Bryan Schumaker7c513052011-03-24 17:12:24 +00003913 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003915 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003916 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003918 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return status;
3920}
3921
3922static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3923{
3924 struct nfs4_exception exception = { };
3925 int err;
3926 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003927 err = _nfs4_proc_access(inode, entry);
3928 trace_nfs4_access(inode, err);
3929 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 &exception);
3931 } while (exception.retry);
3932 return err;
3933}
3934
3935/*
3936 * TODO: For the time being, we don't try to get any attributes
3937 * along with any of the zero-copy operations READ, READDIR,
3938 * READLINK, WRITE.
3939 *
3940 * In the case of the first three, we want to put the GETATTR
3941 * after the read-type operation -- this is because it is hard
3942 * to predict the length of a GETATTR response in v4, and thus
3943 * align the READ data correctly. This means that the GETATTR
3944 * may end up partially falling into the page cache, and we should
3945 * shift it into the 'tail' of the xdr_buf before processing.
3946 * To do this efficiently, we need to know the total length
3947 * of data received, which doesn't seem to be available outside
3948 * of the RPC layer.
3949 *
3950 * In the case of WRITE, we also want to put the GETATTR after
3951 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003952 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 *
3954 * Both of these changes to the XDR layer would in fact be quite
3955 * minor, but I decided to leave them for a subsequent patch.
3956 */
3957static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3958 unsigned int pgbase, unsigned int pglen)
3959{
3960 struct nfs4_readlink args = {
3961 .fh = NFS_FH(inode),
3962 .pgbase = pgbase,
3963 .pglen = pglen,
3964 .pages = &page,
3965 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003966 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 struct rpc_message msg = {
3968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3969 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003970 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 };
3972
Bryan Schumaker7c513052011-03-24 17:12:24 +00003973 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 -07003974}
3975
3976static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3977 unsigned int pgbase, unsigned int pglen)
3978{
3979 struct nfs4_exception exception = { };
3980 int err;
3981 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003982 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3983 trace_nfs4_readlink(inode, err);
3984 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 &exception);
3986 } while (exception.retry);
3987 return err;
3988}
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003991 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993static int
3994nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003995 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
David Quigleyaa9c2662013-05-22 12:50:44 -04003997 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003998 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 int status = 0;
4001
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004002 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
4003 if (IS_ERR(ctx))
4004 return PTR_ERR(ctx);
4005
David Quigleyaa9c2662013-05-22 12:50:44 -04004006 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4007
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004008 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004009 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (IS_ERR(state)) {
4011 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004015 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004016 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 return status;
4018}
4019
Al Virobeffb8f2016-07-20 16:34:42 -04004020static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
Trond Myklebust16e42952005-10-27 22:12:44 -04004022 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004023 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004025 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004027 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004028 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4032 .rpc_argp = &args,
4033 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004035 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004036
Bryan Schumaker7c513052011-03-24 17:12:24 +00004037 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004038 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004039 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return status;
4041}
4042
Al Virobeffb8f2016-07-20 16:34:42 -04004043static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
4045 struct nfs4_exception exception = { };
4046 int err;
4047 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004048 err = _nfs4_proc_remove(dir, name);
4049 trace_nfs4_remove(dir, name, err);
4050 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 &exception);
4052 } while (exception.retry);
4053 return err;
4054}
4055
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004056static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004058 struct nfs_server *server = NFS_SERVER(dir);
4059 struct nfs_removeargs *args = msg->rpc_argp;
4060 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004062 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004064 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004065
4066 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004069static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4070{
Al Viro884be172016-04-28 23:56:31 -04004071 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004072 &data->args.seq_args,
4073 &data->res.seq_res,
4074 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004077static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004079 struct nfs_unlinkdata *data = task->tk_calldata;
4080 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004081
Trond Myklebust14516c32010-07-31 14:29:06 -04004082 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,
4085 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004086 return 0;
4087 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004088 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
Jeff Laytond3d41522010-09-17 17:31:57 -04004091static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4092{
4093 struct nfs_server *server = NFS_SERVER(dir);
4094 struct nfs_renameargs *arg = msg->rpc_argp;
4095 struct nfs_renameres *res = msg->rpc_resp;
4096
4097 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004098 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004099 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004100}
4101
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004102static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4103{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004104 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4105 &data->args.seq_args,
4106 &data->res.seq_res,
4107 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004108}
4109
4110static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4111 struct inode *new_dir)
4112{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004113 struct nfs_renamedata *data = task->tk_calldata;
4114 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004115
4116 if (!nfs4_sequence_done(task, &res->seq_res))
4117 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004118 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004119 return 0;
4120
4121 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004122 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004123 return 1;
4124}
4125
Al Virobeffb8f2016-07-20 16:34:42 -04004126static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004128 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 struct nfs4_link_arg arg = {
4130 .fh = NFS_FH(inode),
4131 .dir_fh = NFS_FH(dir),
4132 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004133 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004135 struct nfs4_link_res res = {
4136 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004137 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 struct rpc_message msg = {
4140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4141 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004142 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004144 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Trond Myklebust136f2622010-04-16 16:22:49 -04004146 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004147 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004148 goto out;
4149
David Quigley14c43f72013-05-22 12:50:43 -04004150 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4151 if (IS_ERR(res.label)) {
4152 status = PTR_ERR(res.label);
4153 goto out;
4154 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004155 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004156
Bryan Schumaker7c513052011-03-24 17:12:24 +00004157 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004158 if (!status) {
4159 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004160 status = nfs_post_op_update_inode(inode, res.fattr);
4161 if (!status)
4162 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004163 }
David Quigley14c43f72013-05-22 12:50:43 -04004164
4165
4166 nfs4_label_free(res.label);
4167
Trond Myklebust136f2622010-04-16 16:22:49 -04004168out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004169 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return status;
4171}
4172
Al Virobeffb8f2016-07-20 16:34:42 -04004173static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
4175 struct nfs4_exception exception = { };
4176 int err;
4177 do {
4178 err = nfs4_handle_exception(NFS_SERVER(inode),
4179 _nfs4_proc_link(inode, dir, name),
4180 &exception);
4181 } while (exception.retry);
4182 return err;
4183}
4184
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004185struct nfs4_createdata {
4186 struct rpc_message msg;
4187 struct nfs4_create_arg arg;
4188 struct nfs4_create_res res;
4189 struct nfs_fh fh;
4190 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004191 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192};
4193
4194static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004195 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004196{
4197 struct nfs4_createdata *data;
4198
4199 data = kzalloc(sizeof(*data), GFP_KERNEL);
4200 if (data != NULL) {
4201 struct nfs_server *server = NFS_SERVER(dir);
4202
David Quigley14c43f72013-05-22 12:50:43 -04004203 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4204 if (IS_ERR(data->label))
4205 goto out_free;
4206
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004207 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4208 data->msg.rpc_argp = &data->arg;
4209 data->msg.rpc_resp = &data->res;
4210 data->arg.dir_fh = NFS_FH(dir);
4211 data->arg.server = server;
4212 data->arg.name = name;
4213 data->arg.attrs = sattr;
4214 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004215 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 data->res.server = server;
4217 data->res.fh = &data->fh;
4218 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004219 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004220 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221 }
4222 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004223out_free:
4224 kfree(data);
4225 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004226}
4227
4228static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4229{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004230 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004231 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232 if (status == 0) {
4233 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004234 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004235 }
4236 return status;
4237}
4238
4239static void nfs4_free_createdata(struct nfs4_createdata *data)
4240{
David Quigley14c43f72013-05-22 12:50:43 -04004241 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242 kfree(data);
4243}
4244
Chuck Lever4f390c12006-08-22 20:06:22 -04004245static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004246 struct page *page, unsigned int len, struct iattr *sattr,
4247 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 struct nfs4_createdata *data;
4250 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Chuck Lever94a6d752006-08-22 20:06:23 -04004252 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004253 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004254
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004255 status = -ENOMEM;
4256 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4257 if (data == NULL)
4258 goto out;
4259
4260 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4261 data->arg.u.symlink.pages = &page;
4262 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004263 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004265 status = nfs4_do_create(dir, dentry, data);
4266
4267 nfs4_free_createdata(data);
4268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return status;
4270}
4271
Chuck Lever4f390c12006-08-22 20:06:22 -04004272static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004273 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
4275 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004276 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004278
4279 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004282 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4283 trace_nfs4_symlink(dir, &dentry->d_name, err);
4284 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 &exception);
4286 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004287
4288 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return err;
4290}
4291
4292static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004293 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004295 struct nfs4_createdata *data;
4296 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004298 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4299 if (data == NULL)
4300 goto out;
4301
David Quigley1775fd32013-05-22 12:50:42 -04004302 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004303 status = nfs4_do_create(dir, dentry, data);
4304
4305 nfs4_free_createdata(data);
4306out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return status;
4308}
4309
4310static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4311 struct iattr *sattr)
4312{
4313 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004314 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004316
David Quigleyaa9c2662013-05-22 12:50:44 -04004317 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4318
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004319 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004321 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4322 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4323 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 &exception);
4325 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004326 nfs4_label_release_security(label);
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 return err;
4329}
4330
4331static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004332 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
David Howells2b0143b2015-03-17 22:25:59 +00004334 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 struct nfs4_readdir_arg args = {
4336 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004337 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 .pgbase = 0,
4339 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004340 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004341 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 };
4343 struct nfs4_readdir_res res;
4344 struct rpc_message msg = {
4345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4346 .rpc_argp = &args,
4347 .rpc_resp = &res,
4348 .rpc_cred = cred,
4349 };
4350 int status;
4351
Al Viro6de14722013-09-16 10:53:17 -04004352 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4353 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004354 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004355 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004357 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 -05004358 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004359 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004360 status += args.pgbase;
4361 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004362
4363 nfs_invalidate_atime(dir);
4364
Harvey Harrison3110ff82008-05-02 13:42:44 -07004365 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 return status;
4367}
4368
4369static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004370 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
4372 struct nfs4_exception exception = { };
4373 int err;
4374 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004375 err = _nfs4_proc_readdir(dentry, cred, cookie,
4376 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004377 trace_nfs4_readdir(d_inode(dentry), err);
4378 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 &exception);
4380 } while (exception.retry);
4381 return err;
4382}
4383
4384static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004385 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004387 struct nfs4_createdata *data;
4388 int mode = sattr->ia_mode;
4389 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004391 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4392 if (data == NULL)
4393 goto out;
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004398 data->arg.ftype = NF4BLK;
4399 data->arg.u.device.specdata1 = MAJOR(rdev);
4400 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004403 data->arg.ftype = NF4CHR;
4404 data->arg.u.device.specdata1 = MAJOR(rdev);
4405 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004406 } else if (!S_ISSOCK(mode)) {
4407 status = -EINVAL;
4408 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
David Quigley1775fd32013-05-22 12:50:42 -04004410
David Quigleyaa9c2662013-05-22 12:50:44 -04004411 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004413out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414 nfs4_free_createdata(data);
4415out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return status;
4417}
4418
4419static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4420 struct iattr *sattr, dev_t rdev)
4421{
4422 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004423 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004425
David Quigleyaa9c2662013-05-22 12:50:44 -04004426 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4427
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004428 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004430 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4431 trace_nfs4_mknod(dir, &dentry->d_name, err);
4432 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 &exception);
4434 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004435
4436 nfs4_label_release_security(label);
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return err;
4439}
4440
4441static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4442 struct nfs_fsstat *fsstat)
4443{
4444 struct nfs4_statfs_arg args = {
4445 .fh = fhandle,
4446 .bitmask = server->attr_bitmask,
4447 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004448 struct nfs4_statfs_res res = {
4449 .fsstat = fsstat,
4450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4453 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004454 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 };
4456
Trond Myklebust0e574af2005-10-27 22:12:38 -04004457 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004458 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
4461static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4462{
4463 struct nfs4_exception exception = { };
4464 int err;
4465 do {
4466 err = nfs4_handle_exception(server,
4467 _nfs4_proc_statfs(server, fhandle, fsstat),
4468 &exception);
4469 } while (exception.retry);
4470 return err;
4471}
4472
4473static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4474 struct nfs_fsinfo *fsinfo)
4475{
4476 struct nfs4_fsinfo_arg args = {
4477 .fh = fhandle,
4478 .bitmask = server->attr_bitmask,
4479 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004480 struct nfs4_fsinfo_res res = {
4481 .fsinfo = fsinfo,
4482 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 struct rpc_message msg = {
4484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4485 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004486 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 };
4488
Bryan Schumaker7c513052011-03-24 17:12:24 +00004489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490}
4491
4492static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4493{
4494 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004495 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 int err;
4497
4498 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004499 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004500 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004501 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004502 nfs4_set_lease_period(server->nfs_client,
4503 fsinfo->lease_time * HZ,
4504 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004505 break;
4506 }
4507 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 } while (exception.retry);
4509 return err;
4510}
4511
4512static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4513{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004514 int error;
4515
Trond Myklebust0e574af2005-10-27 22:12:38 -04004516 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004517 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004518 if (error == 0) {
4519 /* block layout checks this! */
4520 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004521 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004522 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004523
4524 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525}
4526
4527static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4528 struct nfs_pathconf *pathconf)
4529{
4530 struct nfs4_pathconf_arg args = {
4531 .fh = fhandle,
4532 .bitmask = server->attr_bitmask,
4533 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004534 struct nfs4_pathconf_res res = {
4535 .pathconf = pathconf,
4536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 struct rpc_message msg = {
4538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4539 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004540 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 };
4542
4543 /* None of the pathconf attributes are mandatory to implement */
4544 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4545 memset(pathconf, 0, sizeof(*pathconf));
4546 return 0;
4547 }
4548
Trond Myklebust0e574af2005-10-27 22:12:38 -04004549 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004550 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
4553static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4554 struct nfs_pathconf *pathconf)
4555{
4556 struct nfs4_exception exception = { };
4557 int err;
4558
4559 do {
4560 err = nfs4_handle_exception(server,
4561 _nfs4_proc_pathconf(server, fhandle, pathconf),
4562 &exception);
4563 } while (exception.retry);
4564 return err;
4565}
4566
Trond Myklebust5521abf2013-03-16 20:54:34 -04004567int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004568 const struct nfs_open_context *ctx,
4569 const struct nfs_lock_context *l_ctx,
4570 fmode_t fmode)
4571{
4572 const struct nfs_lockowner *lockowner = NULL;
4573
4574 if (l_ctx != NULL)
4575 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004576 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004577}
4578EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4579
Trond Myklebust5521abf2013-03-16 20:54:34 -04004580static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4581 const struct nfs_open_context *ctx,
4582 const struct nfs_lock_context *l_ctx,
4583 fmode_t fmode)
4584{
4585 nfs4_stateid current_stateid;
4586
Trond Myklebuste1253be2014-03-05 08:44:23 -05004587 /* If the current stateid represents a lost lock, then exit */
4588 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4589 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004590 return nfs4_stateid_match(stateid, &current_stateid);
4591}
4592
4593static bool nfs4_error_stateid_expired(int err)
4594{
4595 switch (err) {
4596 case -NFS4ERR_DELEG_REVOKED:
4597 case -NFS4ERR_ADMIN_REVOKED:
4598 case -NFS4ERR_BAD_STATEID:
4599 case -NFS4ERR_STALE_STATEID:
4600 case -NFS4ERR_OLD_STATEID:
4601 case -NFS4ERR_OPENMODE:
4602 case -NFS4ERR_EXPIRED:
4603 return true;
4604 }
4605 return false;
4606}
4607
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004608static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004613 if (task->tk_status < 0) {
4614 struct nfs4_exception exception = {
4615 .inode = hdr->inode,
4616 .state = hdr->args.context->state,
4617 .stateid = &hdr->args.stateid,
4618 };
4619 task->tk_status = nfs4_async_handle_exception(task,
4620 server, task->tk_status, &exception);
4621 if (exception.retry) {
4622 rpc_restart_call_prepare(task);
4623 return -EAGAIN;
4624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004628 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
Trond Myklebust5521abf2013-03-16 20:54:34 -04004632static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004633 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004634{
4635
4636 if (!nfs4_error_stateid_expired(task->tk_status) ||
4637 nfs4_stateid_is_current(&args->stateid,
4638 args->context,
4639 args->lock_context,
4640 FMODE_READ))
4641 return false;
4642 rpc_restart_call_prepare(task);
4643 return true;
4644}
4645
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004646static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004647{
4648
4649 dprintk("--> %s\n", __func__);
4650
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004651 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004652 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004654 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004655 if (task->tk_status > 0)
4656 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4658 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004659}
4660
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4662 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004665 if (!hdr->pgio_done_cb)
4666 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004667 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4672 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004673{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4675 &hdr->args.seq_args,
4676 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004677 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004678 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4680 hdr->args.lock_context,
4681 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004682 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004684 return -EIO;
4685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686}
4687
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688static int nfs4_write_done_cb(struct rpc_task *task,
4689 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004692
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004694 if (task->tk_status < 0) {
4695 struct nfs4_exception exception = {
4696 .inode = hdr->inode,
4697 .state = hdr->args.context->state,
4698 .stateid = &hdr->args.stateid,
4699 };
4700 task->tk_status = nfs4_async_handle_exception(task,
4701 NFS_SERVER(inode), task->tk_status,
4702 &exception);
4703 if (exception.retry) {
4704 rpc_restart_call_prepare(task);
4705 return -EAGAIN;
4706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004708 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004710 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004711 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
Trond Myklebust5521abf2013-03-16 20:54:34 -04004715static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004716 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004717{
4718
4719 if (!nfs4_error_stateid_expired(task->tk_status) ||
4720 nfs4_stateid_is_current(&args->stateid,
4721 args->context,
4722 args->lock_context,
4723 FMODE_WRITE))
4724 return false;
4725 rpc_restart_call_prepare(task);
4726 return true;
4727}
4728
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004729static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004730{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004731 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004732 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004733 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004734 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004735 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4736 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004737}
4738
Trond Myklebust5a37f852012-04-28 14:55:16 -04004739static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004741{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004742 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004743 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004744 return false;
4745 /* Otherwise, request attributes if and only if we don't hold
4746 * a delegation
4747 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004748 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004749}
Fred Isamana69aef12011-03-03 15:13:47 +00004750
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004751static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4752 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004754 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004755
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004756 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4757 hdr->args.bitmask = NULL;
4758 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004759 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004760 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004761
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762 if (!hdr->pgio_done_cb)
4763 hdr->pgio_done_cb = nfs4_write_done_cb;
4764 hdr->res.server = server;
4765 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004767 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004768 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}
4770
Fred Isaman0b7c0152012-04-20 14:47:39 -04004771static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4772{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004773 nfs4_setup_sequence(NFS_SERVER(data->inode),
4774 &data->args.seq_args,
4775 &data->res.seq_res,
4776 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
Fred Isaman0b7c0152012-04-20 14:47:39 -04004779static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004782
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004783 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004784 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4785 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004786 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004787 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
4791
Fred Isaman0b7c0152012-04-20 14:47:39 -04004792static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004793{
4794 if (!nfs4_sequence_done(task, &data->res.seq_res))
4795 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004796 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004797}
4798
Fred Isaman0b7c0152012-04-20 14:47:39 -04004799static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004801 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004802
Fred Isaman0b7c0152012-04-20 14:47:39 -04004803 if (data->commit_done_cb == NULL)
4804 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004805 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004806 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004807 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808}
4809
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004810struct nfs4_renewdata {
4811 struct nfs_client *client;
4812 unsigned long timestamp;
4813};
4814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815/*
4816 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4817 * standalone procedure for queueing an asynchronous RENEW.
4818 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004819static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004820{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004821 struct nfs4_renewdata *data = calldata;
4822 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004823
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004824 if (atomic_read(&clp->cl_count) > 1)
4825 nfs4_schedule_state_renewal(clp);
4826 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004827 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004828}
4829
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004830static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004832 struct nfs4_renewdata *data = calldata;
4833 struct nfs_client *clp = data->client;
4834 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004836 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004837 switch (task->tk_status) {
4838 case 0:
4839 break;
4840 case -NFS4ERR_LEASE_MOVED:
4841 nfs4_schedule_lease_moved_recovery(clp);
4842 break;
4843 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004844 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004845 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4846 return;
4847 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004848 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004849 return;
4850 }
4851 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004853 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004856static const struct rpc_call_ops nfs4_renew_ops = {
4857 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004858 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004859};
4860
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004861static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862{
4863 struct rpc_message msg = {
4864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4865 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004866 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004868 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004870 if (renew_flags == 0)
4871 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004872 if (!atomic_inc_not_zero(&clp->cl_count))
4873 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004874 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004875 if (data == NULL)
4876 return -ENOMEM;
4877 data->client = clp;
4878 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004879 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004880 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004883static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
4885 struct rpc_message msg = {
4886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4887 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004888 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 };
4890 unsigned long now = jiffies;
4891 int status;
4892
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004893 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 if (status < 0)
4895 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004896 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 return 0;
4898}
4899
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004900static inline int nfs4_server_supports_acls(struct nfs_server *server)
4901{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004902 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004903}
4904
Trond Myklebust21f498c2012-08-24 10:59:25 -04004905/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4906 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004907 * the stack.
4908 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004909#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004910
Neil Hormane9e3d722011-03-04 19:26:03 -05004911static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004912 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004913{
4914 struct page *newpage, **spages;
4915 int rc = 0;
4916 size_t len;
4917 spages = pages;
4918
4919 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004920 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004921 newpage = alloc_page(GFP_KERNEL);
4922
4923 if (newpage == NULL)
4924 goto unwind;
4925 memcpy(page_address(newpage), buf, len);
4926 buf += len;
4927 buflen -= len;
4928 *pages++ = newpage;
4929 rc++;
4930 } while (buflen != 0);
4931
4932 return rc;
4933
4934unwind:
4935 for(; rc > 0; rc--)
4936 __free_page(spages[rc-1]);
4937 return -ENOMEM;
4938}
4939
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004940struct nfs4_cached_acl {
4941 int cached;
4942 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004943 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004944};
4945
4946static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004947{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004948 struct nfs_inode *nfsi = NFS_I(inode);
4949
4950 spin_lock(&inode->i_lock);
4951 kfree(nfsi->nfs4_acl);
4952 nfsi->nfs4_acl = acl;
4953 spin_unlock(&inode->i_lock);
4954}
4955
4956static void nfs4_zap_acl_attr(struct inode *inode)
4957{
4958 nfs4_set_cached_acl(inode, NULL);
4959}
4960
4961static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4962{
4963 struct nfs_inode *nfsi = NFS_I(inode);
4964 struct nfs4_cached_acl *acl;
4965 int ret = -ENOENT;
4966
4967 spin_lock(&inode->i_lock);
4968 acl = nfsi->nfs4_acl;
4969 if (acl == NULL)
4970 goto out;
4971 if (buf == NULL) /* user is just asking for length */
4972 goto out_len;
4973 if (acl->cached == 0)
4974 goto out;
4975 ret = -ERANGE; /* see getxattr(2) man page */
4976 if (acl->len > buflen)
4977 goto out;
4978 memcpy(buf, acl->data, acl->len);
4979out_len:
4980 ret = acl->len;
4981out:
4982 spin_unlock(&inode->i_lock);
4983 return ret;
4984}
4985
Sachin Prabhu5794d212012-04-17 14:36:40 +01004986static 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 +00004987{
4988 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004989 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004990
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004991 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004992 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004993 if (acl == NULL)
4994 goto out;
4995 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004996 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004997 } else {
4998 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4999 if (acl == NULL)
5000 goto out;
5001 acl->cached = 0;
5002 }
5003 acl->len = acl_len;
5004out:
5005 nfs4_set_cached_acl(inode, acl);
5006}
5007
Andy Adamsonbf118a32011-12-07 11:55:27 -05005008/*
5009 * The getxattr API returns the required buffer length when called with a
5010 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5011 * the required buf. On a NULL buf, we send a page of data to the server
5012 * guessing that the ACL request can be serviced by a page. If so, we cache
5013 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5014 * the cache. If not so, we throw away the page, and cache the required
5015 * length. The next getxattr call will then produce another round trip to
5016 * the server, this time with the input buf of the required size.
5017 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005018static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005019{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005020 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005021 struct nfs_getaclargs args = {
5022 .fh = NFS_FH(inode),
5023 .acl_pages = pages,
5024 .acl_len = buflen,
5025 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005026 struct nfs_getaclres res = {
5027 .acl_len = buflen,
5028 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005029 struct rpc_message msg = {
5030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5031 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005032 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005033 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005034 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005035 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005036
Trond Myklebust21f498c2012-08-24 10:59:25 -04005037 if (npages > ARRAY_SIZE(pages))
5038 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005039
Andy Adamsonbf118a32011-12-07 11:55:27 -05005040 for (i = 0; i < npages; i++) {
5041 pages[i] = alloc_page(GFP_KERNEL);
5042 if (!pages[i])
5043 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005044 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005045
5046 /* for decoding across pages */
5047 res.acl_scratch = alloc_page(GFP_KERNEL);
5048 if (!res.acl_scratch)
5049 goto out_free;
5050
Andy Adamsonbf118a32011-12-07 11:55:27 -05005051 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005052
Peng Taode040be2012-01-10 22:42:47 +08005053 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005054 __func__, buf, buflen, npages, args.acl_len);
5055 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5056 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005057 if (ret)
5058 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005059
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005060 /* Handle the case where the passed-in buffer is too short */
5061 if (res.acl_flags & NFS4_ACL_TRUNC) {
5062 /* Did the user only issue a request for the acl length? */
5063 if (buf == NULL)
5064 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005065 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005066 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005067 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005068 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005069 if (buf) {
5070 if (res.acl_len > buflen) {
5071 ret = -ERANGE;
5072 goto out_free;
5073 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005074 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005075 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005076out_ok:
5077 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005078out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005079 for (i = 0; i < npages; i++)
5080 if (pages[i])
5081 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005082 if (res.acl_scratch)
5083 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005084 return ret;
5085}
5086
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005087static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5088{
5089 struct nfs4_exception exception = { };
5090 ssize_t ret;
5091 do {
5092 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005093 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005094 if (ret >= 0)
5095 break;
5096 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5097 } while (exception.retry);
5098 return ret;
5099}
5100
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005101static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5102{
5103 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005104 int ret;
5105
5106 if (!nfs4_server_supports_acls(server))
5107 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005108 ret = nfs_revalidate_inode(server, inode);
5109 if (ret < 0)
5110 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005111 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5112 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005113 ret = nfs4_read_cached_acl(inode, buf, buflen);
5114 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005115 /* -ENOENT is returned if there is no ACL or if there is an ACL
5116 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005117 return ret;
5118 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005119}
5120
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005121static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005122{
5123 struct nfs_server *server = NFS_SERVER(inode);
5124 struct page *pages[NFS4ACL_MAXPAGES];
5125 struct nfs_setaclargs arg = {
5126 .fh = NFS_FH(inode),
5127 .acl_pages = pages,
5128 .acl_len = buflen,
5129 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005130 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005131 struct rpc_message msg = {
5132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5133 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005134 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005135 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005136 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005137 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005138
5139 if (!nfs4_server_supports_acls(server))
5140 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005141 if (npages > ARRAY_SIZE(pages))
5142 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005143 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005144 if (i < 0)
5145 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005146 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005147 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005148
5149 /*
5150 * Free each page after tx, so the only ref left is
5151 * held by the network stack
5152 */
5153 for (; i > 0; i--)
5154 put_page(pages[i-1]);
5155
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005156 /*
5157 * Acl update can result in inode attribute update.
5158 * so mark the attribute cache invalid.
5159 */
5160 spin_lock(&inode->i_lock);
5161 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5162 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005163 nfs_access_zap_cache(inode);
5164 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005165 return ret;
5166}
5167
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005168static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5169{
5170 struct nfs4_exception exception = { };
5171 int err;
5172 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005173 err = __nfs4_proc_set_acl(inode, buf, buflen);
5174 trace_nfs4_set_acl(inode, err);
5175 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005176 &exception);
5177 } while (exception.retry);
5178 return err;
5179}
5180
David Quigleyaa9c2662013-05-22 12:50:44 -04005181#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5182static int _nfs4_get_security_label(struct inode *inode, void *buf,
5183 size_t buflen)
5184{
5185 struct nfs_server *server = NFS_SERVER(inode);
5186 struct nfs_fattr fattr;
5187 struct nfs4_label label = {0, 0, buflen, buf};
5188
5189 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005190 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005191 .fh = NFS_FH(inode),
5192 .bitmask = bitmask,
5193 };
5194 struct nfs4_getattr_res res = {
5195 .fattr = &fattr,
5196 .label = &label,
5197 .server = server,
5198 };
5199 struct rpc_message msg = {
5200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005201 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005202 .rpc_resp = &res,
5203 };
5204 int ret;
5205
5206 nfs_fattr_init(&fattr);
5207
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005208 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005209 if (ret)
5210 return ret;
5211 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5212 return -ENOENT;
5213 if (buflen < label.len)
5214 return -ERANGE;
5215 return 0;
5216}
5217
5218static int nfs4_get_security_label(struct inode *inode, void *buf,
5219 size_t buflen)
5220{
5221 struct nfs4_exception exception = { };
5222 int err;
5223
5224 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5225 return -EOPNOTSUPP;
5226
5227 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005228 err = _nfs4_get_security_label(inode, buf, buflen);
5229 trace_nfs4_get_security_label(inode, err);
5230 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005231 &exception);
5232 } while (exception.retry);
5233 return err;
5234}
5235
5236static int _nfs4_do_set_security_label(struct inode *inode,
5237 struct nfs4_label *ilabel,
5238 struct nfs_fattr *fattr,
5239 struct nfs4_label *olabel)
5240{
5241
5242 struct iattr sattr = {0};
5243 struct nfs_server *server = NFS_SERVER(inode);
5244 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005245 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005246 .fh = NFS_FH(inode),
5247 .iap = &sattr,
5248 .server = server,
5249 .bitmask = bitmask,
5250 .label = ilabel,
5251 };
5252 struct nfs_setattrres res = {
5253 .fattr = fattr,
5254 .label = olabel,
5255 .server = server,
5256 };
5257 struct rpc_message msg = {
5258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005259 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005260 .rpc_resp = &res,
5261 };
5262 int status;
5263
Jeff Layton12207f62013-11-01 10:49:32 -04005264 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005265
Jeff Layton12207f62013-11-01 10:49:32 -04005266 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005267 if (status)
5268 dprintk("%s failed: %d\n", __func__, status);
5269
5270 return status;
5271}
5272
5273static int nfs4_do_set_security_label(struct inode *inode,
5274 struct nfs4_label *ilabel,
5275 struct nfs_fattr *fattr,
5276 struct nfs4_label *olabel)
5277{
5278 struct nfs4_exception exception = { };
5279 int err;
5280
5281 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005282 err = _nfs4_do_set_security_label(inode, ilabel,
5283 fattr, olabel);
5284 trace_nfs4_set_security_label(inode, err);
5285 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005286 &exception);
5287 } while (exception.retry);
5288 return err;
5289}
5290
5291static int
Al Viro59301222016-05-27 10:19:30 -04005292nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005293{
5294 struct nfs4_label ilabel, *olabel = NULL;
5295 struct nfs_fattr fattr;
5296 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005297 int status;
5298
5299 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5300 return -EOPNOTSUPP;
5301
5302 nfs_fattr_init(&fattr);
5303
5304 ilabel.pi = 0;
5305 ilabel.lfs = 0;
5306 ilabel.label = (char *)buf;
5307 ilabel.len = buflen;
5308
5309 cred = rpc_lookup_cred();
5310 if (IS_ERR(cred))
5311 return PTR_ERR(cred);
5312
5313 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5314 if (IS_ERR(olabel)) {
5315 status = -PTR_ERR(olabel);
5316 goto out;
5317 }
5318
5319 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5320 if (status == 0)
5321 nfs_setsecurity(inode, &fattr, olabel);
5322
5323 nfs4_label_free(olabel);
5324out:
5325 put_rpccred(cred);
5326 return status;
5327}
5328#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5329
5330
Chuck Leverf0920752012-05-21 22:45:41 -04005331static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5332 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005333{
5334 __be32 verf[2];
5335
Chuck Lever2c820d92012-05-21 22:45:33 -04005336 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5337 /* An impossible timestamp guarantees this value
5338 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005339 verf[0] = cpu_to_be32(U32_MAX);
5340 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005341 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005342 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005343 u64 ns = ktime_to_ns(nn->boot_time);
5344
5345 verf[0] = cpu_to_be32(ns >> 32);
5346 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005347 }
Chuck Levercd937102012-03-02 17:14:31 -05005348 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5349}
5350
Jeff Laytona3192682015-06-09 19:43:59 -04005351static int
5352nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005353{
Jeff Laytona3192682015-06-09 19:43:59 -04005354 size_t len;
5355 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005356
Trond Myklebustceb3a162015-01-03 15:16:04 -05005357 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005358 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005359
Jeff Laytona3192682015-06-09 19:43:59 -04005360 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005361 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005362 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5363 1 +
5364 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5365 1;
5366 rcu_read_unlock();
5367
5368 if (len > NFS4_OPAQUE_LIMIT + 1)
5369 return -EINVAL;
5370
5371 /*
5372 * Since this string is allocated at mount time, and held until the
5373 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5374 * about a memory-reclaim deadlock.
5375 */
5376 str = kmalloc(len, GFP_KERNEL);
5377 if (!str)
5378 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005379
Chuck Levere984a552012-09-14 17:24:21 -04005380 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005381 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005382 clp->cl_ipaddr,
5383 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5384 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005385 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005386
Jeff Laytona3192682015-06-09 19:43:59 -04005387 clp->cl_owner_id = str;
5388 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005389}
5390
Jeff Layton873e3852015-06-09 19:44:00 -04005391static int
5392nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005393{
Jeff Layton873e3852015-06-09 19:44:00 -04005394 size_t len;
5395 char *str;
5396
5397 len = 10 + 10 + 1 + 10 + 1 +
5398 strlen(nfs4_client_id_uniquifier) + 1 +
5399 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5400
5401 if (len > NFS4_OPAQUE_LIMIT + 1)
5402 return -EINVAL;
5403
5404 /*
5405 * Since this string is allocated at mount time, and held until the
5406 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5407 * about a memory-reclaim deadlock.
5408 */
5409 str = kmalloc(len, GFP_KERNEL);
5410 if (!str)
5411 return -ENOMEM;
5412
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005413 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005414 clp->rpc_ops->version, clp->cl_minorversion,
5415 nfs4_client_id_uniquifier,
5416 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005417 clp->cl_owner_id = str;
5418 return 0;
5419}
5420
5421static int
5422nfs4_init_uniform_client_string(struct nfs_client *clp)
5423{
Jeff Layton873e3852015-06-09 19:44:00 -04005424 size_t len;
5425 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005426
5427 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005428 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005429
5430 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005431 return nfs4_init_uniquifier_client_string(clp);
5432
5433 len = 10 + 10 + 1 + 10 + 1 +
5434 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5435
5436 if (len > NFS4_OPAQUE_LIMIT + 1)
5437 return -EINVAL;
5438
5439 /*
5440 * Since this string is allocated at mount time, and held until the
5441 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5442 * about a memory-reclaim deadlock.
5443 */
5444 str = kmalloc(len, GFP_KERNEL);
5445 if (!str)
5446 return -ENOMEM;
5447
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005448 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005449 clp->rpc_ops->version, clp->cl_minorversion,
5450 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005451 clp->cl_owner_id = str;
5452 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005453}
5454
Chuck Lever706cb8d2014-03-12 12:51:47 -04005455/*
5456 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5457 * services. Advertise one based on the address family of the
5458 * clientaddr.
5459 */
5460static unsigned int
5461nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5462{
5463 if (strchr(clp->cl_ipaddr, ':') != NULL)
5464 return scnprintf(buf, len, "tcp6");
5465 else
5466 return scnprintf(buf, len, "tcp");
5467}
5468
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005469static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5470{
5471 struct nfs4_setclientid *sc = calldata;
5472
5473 if (task->tk_status == 0)
5474 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5475}
5476
5477static const struct rpc_call_ops nfs4_setclientid_ops = {
5478 .rpc_call_done = nfs4_setclientid_done,
5479};
5480
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005481/**
5482 * nfs4_proc_setclientid - Negotiate client ID
5483 * @clp: state data structure
5484 * @program: RPC program for NFSv4 callback service
5485 * @port: IP port number for NFS4 callback service
5486 * @cred: RPC credential to use for this call
5487 * @res: where to place the result
5488 *
5489 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5490 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005491int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5492 unsigned short port, struct rpc_cred *cred,
5493 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494{
5495 nfs4_verifier sc_verifier;
5496 struct nfs4_setclientid setclientid = {
5497 .sc_verifier = &sc_verifier,
5498 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005499 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 };
5501 struct rpc_message msg = {
5502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5503 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005504 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005505 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005507 struct rpc_task *task;
5508 struct rpc_task_setup task_setup_data = {
5509 .rpc_client = clp->cl_rpcclient,
5510 .rpc_message = &msg,
5511 .callback_ops = &nfs4_setclientid_ops,
5512 .callback_data = &setclientid,
5513 .flags = RPC_TASK_TIMEOUT,
5514 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005515 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Chuck Leverde734832012-07-11 16:30:50 -04005517 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005518 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005519
5520 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5521 status = nfs4_init_uniform_client_string(clp);
5522 else
Jeff Laytona3192682015-06-09 19:43:59 -04005523 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005524
5525 if (status)
5526 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005527
Chuck Leverde734832012-07-11 16:30:50 -04005528 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005529 setclientid.sc_netid_len =
5530 nfs4_init_callback_netid(clp,
5531 setclientid.sc_netid,
5532 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005533 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005534 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 clp->cl_ipaddr, port >> 8, port & 255);
5536
Jeff Layton3a6bb732015-06-09 19:43:57 -04005537 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005538 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005539 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005540 task = rpc_run_task(&task_setup_data);
5541 if (IS_ERR(task)) {
5542 status = PTR_ERR(task);
5543 goto out;
5544 }
5545 status = task->tk_status;
5546 if (setclientid.sc_cred) {
5547 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5548 put_rpccred(setclientid.sc_cred);
5549 }
5550 rpc_put_task(task);
5551out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005552 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005553 dprintk("NFS reply setclientid: %d\n", status);
5554 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
5556
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005557/**
5558 * nfs4_proc_setclientid_confirm - Confirm client ID
5559 * @clp: state data structure
5560 * @res: result of a previous SETCLIENTID
5561 * @cred: RPC credential to use for this call
5562 *
5563 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5564 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005565int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005566 struct nfs4_setclientid_res *arg,
5567 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 struct rpc_message msg = {
5570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005571 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005572 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 int status;
5575
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005576 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5577 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5578 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005579 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005580 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005581 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 return status;
5583}
5584
Trond Myklebustfe650402006-01-03 09:55:18 +01005585struct nfs4_delegreturndata {
5586 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005587 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005588 struct nfs_fh fh;
5589 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005590 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005591 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005592 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005593 struct inode *inode;
5594 bool roc;
5595 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005596};
5597
Trond Myklebustfe650402006-01-03 09:55:18 +01005598static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5599{
5600 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005601
Trond Myklebust14516c32010-07-31 14:29:06 -04005602 if (!nfs4_sequence_done(task, &data->res.seq_res))
5603 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005604
Trond Myklebustca8acf82013-08-13 10:36:56 -04005605 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005606 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005607 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005608 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005609 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005610 case -NFS4ERR_ADMIN_REVOKED:
5611 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005612 case -NFS4ERR_EXPIRED:
5613 nfs4_free_revoked_stateid(data->res.server,
5614 data->args.stateid,
5615 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005616 case -NFS4ERR_BAD_STATEID:
5617 case -NFS4ERR_OLD_STATEID:
5618 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005619 task->tk_status = 0;
5620 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005621 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005622 if (nfs4_async_handle_error(task, data->res.server,
5623 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005624 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005625 return;
5626 }
5627 }
5628 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005629 if (data->roc && data->rpc_status == 0)
5630 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005631}
5632
5633static void nfs4_delegreturn_release(void *calldata)
5634{
Peng Tao039b7562014-07-03 13:05:02 +08005635 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005636 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005637
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005638 if (inode) {
5639 if (data->roc)
5640 pnfs_roc_release(inode);
5641 nfs_iput_and_deactive(inode);
5642 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005643 kfree(calldata);
5644}
5645
Andy Adamson938e1012009-04-01 09:22:28 -04005646static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5647{
5648 struct nfs4_delegreturndata *d_data;
5649
5650 d_data = (struct nfs4_delegreturndata *)data;
5651
Peng Tao500d7012015-09-22 11:35:22 +08005652 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5653 return;
5654
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005655 if (d_data->roc)
5656 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005657
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005658 nfs4_setup_sequence(d_data->res.server,
5659 &d_data->args.seq_args,
5660 &d_data->res.seq_res,
5661 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005662}
Andy Adamson938e1012009-04-01 09:22:28 -04005663
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005664static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005665 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005666 .rpc_call_done = nfs4_delegreturn_done,
5667 .rpc_release = nfs4_delegreturn_release,
5668};
5669
Trond Myklebuste6f81072008-01-24 18:14:34 -05005670static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005671{
5672 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005673 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005674 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005675 struct rpc_message msg = {
5676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5677 .rpc_cred = cred,
5678 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005679 struct rpc_task_setup task_setup_data = {
5680 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005681 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005682 .callback_ops = &nfs4_delegreturn_ops,
5683 .flags = RPC_TASK_ASYNC,
5684 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005685 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005686
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005687 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005688 if (data == NULL)
5689 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005690 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005691
5692 nfs4_state_protect(server->nfs_client,
5693 NFS_SP4_MACH_CRED_CLEANUP,
5694 &task_setup_data.rpc_client, &msg);
5695
Trond Myklebustfe650402006-01-03 09:55:18 +01005696 data->args.fhandle = &data->fh;
5697 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005698 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005699 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005700 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005701 data->res.fattr = &data->fattr;
5702 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005703 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005704 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005705 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005706 data->inode = nfs_igrab_and_active(inode);
5707 if (data->inode)
5708 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005709
Trond Myklebustc970aa82007-07-14 15:39:59 -04005710 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005711 msg.rpc_argp = &data->args;
5712 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005713 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005714 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005715 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005716 if (!issync)
5717 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005718 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005719 if (status != 0)
5720 goto out;
5721 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005722 if (status == 0)
5723 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5724 else
5725 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005726out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005727 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005728 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729}
5730
Trond Myklebuste6f81072008-01-24 18:14:34 -05005731int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732{
5733 struct nfs_server *server = NFS_SERVER(inode);
5734 struct nfs4_exception exception = { };
5735 int err;
5736 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005737 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005738 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 switch (err) {
5740 case -NFS4ERR_STALE_STATEID:
5741 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 case 0:
5743 return 0;
5744 }
5745 err = nfs4_handle_exception(server, err, &exception);
5746 } while (exception.retry);
5747 return err;
5748}
5749
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5751{
5752 struct inode *inode = state->inode;
5753 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005754 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005755 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005757 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005759 struct nfs_lockt_res res = {
5760 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 };
5762 struct rpc_message msg = {
5763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5764 .rpc_argp = &arg,
5765 .rpc_resp = &res,
5766 .rpc_cred = state->owner->so_cred,
5767 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 struct nfs4_lock_state *lsp;
5769 int status;
5770
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005771 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005772 status = nfs4_set_lock_state(state, request);
5773 if (status != 0)
5774 goto out;
5775 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005776 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005777 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005778 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005779 switch (status) {
5780 case 0:
5781 request->fl_type = F_UNLCK;
5782 break;
5783 case -NFS4ERR_DENIED:
5784 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005786 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005787 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005788out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 return status;
5790}
5791
5792static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5793{
5794 struct nfs4_exception exception = { };
5795 int err;
5796
5797 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005798 err = _nfs4_proc_getlk(state, cmd, request);
5799 trace_nfs4_get_lock(request, state, cmd, err);
5800 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 &exception);
5802 } while (exception.retry);
5803 return err;
5804}
5805
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005806struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005807 struct nfs_locku_args arg;
5808 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005809 struct nfs4_lock_state *lsp;
5810 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005811 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005812 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005813 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005814};
5815
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005816static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5817 struct nfs_open_context *ctx,
5818 struct nfs4_lock_state *lsp,
5819 struct nfs_seqid *seqid)
5820{
5821 struct nfs4_unlockdata *p;
5822 struct inode *inode = lsp->ls_state->inode;
5823
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005824 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005825 if (p == NULL)
5826 return NULL;
5827 p->arg.fh = NFS_FH(inode);
5828 p->arg.fl = &p->fl;
5829 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005830 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005831 p->lsp = lsp;
5832 atomic_inc(&lsp->ls_count);
5833 /* Ensure we don't close file until we're done freeing locks! */
5834 p->ctx = get_nfs_open_context(ctx);
5835 memcpy(&p->fl, fl, sizeof(p->fl));
5836 p->server = NFS_SERVER(inode);
5837 return p;
5838}
5839
Trond Myklebust06f814a2006-01-03 09:55:07 +01005840static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005841{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005842 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005843 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005844 nfs4_put_lock_state(calldata->lsp);
5845 put_nfs_open_context(calldata->ctx);
5846 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005847}
5848
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005849static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005850{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005851 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005852
Trond Myklebust14516c32010-07-31 14:29:06 -04005853 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5854 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005855 switch (task->tk_status) {
5856 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005857 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005858 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005859 if (nfs4_update_lock_stateid(calldata->lsp,
5860 &calldata->res.stateid))
5861 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005862 case -NFS4ERR_ADMIN_REVOKED:
5863 case -NFS4ERR_EXPIRED:
5864 nfs4_free_revoked_stateid(calldata->server,
5865 &calldata->arg.stateid,
5866 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005867 case -NFS4ERR_BAD_STATEID:
5868 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005869 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005870 if (!nfs4_stateid_match(&calldata->arg.stateid,
5871 &calldata->lsp->ls_stateid))
5872 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005873 break;
5874 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005875 if (nfs4_async_handle_error(task, calldata->server,
5876 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005877 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005878 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005879 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005880}
5881
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005882static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005883{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005884 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005886 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005887 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005888 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005889 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005890 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005891 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005892 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005893 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005894 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005895 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005896 &calldata->res.seq_res,
5897 task) != 0)
5898 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005899 return;
5900out_no_action:
5901 task->tk_action = NULL;
5902out_wait:
5903 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904}
5905
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005906static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005907 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005908 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005909 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005910};
5911
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005912static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5913 struct nfs_open_context *ctx,
5914 struct nfs4_lock_state *lsp,
5915 struct nfs_seqid *seqid)
5916{
5917 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005918 struct rpc_message msg = {
5919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5920 .rpc_cred = ctx->cred,
5921 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005922 struct rpc_task_setup task_setup_data = {
5923 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005924 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005925 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005926 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005927 .flags = RPC_TASK_ASYNC,
5928 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005929
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005930 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5931 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5932
Frank Filz137d6ac2007-07-09 15:32:29 -07005933 /* Ensure this is an unlock - when canceling a lock, the
5934 * canceled lock is passed in, and it won't be an unlock.
5935 */
5936 fl->fl_type = F_UNLCK;
5937
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005938 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5939 if (data == NULL) {
5940 nfs_free_seqid(seqid);
5941 return ERR_PTR(-ENOMEM);
5942 }
5943
Chuck Levera9c92d62013-08-09 12:48:18 -04005944 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005945 msg.rpc_argp = &data->arg;
5946 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005947 task_setup_data.callback_data = data;
5948 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005949}
5950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5952{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005953 struct inode *inode = state->inode;
5954 struct nfs4_state_owner *sp = state->owner;
5955 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005956 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005957 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005958 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005959 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005960 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005961 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
Trond Myklebust9b073572006-06-29 16:38:34 -04005963 status = nfs4_set_lock_state(state, request);
5964 /* Unlock _before_ we do the RPC call */
5965 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005966 /* Exclude nfs_delegation_claim_locks() */
5967 mutex_lock(&sp->so_delegreturn_mutex);
5968 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005969 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005970 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005971 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005972 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005973 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005974 }
5975 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005976 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005977 if (status != 0)
5978 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005979 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005980 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005981 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5982 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005983 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5984 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005985 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005986 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005987 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005988 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005989 status = PTR_ERR(task);
5990 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005991 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005992 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005993 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005994out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005995 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005996 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005997 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998}
5999
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000struct nfs4_lockdata {
6001 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006002 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003 struct nfs4_lock_state *lsp;
6004 struct nfs_open_context *ctx;
6005 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006006 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006007 int rpc_status;
6008 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006009 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010};
6011
6012static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006013 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6014 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006015{
6016 struct nfs4_lockdata *p;
6017 struct inode *inode = lsp->ls_state->inode;
6018 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006019 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006021 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006022 if (p == NULL)
6023 return NULL;
6024
6025 p->arg.fh = NFS_FH(inode);
6026 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006027 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006028 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006029 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006030 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6031 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006032 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006033 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006034 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006035 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006036 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006037 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006038 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006039 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006040 atomic_inc(&lsp->ls_count);
6041 p->ctx = get_nfs_open_context(ctx);
6042 memcpy(&p->fl, fl, sizeof(p->fl));
6043 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006044out_free_seqid:
6045 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006046out_free:
6047 kfree(p);
6048 return NULL;
6049}
6050
6051static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6052{
6053 struct nfs4_lockdata *data = calldata;
6054 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055
Harvey Harrison3110ff82008-05-02 13:42:44 -07006056 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006057 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006058 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006059 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006060 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006061 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006062 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006063 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006064 nfs4_stateid_copy(&data->arg.open_stateid,
6065 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006067 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006068 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006069 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006070 nfs4_stateid_copy(&data->arg.lock_stateid,
6071 &data->lsp->ls_stateid);
6072 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006073 if (!nfs4_valid_open_stateid(state)) {
6074 data->rpc_status = -EBADF;
6075 task->tk_action = NULL;
6076 goto out_release_open_seqid;
6077 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006078 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006079 if (nfs4_setup_sequence(data->server,
6080 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006081 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006082 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006083 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006084out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006085 nfs_release_seqid(data->arg.open_seqid);
6086out_release_lock_seqid:
6087 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006088out_wait:
6089 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006090 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006091}
6092
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006093static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6094{
6095 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006096 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006097
Harvey Harrison3110ff82008-05-02 13:42:44 -07006098 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099
Trond Myklebust14516c32010-07-31 14:29:06 -04006100 if (!nfs4_sequence_done(task, &data->res.seq_res))
6101 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006102
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006103 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006104 switch (task->tk_status) {
6105 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006106 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006107 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006108 if (data->arg.new_lock) {
6109 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006110 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006111 rpc_restart_call_prepare(task);
6112 break;
6113 }
6114 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006115 if (data->arg.new_lock_owner != 0) {
6116 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6117 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6118 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6119 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6120 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006121 break;
6122 case -NFS4ERR_BAD_STATEID:
6123 case -NFS4ERR_OLD_STATEID:
6124 case -NFS4ERR_STALE_STATEID:
6125 case -NFS4ERR_EXPIRED:
6126 if (data->arg.new_lock_owner != 0) {
6127 if (!nfs4_stateid_match(&data->arg.open_stateid,
6128 &lsp->ls_state->open_stateid))
6129 rpc_restart_call_prepare(task);
6130 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6131 &lsp->ls_stateid))
6132 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006133 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006134 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006135}
6136
6137static void nfs4_lock_release(void *calldata)
6138{
6139 struct nfs4_lockdata *data = calldata;
6140
Harvey Harrison3110ff82008-05-02 13:42:44 -07006141 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006142 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006143 if (data->cancelled != 0) {
6144 struct rpc_task *task;
6145 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6146 data->arg.lock_seqid);
6147 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006148 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006149 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006150 } else
6151 nfs_free_seqid(data->arg.lock_seqid);
6152 nfs4_put_lock_state(data->lsp);
6153 put_nfs_open_context(data->ctx);
6154 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006155 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006156}
6157
6158static const struct rpc_call_ops nfs4_lock_ops = {
6159 .rpc_call_prepare = nfs4_lock_prepare,
6160 .rpc_call_done = nfs4_lock_done,
6161 .rpc_release = nfs4_lock_release,
6162};
6163
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006164static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6165{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006166 switch (error) {
6167 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006168 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006169 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006170 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006171 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006172 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006173 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006174 break;
6175 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006176 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006177 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006178 };
6179}
6180
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006181static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006182{
6183 struct nfs4_lockdata *data;
6184 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006185 struct rpc_message msg = {
6186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6187 .rpc_cred = state->owner->so_cred,
6188 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006189 struct rpc_task_setup task_setup_data = {
6190 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006191 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006192 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006193 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006194 .flags = RPC_TASK_ASYNC,
6195 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006196 int ret;
6197
Harvey Harrison3110ff82008-05-02 13:42:44 -07006198 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006199 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006200 fl->fl_u.nfs4_fl.owner,
6201 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202 if (data == NULL)
6203 return -ENOMEM;
6204 if (IS_SETLKW(cmd))
6205 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006206 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006207 msg.rpc_argp = &data->arg;
6208 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006209 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006210 if (recovery_type > NFS_LOCK_NEW) {
6211 if (recovery_type == NFS_LOCK_RECLAIM)
6212 data->arg.reclaim = NFS_LOCK_RECLAIM;
6213 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006214 } else
6215 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006216 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006217 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006218 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219 ret = nfs4_wait_for_completion_rpc_task(task);
6220 if (ret == 0) {
6221 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006222 if (ret)
6223 nfs4_handle_setlk_error(data->server, data->lsp,
6224 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006225 } else
6226 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006227 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006228 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006229 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006230 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231}
6232
6233static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6234{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006235 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006236 struct nfs4_exception exception = {
6237 .inode = state->inode,
6238 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006239 int err;
6240
6241 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006242 /* Cache the lock if possible... */
6243 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6244 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006245 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006246 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006247 break;
6248 nfs4_handle_exception(server, err, &exception);
6249 } while (exception.retry);
6250 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251}
6252
6253static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6254{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006255 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006256 struct nfs4_exception exception = {
6257 .inode = state->inode,
6258 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006259 int err;
6260
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006261 err = nfs4_set_lock_state(state, request);
6262 if (err != 0)
6263 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006264 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006265 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6266 return 0;
6267 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006268 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006269 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6270 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006271 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006272 switch (err) {
6273 default:
6274 goto out;
6275 case -NFS4ERR_GRACE:
6276 case -NFS4ERR_DELAY:
6277 nfs4_handle_exception(server, err, &exception);
6278 err = 0;
6279 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006280 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006281out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283}
6284
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006285#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006286static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6287{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006288 struct nfs4_lock_state *lsp;
6289 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006290
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006291 status = nfs4_set_lock_state(state, request);
6292 if (status != 0)
6293 return status;
6294 lsp = request->fl_u.nfs4_fl.owner;
6295 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6296 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6297 return 0;
6298 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006299 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006300}
6301#endif
6302
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6304{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006305 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006306 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006307 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006308 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309
Trond Myklebust01c3b862006-06-29 16:38:39 -04006310 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006311 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006312 if (status < 0)
6313 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006314 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006315 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006316 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006317 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006318 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006319 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006320 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006321 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006322 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006323 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006324 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006325 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006326 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006327 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006328out:
6329 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 return status;
6331}
6332
6333static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6334{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006335 struct nfs4_exception exception = {
6336 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006337 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006338 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 int err;
6340
6341 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006342 err = _nfs4_proc_setlk(state, cmd, request);
6343 if (err == -NFS4ERR_DENIED)
6344 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006346 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 } while (exception.retry);
6348 return err;
6349}
6350
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006351#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6352#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6353
6354static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006355nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6356 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006357{
6358 int status = -ERESTARTSYS;
6359 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6360
6361 while(!signalled()) {
6362 status = nfs4_proc_setlk(state, cmd, request);
6363 if ((status != -EAGAIN) || IS_SETLK(cmd))
6364 break;
6365 freezable_schedule_timeout_interruptible(timeout);
6366 timeout *= 2;
6367 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6368 status = -ERESTARTSYS;
6369 }
6370 return status;
6371}
6372
Jeff Laytona1d617d82016-09-17 18:17:39 -04006373#ifdef CONFIG_NFS_V4_1
6374struct nfs4_lock_waiter {
6375 struct task_struct *task;
6376 struct inode *inode;
6377 struct nfs_lowner *owner;
6378 bool notified;
6379};
6380
6381static int
6382nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6383{
6384 int ret;
6385 struct cb_notify_lock_args *cbnl = key;
6386 struct nfs4_lock_waiter *waiter = wait->private;
6387 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6388 *wowner = waiter->owner;
6389
6390 /* Only wake if the callback was for the same owner */
6391 if (lowner->clientid != wowner->clientid ||
6392 lowner->id != wowner->id ||
6393 lowner->s_dev != wowner->s_dev)
6394 return 0;
6395
6396 /* Make sure it's for the right inode */
6397 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6398 return 0;
6399
6400 waiter->notified = true;
6401
6402 /* override "private" so we can use default_wake_function */
6403 wait->private = waiter->task;
6404 ret = autoremove_wake_function(wait, mode, flags, key);
6405 wait->private = waiter;
6406 return ret;
6407}
6408
6409static int
6410nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6411{
6412 int status = -ERESTARTSYS;
6413 unsigned long flags;
6414 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6415 struct nfs_server *server = NFS_SERVER(state->inode);
6416 struct nfs_client *clp = server->nfs_client;
6417 wait_queue_head_t *q = &clp->cl_lock_waitq;
6418 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6419 .id = lsp->ls_seqid.owner_id,
6420 .s_dev = server->s_dev };
6421 struct nfs4_lock_waiter waiter = { .task = current,
6422 .inode = state->inode,
6423 .owner = &owner,
6424 .notified = false };
6425 wait_queue_t wait;
6426
6427 /* Don't bother with waitqueue if we don't expect a callback */
6428 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6429 return nfs4_retry_setlk_simple(state, cmd, request);
6430
6431 init_wait(&wait);
6432 wait.private = &waiter;
6433 wait.func = nfs4_wake_lock_waiter;
6434 add_wait_queue(q, &wait);
6435
6436 while(!signalled()) {
6437 status = nfs4_proc_setlk(state, cmd, request);
6438 if ((status != -EAGAIN) || IS_SETLK(cmd))
6439 break;
6440
6441 status = -ERESTARTSYS;
6442 spin_lock_irqsave(&q->lock, flags);
6443 if (waiter.notified) {
6444 spin_unlock_irqrestore(&q->lock, flags);
6445 continue;
6446 }
6447 set_current_state(TASK_INTERRUPTIBLE);
6448 spin_unlock_irqrestore(&q->lock, flags);
6449
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006450 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006451 }
6452
6453 finish_wait(q, &wait);
6454 return status;
6455}
6456#else /* !CONFIG_NFS_V4_1 */
6457static inline int
6458nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6459{
6460 return nfs4_retry_setlk_simple(state, cmd, request);
6461}
6462#endif
6463
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464static int
6465nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6466{
6467 struct nfs_open_context *ctx;
6468 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 int status;
6470
6471 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006472 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 state = ctx->state;
6474
6475 if (request->fl_start < 0 || request->fl_end < 0)
6476 return -EINVAL;
6477
Trond Myklebustd9531262009-07-21 19:22:38 -04006478 if (IS_GETLK(cmd)) {
6479 if (state != NULL)
6480 return nfs4_proc_getlk(state, F_GETLK, request);
6481 return 0;
6482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483
6484 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6485 return -EINVAL;
6486
Trond Myklebustd9531262009-07-21 19:22:38 -04006487 if (request->fl_type == F_UNLCK) {
6488 if (state != NULL)
6489 return nfs4_proc_unlck(state, cmd, request);
6490 return 0;
6491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492
Trond Myklebustd9531262009-07-21 19:22:38 -04006493 if (state == NULL)
6494 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006495
6496 if ((request->fl_flags & FL_POSIX) &&
6497 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6498 return -ENOLCK;
6499
Trond Myklebust55725512012-04-18 12:48:35 -04006500 /*
6501 * Don't rely on the VFS having checked the file open mode,
6502 * since it won't do this for flock() locks.
6503 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006504 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006505 case F_RDLCK:
6506 if (!(filp->f_mode & FMODE_READ))
6507 return -EBADF;
6508 break;
6509 case F_WRLCK:
6510 if (!(filp->f_mode & FMODE_WRITE))
6511 return -EBADF;
6512 }
6513
Jeff Layton1ea67db2016-09-17 18:17:37 -04006514 status = nfs4_set_lock_state(state, request);
6515 if (status != 0)
6516 return status;
6517
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006518 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519}
6520
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006521int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006522{
6523 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006524 int err;
6525
6526 err = nfs4_set_lock_state(state, fl);
6527 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006528 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006529 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrown70f254a2017-12-13 09:57:09 +11006530 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006531}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006532
Trond Myklebustcf470c32012-03-07 13:49:12 -05006533struct nfs_release_lockowner_data {
6534 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006535 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006536 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006537 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006538 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006539};
6540
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006541static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6542{
6543 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006544 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006545 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6546 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006547 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006548 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006549}
6550
6551static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6552{
6553 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006554 struct nfs_server *server = data->server;
6555
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006556 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006557
6558 switch (task->tk_status) {
6559 case 0:
6560 renew_lease(server, data->timestamp);
6561 break;
6562 case -NFS4ERR_STALE_CLIENTID:
6563 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006564 nfs4_schedule_lease_recovery(server->nfs_client);
6565 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006566 case -NFS4ERR_LEASE_MOVED:
6567 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006568 if (nfs4_async_handle_error(task, server,
6569 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006570 rpc_restart_call_prepare(task);
6571 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006572}
6573
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006574static void nfs4_release_lockowner_release(void *calldata)
6575{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006576 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006577 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006578 kfree(calldata);
6579}
6580
Trond Myklebust17280172012-03-11 13:11:00 -04006581static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006582 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6583 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006584 .rpc_release = nfs4_release_lockowner_release,
6585};
6586
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006587static void
6588nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006589{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006590 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006591 struct rpc_message msg = {
6592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6593 };
6594
6595 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006596 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006597
Trond Myklebustcf470c32012-03-07 13:49:12 -05006598 data = kmalloc(sizeof(*data), GFP_NOFS);
6599 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006600 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006601 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006602 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006603 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6604 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6605 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006606
Trond Myklebustcf470c32012-03-07 13:49:12 -05006607 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006608 msg.rpc_resp = &data->res;
6609 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006610 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006611}
6612
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006613#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6614
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006615static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006616 struct dentry *unused, struct inode *inode,
6617 const char *key, const void *buf,
6618 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006619{
Al Viro59301222016-05-27 10:19:30 -04006620 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006621}
6622
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006623static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006624 struct dentry *unused, struct inode *inode,
6625 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006626{
Al Virob2968212016-04-10 20:48:24 -04006627 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006628}
6629
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006630static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006631{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006632 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006633}
6634
David Quigleyc9bccef2013-05-22 12:50:45 -04006635#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006636
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006637static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006638 struct dentry *unused, struct inode *inode,
6639 const char *key, const void *buf,
6640 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006641{
6642 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006643 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006644
6645 return -EOPNOTSUPP;
6646}
6647
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006648static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006649 struct dentry *unused, struct inode *inode,
6650 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006651{
6652 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006653 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006654 return -EOPNOTSUPP;
6655}
6656
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006657static ssize_t
6658nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006659{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006660 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006661
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006662 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6663 len = security_inode_listsecurity(inode, list, list_len);
6664 if (list_len && len > list_len)
6665 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006666 }
6667 return len;
6668}
6669
6670static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6671 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006672 .get = nfs4_xattr_get_nfs4_label,
6673 .set = nfs4_xattr_set_nfs4_label,
6674};
David Quigleyc9bccef2013-05-22 12:50:45 -04006675
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006676#else
6677
6678static ssize_t
6679nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6680{
6681 return 0;
6682}
6683
6684#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006685
Andy Adamson533eb462011-06-13 18:25:56 -04006686/*
6687 * nfs_fhget will use either the mounted_on_fileid or the fileid
6688 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006689static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6690{
Andy Adamson533eb462011-06-13 18:25:56 -04006691 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6692 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6693 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006694 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006695 return;
6696
6697 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006698 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006699 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6700 fattr->nlink = 2;
6701}
6702
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006703static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6704 const struct qstr *name,
6705 struct nfs4_fs_locations *fs_locations,
6706 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006707{
6708 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006709 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006710 struct nfs4_fs_locations_arg args = {
6711 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006712 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006713 .page = page,
6714 .bitmask = bitmask,
6715 };
Benny Halevy22958462009-04-01 09:22:02 -04006716 struct nfs4_fs_locations_res res = {
6717 .fs_locations = fs_locations,
6718 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006719 struct rpc_message msg = {
6720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6721 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006722 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006723 };
6724 int status;
6725
Harvey Harrison3110ff82008-05-02 13:42:44 -07006726 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006727
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006728 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6729 bitmask[1] = nfs4_fattr_bitmap[1];
6730
Andy Adamson533eb462011-06-13 18:25:56 -04006731 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6732 * is not supported */
6733 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006734 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006735 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006736 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006737
Trond Myklebustc228fd32007-01-13 02:28:11 -05006738 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006739 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006740 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006741 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006742 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006743 return status;
6744}
6745
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006746int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6747 const struct qstr *name,
6748 struct nfs4_fs_locations *fs_locations,
6749 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006750{
6751 struct nfs4_exception exception = { };
6752 int err;
6753 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006754 err = _nfs4_proc_fs_locations(client, dir, name,
6755 fs_locations, page);
6756 trace_nfs4_get_fs_locations(dir, name, err);
6757 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006758 &exception);
6759 } while (exception.retry);
6760 return err;
6761}
6762
Chuck Leverb03d7352013-10-17 14:12:50 -04006763/*
6764 * This operation also signals the server that this client is
6765 * performing migration recovery. The server can stop returning
6766 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6767 * appended to this compound to identify the client ID which is
6768 * performing recovery.
6769 */
6770static int _nfs40_proc_get_locations(struct inode *inode,
6771 struct nfs4_fs_locations *locations,
6772 struct page *page, struct rpc_cred *cred)
6773{
6774 struct nfs_server *server = NFS_SERVER(inode);
6775 struct rpc_clnt *clnt = server->client;
6776 u32 bitmask[2] = {
6777 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6778 };
6779 struct nfs4_fs_locations_arg args = {
6780 .clientid = server->nfs_client->cl_clientid,
6781 .fh = NFS_FH(inode),
6782 .page = page,
6783 .bitmask = bitmask,
6784 .migration = 1, /* skip LOOKUP */
6785 .renew = 1, /* append RENEW */
6786 };
6787 struct nfs4_fs_locations_res res = {
6788 .fs_locations = locations,
6789 .migration = 1,
6790 .renew = 1,
6791 };
6792 struct rpc_message msg = {
6793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6794 .rpc_argp = &args,
6795 .rpc_resp = &res,
6796 .rpc_cred = cred,
6797 };
6798 unsigned long now = jiffies;
6799 int status;
6800
6801 nfs_fattr_init(&locations->fattr);
6802 locations->server = server;
6803 locations->nlocations = 0;
6804
6805 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6806 nfs4_set_sequence_privileged(&args.seq_args);
6807 status = nfs4_call_sync_sequence(clnt, server, &msg,
6808 &args.seq_args, &res.seq_res);
6809 if (status)
6810 return status;
6811
6812 renew_lease(server, now);
6813 return 0;
6814}
6815
6816#ifdef CONFIG_NFS_V4_1
6817
6818/*
6819 * This operation also signals the server that this client is
6820 * performing migration recovery. The server can stop asserting
6821 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6822 * performing this operation is identified in the SEQUENCE
6823 * operation in this compound.
6824 *
6825 * When the client supports GETATTR(fs_locations_info), it can
6826 * be plumbed in here.
6827 */
6828static int _nfs41_proc_get_locations(struct inode *inode,
6829 struct nfs4_fs_locations *locations,
6830 struct page *page, struct rpc_cred *cred)
6831{
6832 struct nfs_server *server = NFS_SERVER(inode);
6833 struct rpc_clnt *clnt = server->client;
6834 u32 bitmask[2] = {
6835 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6836 };
6837 struct nfs4_fs_locations_arg args = {
6838 .fh = NFS_FH(inode),
6839 .page = page,
6840 .bitmask = bitmask,
6841 .migration = 1, /* skip LOOKUP */
6842 };
6843 struct nfs4_fs_locations_res res = {
6844 .fs_locations = locations,
6845 .migration = 1,
6846 };
6847 struct rpc_message msg = {
6848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6849 .rpc_argp = &args,
6850 .rpc_resp = &res,
6851 .rpc_cred = cred,
6852 };
6853 int status;
6854
6855 nfs_fattr_init(&locations->fattr);
6856 locations->server = server;
6857 locations->nlocations = 0;
6858
6859 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6860 nfs4_set_sequence_privileged(&args.seq_args);
6861 status = nfs4_call_sync_sequence(clnt, server, &msg,
6862 &args.seq_args, &res.seq_res);
6863 if (status == NFS4_OK &&
6864 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6865 status = -NFS4ERR_LEASE_MOVED;
6866 return status;
6867}
6868
6869#endif /* CONFIG_NFS_V4_1 */
6870
6871/**
6872 * nfs4_proc_get_locations - discover locations for a migrated FSID
6873 * @inode: inode on FSID that is migrating
6874 * @locations: result of query
6875 * @page: buffer
6876 * @cred: credential to use for this operation
6877 *
6878 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6879 * operation failed, or a negative errno if a local error occurred.
6880 *
6881 * On success, "locations" is filled in, but if the server has
6882 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6883 * asserted.
6884 *
6885 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6886 * from this client that require migration recovery.
6887 */
6888int nfs4_proc_get_locations(struct inode *inode,
6889 struct nfs4_fs_locations *locations,
6890 struct page *page, struct rpc_cred *cred)
6891{
6892 struct nfs_server *server = NFS_SERVER(inode);
6893 struct nfs_client *clp = server->nfs_client;
6894 const struct nfs4_mig_recovery_ops *ops =
6895 clp->cl_mvops->mig_recovery_ops;
6896 struct nfs4_exception exception = { };
6897 int status;
6898
6899 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6900 (unsigned long long)server->fsid.major,
6901 (unsigned long long)server->fsid.minor,
6902 clp->cl_hostname);
6903 nfs_display_fhandle(NFS_FH(inode), __func__);
6904
6905 do {
6906 status = ops->get_locations(inode, locations, page, cred);
6907 if (status != -NFS4ERR_DELAY)
6908 break;
6909 nfs4_handle_exception(server, status, &exception);
6910 } while (exception.retry);
6911 return status;
6912}
6913
Chuck Lever44c99932013-10-17 14:13:30 -04006914/*
6915 * This operation also signals the server that this client is
6916 * performing "lease moved" recovery. The server can stop
6917 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6918 * is appended to this compound to identify the client ID which is
6919 * performing recovery.
6920 */
6921static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6922{
6923 struct nfs_server *server = NFS_SERVER(inode);
6924 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6925 struct rpc_clnt *clnt = server->client;
6926 struct nfs4_fsid_present_arg args = {
6927 .fh = NFS_FH(inode),
6928 .clientid = clp->cl_clientid,
6929 .renew = 1, /* append RENEW */
6930 };
6931 struct nfs4_fsid_present_res res = {
6932 .renew = 1,
6933 };
6934 struct rpc_message msg = {
6935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6936 .rpc_argp = &args,
6937 .rpc_resp = &res,
6938 .rpc_cred = cred,
6939 };
6940 unsigned long now = jiffies;
6941 int status;
6942
6943 res.fh = nfs_alloc_fhandle();
6944 if (res.fh == NULL)
6945 return -ENOMEM;
6946
6947 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6948 nfs4_set_sequence_privileged(&args.seq_args);
6949 status = nfs4_call_sync_sequence(clnt, server, &msg,
6950 &args.seq_args, &res.seq_res);
6951 nfs_free_fhandle(res.fh);
6952 if (status)
6953 return status;
6954
6955 do_renew_lease(clp, now);
6956 return 0;
6957}
6958
6959#ifdef CONFIG_NFS_V4_1
6960
6961/*
6962 * This operation also signals the server that this client is
6963 * performing "lease moved" recovery. The server can stop asserting
6964 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6965 * this operation is identified in the SEQUENCE operation in this
6966 * compound.
6967 */
6968static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6969{
6970 struct nfs_server *server = NFS_SERVER(inode);
6971 struct rpc_clnt *clnt = server->client;
6972 struct nfs4_fsid_present_arg args = {
6973 .fh = NFS_FH(inode),
6974 };
6975 struct nfs4_fsid_present_res res = {
6976 };
6977 struct rpc_message msg = {
6978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6979 .rpc_argp = &args,
6980 .rpc_resp = &res,
6981 .rpc_cred = cred,
6982 };
6983 int status;
6984
6985 res.fh = nfs_alloc_fhandle();
6986 if (res.fh == NULL)
6987 return -ENOMEM;
6988
6989 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6990 nfs4_set_sequence_privileged(&args.seq_args);
6991 status = nfs4_call_sync_sequence(clnt, server, &msg,
6992 &args.seq_args, &res.seq_res);
6993 nfs_free_fhandle(res.fh);
6994 if (status == NFS4_OK &&
6995 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6996 status = -NFS4ERR_LEASE_MOVED;
6997 return status;
6998}
6999
7000#endif /* CONFIG_NFS_V4_1 */
7001
7002/**
7003 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7004 * @inode: inode on FSID to check
7005 * @cred: credential to use for this operation
7006 *
7007 * Server indicates whether the FSID is present, moved, or not
7008 * recognized. This operation is necessary to clear a LEASE_MOVED
7009 * condition for this client ID.
7010 *
7011 * Returns NFS4_OK if the FSID is present on this server,
7012 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7013 * NFS4ERR code if some error occurred on the server, or a
7014 * negative errno if a local failure occurred.
7015 */
7016int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7017{
7018 struct nfs_server *server = NFS_SERVER(inode);
7019 struct nfs_client *clp = server->nfs_client;
7020 const struct nfs4_mig_recovery_ops *ops =
7021 clp->cl_mvops->mig_recovery_ops;
7022 struct nfs4_exception exception = { };
7023 int status;
7024
7025 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7026 (unsigned long long)server->fsid.major,
7027 (unsigned long long)server->fsid.minor,
7028 clp->cl_hostname);
7029 nfs_display_fhandle(NFS_FH(inode), __func__);
7030
7031 do {
7032 status = ops->fsid_present(inode, cred);
7033 if (status != -NFS4ERR_DELAY)
7034 break;
7035 nfs4_handle_exception(server, status, &exception);
7036 } while (exception.retry);
7037 return status;
7038}
7039
Andy Adamson5ec16a82013-08-08 10:57:55 -04007040/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007041 * If 'use_integrity' is true and the state managment nfs_client
7042 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7043 * and the machine credential as per RFC3530bis and RFC5661 Security
7044 * Considerations sections. Otherwise, just use the user cred with the
7045 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007046 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007047static 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 +00007048{
7049 int status;
7050 struct nfs4_secinfo_arg args = {
7051 .dir_fh = NFS_FH(dir),
7052 .name = name,
7053 };
7054 struct nfs4_secinfo_res res = {
7055 .flavors = flavors,
7056 };
7057 struct rpc_message msg = {
7058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7059 .rpc_argp = &args,
7060 .rpc_resp = &res,
7061 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007062 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007063 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007064
7065 if (use_integrity) {
7066 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007067 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7068 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007069 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007070
7071 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007072
7073 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7074 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7075
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007076 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7077 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007078 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007079
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007080 if (cred)
7081 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007082
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007083 return status;
7084}
7085
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007086int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7087 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007088{
7089 struct nfs4_exception exception = { };
7090 int err;
7091 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007092 err = -NFS4ERR_WRONGSEC;
7093
7094 /* try to use integrity protection with machine cred */
7095 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7096 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7097
7098 /*
7099 * if unable to use integrity protection, or SECINFO with
7100 * integrity protection returns NFS4ERR_WRONGSEC (which is
7101 * disallowed by spec, but exists in deployed servers) use
7102 * the current filesystem's rpc_client and the user cred.
7103 */
7104 if (err == -NFS4ERR_WRONGSEC)
7105 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7106
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007107 trace_nfs4_secinfo(dir, name, err);
7108 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007109 &exception);
7110 } while (exception.retry);
7111 return err;
7112}
7113
Andy Adamson557134a2009-04-01 09:21:53 -04007114#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007115/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007116 * Check the exchange flags returned by the server for invalid flags, having
7117 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7118 * DS flags set.
7119 */
7120static int nfs4_check_cl_exchange_flags(u32 flags)
7121{
7122 if (flags & ~EXCHGID4_FLAG_MASK_R)
7123 goto out_inval;
7124 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7125 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7126 goto out_inval;
7127 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7128 goto out_inval;
7129 return NFS_OK;
7130out_inval:
7131 return -NFS4ERR_INVAL;
7132}
7133
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007134static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007135nfs41_same_server_scope(struct nfs41_server_scope *a,
7136 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007137{
7138 if (a->server_scope_sz == b->server_scope_sz &&
7139 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7140 return true;
7141
7142 return false;
7143}
7144
Andy Adamson02a95de2016-02-05 16:08:37 -05007145static void
7146nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7147{
7148}
7149
7150static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7151 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7152};
7153
Andy Adamson357f54d2010-12-14 10:11:57 -05007154/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007155 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007156 *
7157 * The 4.1 client currently uses the same TCP connection for the
7158 * fore and backchannel.
7159 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007160static
7161int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7162 struct rpc_xprt *xprt,
7163 struct nfs_client *clp,
7164 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007165{
7166 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007167 struct nfs41_bind_conn_to_session_args args = {
7168 .client = clp,
7169 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7170 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007171 struct nfs41_bind_conn_to_session_res res;
7172 struct rpc_message msg = {
7173 .rpc_proc =
7174 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007175 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007176 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007177 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007178 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007179 struct rpc_task_setup task_setup_data = {
7180 .rpc_client = clnt,
7181 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007182 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007183 .rpc_message = &msg,
7184 .flags = RPC_TASK_TIMEOUT,
7185 };
7186 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007187
7188 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007189
Trond Myklebust71a097c2015-02-18 09:27:18 -08007190 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7191 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7192 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007193
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007194 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7195 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7196 args.dir = NFS4_CDFC4_FORE;
7197
7198 task = rpc_run_task(&task_setup_data);
7199 if (!IS_ERR(task)) {
7200 status = task->tk_status;
7201 rpc_put_task(task);
7202 } else
7203 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007204 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007205 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007206 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007207 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7208 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7209 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007210 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007211 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007212 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007213 dprintk("NFS: %s: Unexpected direction from server\n",
7214 __func__);
7215 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007216 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007217 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007218 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007219 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7220 __func__);
7221 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007222 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007223 }
7224 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007225out:
7226 dprintk("<-- %s status= %d\n", __func__, status);
7227 return status;
7228}
7229
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007230struct rpc_bind_conn_calldata {
7231 struct nfs_client *clp;
7232 struct rpc_cred *cred;
7233};
7234
7235static int
7236nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7237 struct rpc_xprt *xprt,
7238 void *calldata)
7239{
7240 struct rpc_bind_conn_calldata *p = calldata;
7241
7242 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7243}
7244
7245int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7246{
7247 struct rpc_bind_conn_calldata data = {
7248 .clp = clp,
7249 .cred = cred,
7250 };
7251 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7252 nfs4_proc_bind_conn_to_session_callback, &data);
7253}
7254
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007255/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007256 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7257 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007258 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007259static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7260 .how = SP4_MACH_CRED,
7261 .enforce.u.words = {
7262 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7263 1 << (OP_EXCHANGE_ID - 32) |
7264 1 << (OP_CREATE_SESSION - 32) |
7265 1 << (OP_DESTROY_SESSION - 32) |
7266 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007267 },
7268 .allow.u.words = {
7269 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007270 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007271 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007272 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007273 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007274 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007275 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007276 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007277 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007278 1 << (OP_FREE_STATEID - 32) |
7279 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007280 }
7281};
7282
7283/*
7284 * Select the state protection mode for client `clp' given the server results
7285 * from exchange_id in `sp'.
7286 *
7287 * Returns 0 on success, negative errno otherwise.
7288 */
7289static int nfs4_sp4_select_mode(struct nfs_client *clp,
7290 struct nfs41_state_protection *sp)
7291{
7292 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7293 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7294 1 << (OP_EXCHANGE_ID - 32) |
7295 1 << (OP_CREATE_SESSION - 32) |
7296 1 << (OP_DESTROY_SESSION - 32) |
7297 1 << (OP_DESTROY_CLIENTID - 32)
7298 };
7299 unsigned int i;
7300
7301 if (sp->how == SP4_MACH_CRED) {
7302 /* Print state protect result */
7303 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7304 for (i = 0; i <= LAST_NFS4_OP; i++) {
7305 if (test_bit(i, sp->enforce.u.longs))
7306 dfprintk(MOUNT, " enforce op %d\n", i);
7307 if (test_bit(i, sp->allow.u.longs))
7308 dfprintk(MOUNT, " allow op %d\n", i);
7309 }
7310
7311 /* make sure nothing is on enforce list that isn't supported */
7312 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7313 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7314 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7315 return -EINVAL;
7316 }
7317 }
7318
7319 /*
7320 * Minimal mode - state operations are allowed to use machine
7321 * credential. Note this already happens by default, so the
7322 * client doesn't have to do anything more than the negotiation.
7323 *
7324 * NOTE: we don't care if EXCHANGE_ID is in the list -
7325 * we're already using the machine cred for exchange_id
7326 * and will never use a different cred.
7327 */
7328 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7329 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7330 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7331 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7332 dfprintk(MOUNT, "sp4_mach_cred:\n");
7333 dfprintk(MOUNT, " minimal mode enabled\n");
7334 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7335 } else {
7336 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7337 return -EINVAL;
7338 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007339
7340 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007341 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7342 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007343 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7344 dfprintk(MOUNT, " cleanup mode enabled\n");
7345 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7346 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007347
Andrew Elble99ade3c2015-12-02 09:39:51 -05007348 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7349 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7350 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7351 &clp->cl_sp4_flags);
7352 }
7353
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007354 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7355 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7356 dfprintk(MOUNT, " secinfo mode enabled\n");
7357 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7358 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007359
7360 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7361 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7362 dfprintk(MOUNT, " stateid mode enabled\n");
7363 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7364 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007365
7366 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7367 dfprintk(MOUNT, " write mode enabled\n");
7368 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7369 }
7370
7371 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7372 dfprintk(MOUNT, " commit mode enabled\n");
7373 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7374 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007375 }
7376
7377 return 0;
7378}
7379
Andy Adamson8d89bd72016-09-09 09:22:18 -04007380struct nfs41_exchange_id_data {
7381 struct nfs41_exchange_id_res res;
7382 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007383 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007384 int rpc_status;
7385};
7386
7387static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007388{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007389 struct nfs41_exchange_id_data *cdata =
7390 (struct nfs41_exchange_id_data *)data;
7391 struct nfs_client *clp = cdata->args.client;
7392 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007393
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007394 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007395
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007396 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007397 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007398
7399 if (cdata->xprt && status == 0) {
7400 status = nfs4_detect_session_trunking(clp, &cdata->res,
7401 cdata->xprt);
7402 goto out;
7403 }
7404
Andy Adamson8d89bd72016-09-09 09:22:18 -04007405 if (status == 0)
7406 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007407
Chuck Lever177313f2012-05-21 22:44:58 -04007408 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007409 clp->cl_clientid = cdata->res.clientid;
7410 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007411 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007413 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007414 &clp->cl_session->session_state);
7415 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007416 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007417
Chuck Leveracdeb692012-05-21 22:46:16 -04007418 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007419 clp->cl_serverowner = cdata->res.server_owner;
7420 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007421
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007422 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007423 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007424 clp->cl_implid = cdata->res.impl_id;
7425 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007426
Chuck Lever177313f2012-05-21 22:44:58 -04007427 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007428 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007429 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007430 dprintk("%s: server_scope mismatch detected\n",
7431 __func__);
7432 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007433 kfree(clp->cl_serverscope);
7434 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007435 }
7436
Chuck Lever177313f2012-05-21 22:44:58 -04007437 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007438 clp->cl_serverscope = cdata->res.server_scope;
7439 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007440 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007441 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007442 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007443 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007444 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007445out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007447 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007448}
7449
7450static void nfs4_exchange_id_release(void *data)
7451{
7452 struct nfs41_exchange_id_data *cdata =
7453 (struct nfs41_exchange_id_data *)data;
7454
Andy Adamsonad0849a2016-09-09 09:22:28 -04007455 if (cdata->xprt) {
7456 xprt_put(cdata->xprt);
7457 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7458 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007459 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007460 kfree(cdata->res.impl_id);
7461 kfree(cdata->res.server_scope);
7462 kfree(cdata->res.server_owner);
7463 kfree(cdata);
7464}
7465
7466static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7467 .rpc_call_done = nfs4_exchange_id_done,
7468 .rpc_release = nfs4_exchange_id_release,
7469};
7470
Benny Halevy99fe60d2009-04-01 09:22:29 -04007471/*
7472 * _nfs4_proc_exchange_id()
7473 *
7474 * Wrapper for EXCHANGE_ID operation.
7475 */
7476static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007477 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007478{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007479 struct rpc_message msg = {
7480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007481 .rpc_cred = cred,
7482 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 struct rpc_task_setup task_setup_data = {
7484 .rpc_client = clp->cl_rpcclient,
7485 .callback_ops = &nfs4_exchange_id_call_ops,
7486 .rpc_message = &msg,
7487 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7488 };
7489 struct nfs41_exchange_id_data *calldata;
7490 struct rpc_task *task;
7491 int status = -EIO;
7492
7493 if (!atomic_inc_not_zero(&clp->cl_count))
7494 goto out;
7495
7496 status = -ENOMEM;
7497 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7498 if (!calldata)
7499 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007500
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007501 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007502
7503 status = nfs4_init_uniform_client_string(clp);
7504 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007506
7507 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7508 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7509 clp->cl_owner_id);
7510
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7512 GFP_NOFS);
7513 status = -ENOMEM;
7514 if (unlikely(calldata->res.server_owner == NULL))
7515 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007516
Andy Adamson8d89bd72016-09-09 09:22:18 -04007517 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007518 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007519 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007520 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007521
Andy Adamson8d89bd72016-09-09 09:22:18 -04007522 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7523 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007524 goto out_server_scope;
7525
7526 switch (sp4_how) {
7527 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007528 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007529 break;
7530
7531 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007533 break;
7534
7535 default:
7536 /* unsupported! */
7537 WARN_ON_ONCE(1);
7538 status = -EINVAL;
7539 goto out_impl_id;
7540 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007541 if (xprt) {
7542 calldata->xprt = xprt;
7543 task_setup_data.rpc_xprt = xprt;
7544 task_setup_data.flags =
7545 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007546 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7547 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007548 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007549 calldata->args.client = clp;
7550#ifdef CONFIG_NFS_V4_1_MIGRATION
7551 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7552 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7553 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7554#else
7555 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7556 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7557#endif
7558 msg.rpc_argp = &calldata->args;
7559 msg.rpc_resp = &calldata->res;
7560 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007561
Andy Adamson8d89bd72016-09-09 09:22:18 -04007562 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007563 if (IS_ERR(task))
7564 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007565
Andy Adamsonad0849a2016-09-09 09:22:28 -04007566 if (!xprt) {
7567 status = rpc_wait_for_completion_task(task);
7568 if (!status)
7569 status = calldata->rpc_status;
7570 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007571 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007572
Andy Adamson8d89bd72016-09-09 09:22:18 -04007573 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007574out:
Chuck Lever177313f2012-05-21 22:44:58 -04007575 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007576 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007577 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007578 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007579 clp->cl_implid->date.seconds,
7580 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007581 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007582 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007583
7584out_impl_id:
7585 kfree(calldata->res.impl_id);
7586out_server_scope:
7587 kfree(calldata->res.server_scope);
7588out_server_owner:
7589 kfree(calldata->res.server_owner);
7590out_calldata:
7591 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007592 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007593 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007594}
7595
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007596/*
7597 * nfs4_proc_exchange_id()
7598 *
7599 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7600 *
7601 * Since the clientid has expired, all compounds using sessions
7602 * associated with the stale clientid will be returning
7603 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7604 * be in some phase of session reset.
7605 *
7606 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7607 */
7608int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7609{
7610 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7611 int status;
7612
7613 /* try SP4_MACH_CRED if krb5i/p */
7614 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7615 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007616 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007617 if (!status)
7618 return 0;
7619 }
7620
7621 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007622 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007623}
7624
Andy Adamson04fa2c62016-09-09 09:22:29 -04007625/**
7626 * nfs4_test_session_trunk
7627 *
7628 * This is an add_xprt_test() test function called from
7629 * rpc_clnt_setup_test_and_add_xprt.
7630 *
7631 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7632 * and is dereferrenced in nfs4_exchange_id_release
7633 *
7634 * Upon success, add the new transport to the rpc_clnt
7635 *
7636 * @clnt: struct rpc_clnt to get new transport
7637 * @xprt: the rpc_xprt to test
7638 * @data: call data for _nfs4_proc_exchange_id.
7639 */
7640int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7641 void *data)
7642{
7643 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7644 u32 sp4_how;
7645
7646 dprintk("--> %s try %s\n", __func__,
7647 xprt->address_strings[RPC_DISPLAY_ADDR]);
7648
7649 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7650
7651 /* Test connection for session trunking. Async exchange_id call */
7652 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7653}
7654EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7655
Trond Myklebust66245532012-05-25 17:18:09 -04007656static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7657 struct rpc_cred *cred)
7658{
7659 struct rpc_message msg = {
7660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7661 .rpc_argp = clp,
7662 .rpc_cred = cred,
7663 };
7664 int status;
7665
7666 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007667 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007668 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007669 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007670 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7671 return status;
7672}
7673
7674static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7675 struct rpc_cred *cred)
7676{
7677 unsigned int loop;
7678 int ret;
7679
7680 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7681 ret = _nfs4_proc_destroy_clientid(clp, cred);
7682 switch (ret) {
7683 case -NFS4ERR_DELAY:
7684 case -NFS4ERR_CLIENTID_BUSY:
7685 ssleep(1);
7686 break;
7687 default:
7688 return ret;
7689 }
7690 }
7691 return 0;
7692}
7693
7694int nfs4_destroy_clientid(struct nfs_client *clp)
7695{
7696 struct rpc_cred *cred;
7697 int ret = 0;
7698
7699 if (clp->cl_mvops->minor_version < 1)
7700 goto out;
7701 if (clp->cl_exchange_flags == 0)
7702 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007703 if (clp->cl_preserve_clid)
7704 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007705 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007706 ret = nfs4_proc_destroy_clientid(clp, cred);
7707 if (cred)
7708 put_rpccred(cred);
7709 switch (ret) {
7710 case 0:
7711 case -NFS4ERR_STALE_CLIENTID:
7712 clp->cl_exchange_flags = 0;
7713 }
7714out:
7715 return ret;
7716}
7717
Andy Adamson2050f0c2009-04-01 09:22:30 -04007718struct nfs4_get_lease_time_data {
7719 struct nfs4_get_lease_time_args *args;
7720 struct nfs4_get_lease_time_res *res;
7721 struct nfs_client *clp;
7722};
7723
7724static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7725 void *calldata)
7726{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007727 struct nfs4_get_lease_time_data *data =
7728 (struct nfs4_get_lease_time_data *)calldata;
7729
7730 dprintk("--> %s\n", __func__);
7731 /* just setup sequence, do not trigger session recovery
7732 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007733 nfs41_setup_sequence(data->clp->cl_session,
7734 &data->args->la_seq_args,
7735 &data->res->lr_seq_res,
7736 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007737 dprintk("<-- %s\n", __func__);
7738}
7739
7740/*
7741 * Called from nfs4_state_manager thread for session setup, so don't recover
7742 * from sequence operation or clientid errors.
7743 */
7744static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7745{
7746 struct nfs4_get_lease_time_data *data =
7747 (struct nfs4_get_lease_time_data *)calldata;
7748
7749 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007750 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7751 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007752 switch (task->tk_status) {
7753 case -NFS4ERR_DELAY:
7754 case -NFS4ERR_GRACE:
7755 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7756 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7757 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007758 /* fall through */
7759 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007760 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007761 return;
7762 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007763 dprintk("<-- %s\n", __func__);
7764}
7765
Trond Myklebust17280172012-03-11 13:11:00 -04007766static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007767 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7768 .rpc_call_done = nfs4_get_lease_time_done,
7769};
7770
7771int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7772{
7773 struct rpc_task *task;
7774 struct nfs4_get_lease_time_args args;
7775 struct nfs4_get_lease_time_res res = {
7776 .lr_fsinfo = fsinfo,
7777 };
7778 struct nfs4_get_lease_time_data data = {
7779 .args = &args,
7780 .res = &res,
7781 .clp = clp,
7782 };
7783 struct rpc_message msg = {
7784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7785 .rpc_argp = &args,
7786 .rpc_resp = &res,
7787 };
7788 struct rpc_task_setup task_setup = {
7789 .rpc_client = clp->cl_rpcclient,
7790 .rpc_message = &msg,
7791 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007792 .callback_data = &data,
7793 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007794 };
7795 int status;
7796
Chuck Levera9c92d62013-08-09 12:48:18 -04007797 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007798 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007799 dprintk("--> %s\n", __func__);
7800 task = rpc_run_task(&task_setup);
7801
7802 if (IS_ERR(task))
7803 status = PTR_ERR(task);
7804 else {
7805 status = task->tk_status;
7806 rpc_put_task(task);
7807 }
7808 dprintk("<-- %s return %d\n", __func__, status);
7809
7810 return status;
7811}
7812
Andy Adamsonfc931582009-04-01 09:22:31 -04007813/*
7814 * Initialize the values to be used by the client in CREATE_SESSION
7815 * If nfs4_init_session set the fore channel request and response sizes,
7816 * use them.
7817 *
7818 * Set the back channel max_resp_sz_cached to zero to force the client to
7819 * always set csa_cachethis to FALSE because the current implementation
7820 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7821 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007822static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7823 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007824{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007825 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007826 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007827
Andy Adamson18aad3d2013-06-26 12:21:49 -04007828 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7829 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7830
Andy Adamsonfc931582009-04-01 09:22:31 -04007831 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007832 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7833 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007834 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007835 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007836
7837 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007838 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007839 __func__,
7840 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007841 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007842
7843 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007844 args->bc_attrs.max_rqst_sz = max_bc_payload;
7845 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007846 args->bc_attrs.max_resp_sz_cached = 0;
7847 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007848 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007849
7850 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7851 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7852 __func__,
7853 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7854 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7855 args->bc_attrs.max_reqs);
7856}
7857
Trond Myklebust79969dd2015-02-18 11:30:18 -08007858static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7859 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007860{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007861 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007862 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007863
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007864 if (rcvd->max_resp_sz > sent->max_resp_sz)
7865 return -EINVAL;
7866 /*
7867 * Our requested max_ops is the minimum we need; we're not
7868 * prepared to break up compounds into smaller pieces than that.
7869 * So, no point even trying to continue if the server won't
7870 * cooperate:
7871 */
7872 if (rcvd->max_ops < sent->max_ops)
7873 return -EINVAL;
7874 if (rcvd->max_reqs == 0)
7875 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007876 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7877 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007878 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007879}
7880
Trond Myklebust79969dd2015-02-18 11:30:18 -08007881static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7882 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007883{
7884 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007885 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007886
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007887 if (!(res->flags & SESSION4_BACK_CHAN))
7888 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007889 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7890 return -EINVAL;
7891 if (rcvd->max_resp_sz < sent->max_resp_sz)
7892 return -EINVAL;
7893 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7894 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007895 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007896 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007897 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007898 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007899out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007900 return 0;
7901}
Andy Adamson8d353012009-04-01 09:22:32 -04007902
Andy Adamson8d353012009-04-01 09:22:32 -04007903static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007904 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007905{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007906 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007907
Trond Myklebust79969dd2015-02-18 11:30:18 -08007908 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007909 if (ret)
7910 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007911 return nfs4_verify_back_channel_attrs(args, res);
7912}
7913
7914static void nfs4_update_session(struct nfs4_session *session,
7915 struct nfs41_create_session_res *res)
7916{
7917 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007918 /* Mark client id and session as being confirmed */
7919 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7920 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007921 session->flags = res->flags;
7922 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007923 if (res->flags & SESSION4_BACK_CHAN)
7924 memcpy(&session->bc_attrs, &res->bc_attrs,
7925 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007926}
7927
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007928static int _nfs4_proc_create_session(struct nfs_client *clp,
7929 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007930{
7931 struct nfs4_session *session = clp->cl_session;
7932 struct nfs41_create_session_args args = {
7933 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007934 .clientid = clp->cl_clientid,
7935 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007936 .cb_program = NFS4_CALLBACK,
7937 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007938 struct nfs41_create_session_res res;
7939
Andy Adamsonfc931582009-04-01 09:22:31 -04007940 struct rpc_message msg = {
7941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7942 .rpc_argp = &args,
7943 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007944 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007945 };
7946 int status;
7947
Chuck Lever6b26cc82016-05-02 14:40:40 -04007948 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007949 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007950
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007951 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007952 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007953
Trond Myklebustb519d402016-09-11 14:50:01 -04007954 switch (status) {
7955 case -NFS4ERR_STALE_CLIENTID:
7956 case -NFS4ERR_DELAY:
7957 case -ETIMEDOUT:
7958 case -EACCES:
7959 case -EAGAIN:
7960 goto out;
7961 };
7962
7963 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007964 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007965 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007966 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007967 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007968 if (status)
7969 goto out;
7970 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007971 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007972out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007973 return status;
7974}
7975
7976/*
7977 * Issues a CREATE_SESSION operation to the server.
7978 * It is the responsibility of the caller to verify the session is
7979 * expired before calling this routine.
7980 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007981int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007982{
7983 int status;
7984 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007985 struct nfs4_session *session = clp->cl_session;
7986
7987 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7988
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007989 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007990 if (status)
7991 goto out;
7992
Andy Adamsonaacd5532011-11-09 13:58:21 -05007993 /* Init or reset the session slot tables */
7994 status = nfs4_setup_session_slot_tables(session);
7995 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007996 if (status)
7997 goto out;
7998
7999 ptr = (unsigned *)&session->sess_id.data[0];
8000 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8001 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008002out:
8003 dprintk("<-- %s\n", __func__);
8004 return status;
8005}
8006
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008007/*
8008 * Issue the over-the-wire RPC DESTROY_SESSION.
8009 * The caller must serialize access to this routine.
8010 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008011int nfs4_proc_destroy_session(struct nfs4_session *session,
8012 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008013{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008014 struct rpc_message msg = {
8015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8016 .rpc_argp = session,
8017 .rpc_cred = cred,
8018 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008019 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008020
8021 dprintk("--> nfs4_proc_destroy_session\n");
8022
8023 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008024 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8025 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008026
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008027 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008028 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008029
8030 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008031 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008032 "Session has been destroyed regardless...\n", status);
8033
8034 dprintk("<-- nfs4_proc_destroy_session\n");
8035 return status;
8036}
8037
Trond Myklebust7b38c362012-05-23 13:23:31 -04008038/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008039 * Renew the cl_session lease.
8040 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008041struct nfs4_sequence_data {
8042 struct nfs_client *clp;
8043 struct nfs4_sequence_args args;
8044 struct nfs4_sequence_res res;
8045};
8046
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008047static void nfs41_sequence_release(void *data)
8048{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008049 struct nfs4_sequence_data *calldata = data;
8050 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008051
Alexandros Batsakis71358402010-02-05 03:45:05 -08008052 if (atomic_read(&clp->cl_count) > 1)
8053 nfs4_schedule_state_renewal(clp);
8054 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008055 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008056}
8057
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008058static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8059{
8060 switch(task->tk_status) {
8061 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008062 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8063 return -EAGAIN;
8064 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008065 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008066 }
8067 return 0;
8068}
8069
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008070static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008071{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008072 struct nfs4_sequence_data *calldata = data;
8073 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074
Trond Myklebust14516c32010-07-31 14:29:06 -04008075 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8076 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008078 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008079 if (task->tk_status < 0) {
8080 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008081 if (atomic_read(&clp->cl_count) == 1)
8082 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008083
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008084 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8085 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086 return;
8087 }
8088 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008090out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008091 dprintk("<-- %s\n", __func__);
8092}
8093
8094static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8095{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008096 struct nfs4_sequence_data *calldata = data;
8097 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008098 struct nfs4_sequence_args *args;
8099 struct nfs4_sequence_res *res;
8100
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008101 args = task->tk_msg.rpc_argp;
8102 res = task->tk_msg.rpc_resp;
8103
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008104 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008105}
8106
8107static const struct rpc_call_ops nfs41_sequence_ops = {
8108 .rpc_call_done = nfs41_sequence_call_done,
8109 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008110 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008111};
8112
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008113static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8114 struct rpc_cred *cred,
8115 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008116{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008117 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008118 struct rpc_message msg = {
8119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8120 .rpc_cred = cred,
8121 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008122 struct rpc_task_setup task_setup_data = {
8123 .rpc_client = clp->cl_rpcclient,
8124 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008125 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008126 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008127 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008128
Alexandros Batsakis71358402010-02-05 03:45:05 -08008129 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008130 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008131 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008132 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008133 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008134 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008135 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008136 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008137 if (is_privileged)
8138 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008139 msg.rpc_argp = &calldata->args;
8140 msg.rpc_resp = &calldata->res;
8141 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008142 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008143
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008144 return rpc_run_task(&task_setup_data);
8145}
8146
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008147static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008148{
8149 struct rpc_task *task;
8150 int ret = 0;
8151
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008152 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008153 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008154 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008155 if (IS_ERR(task))
8156 ret = PTR_ERR(task);
8157 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008158 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008159 dprintk("<-- %s status=%d\n", __func__, ret);
8160 return ret;
8161}
8162
8163static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8164{
8165 struct rpc_task *task;
8166 int ret;
8167
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008168 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008169 if (IS_ERR(task)) {
8170 ret = PTR_ERR(task);
8171 goto out;
8172 }
8173 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008174 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008175 ret = task->tk_status;
8176 rpc_put_task(task);
8177out:
8178 dprintk("<-- %s status=%d\n", __func__, ret);
8179 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008180}
8181
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008182struct nfs4_reclaim_complete_data {
8183 struct nfs_client *clp;
8184 struct nfs41_reclaim_complete_args arg;
8185 struct nfs41_reclaim_complete_res res;
8186};
8187
8188static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8189{
8190 struct nfs4_reclaim_complete_data *calldata = data;
8191
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008192 nfs41_setup_sequence(calldata->clp->cl_session,
8193 &calldata->arg.seq_args,
8194 &calldata->res.seq_res,
8195 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008196}
8197
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008198static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8199{
8200 switch(task->tk_status) {
8201 case 0:
8202 case -NFS4ERR_COMPLETE_ALREADY:
8203 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8204 break;
8205 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008206 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008207 /* fall through */
8208 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008209 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008210 case -NFS4ERR_BADSESSION:
8211 case -NFS4ERR_DEADSESSION:
8212 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8213 nfs4_schedule_session_recovery(clp->cl_session,
8214 task->tk_status);
8215 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008216 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008217 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008218 }
8219 return 0;
8220}
8221
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008222static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8223{
8224 struct nfs4_reclaim_complete_data *calldata = data;
8225 struct nfs_client *clp = calldata->clp;
8226 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8227
8228 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008229 if (!nfs41_sequence_done(task, res))
8230 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008231
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008232 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008233 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8234 rpc_restart_call_prepare(task);
8235 return;
8236 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008237 dprintk("<-- %s\n", __func__);
8238}
8239
8240static void nfs4_free_reclaim_complete_data(void *data)
8241{
8242 struct nfs4_reclaim_complete_data *calldata = data;
8243
8244 kfree(calldata);
8245}
8246
8247static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8248 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8249 .rpc_call_done = nfs4_reclaim_complete_done,
8250 .rpc_release = nfs4_free_reclaim_complete_data,
8251};
8252
8253/*
8254 * Issue a global reclaim complete.
8255 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008256static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8257 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008258{
8259 struct nfs4_reclaim_complete_data *calldata;
8260 struct rpc_task *task;
8261 struct rpc_message msg = {
8262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008263 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008264 };
8265 struct rpc_task_setup task_setup_data = {
8266 .rpc_client = clp->cl_rpcclient,
8267 .rpc_message = &msg,
8268 .callback_ops = &nfs4_reclaim_complete_call_ops,
8269 .flags = RPC_TASK_ASYNC,
8270 };
8271 int status = -ENOMEM;
8272
8273 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008274 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008275 if (calldata == NULL)
8276 goto out;
8277 calldata->clp = clp;
8278 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008279
Chuck Levera9c92d62013-08-09 12:48:18 -04008280 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008281 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282 msg.rpc_argp = &calldata->arg;
8283 msg.rpc_resp = &calldata->res;
8284 task_setup_data.callback_data = calldata;
8285 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008286 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008287 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008288 goto out;
8289 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008290 status = nfs4_wait_for_completion_rpc_task(task);
8291 if (status == 0)
8292 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008293 rpc_put_task(task);
8294out:
8295 dprintk("<-- %s status=%d\n", __func__, status);
8296 return status;
8297}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008298
8299static void
8300nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8301{
8302 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008303 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008304 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008305
8306 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008307 nfs41_setup_sequence(session, &lgp->args.seq_args,
8308 &lgp->res.seq_res, task);
8309 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008310}
8311
8312static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8313{
8314 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008315
8316 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008317 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008318 dprintk("<-- %s\n", __func__);
8319}
8320
8321static int
8322nfs4_layoutget_handle_exception(struct rpc_task *task,
8323 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8324{
Trond Myklebustee314c22012-10-01 17:25:48 -07008325 struct inode *inode = lgp->args.inode;
8326 struct nfs_server *server = NFS_SERVER(inode);
8327 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008328 int nfs4err = task->tk_status;
8329 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008330 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008331
Boaz Harroshed7e5422014-01-22 20:34:54 +02008332 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008333
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008334 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008335 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008336 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008337
8338 /*
8339 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8340 * on the file. set tk_status to -ENODATA to tell upper layer to
8341 * retry go inband.
8342 */
8343 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008344 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008345 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008346 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008347 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8348 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8349 */
8350 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008351 status = -EOVERFLOW;
8352 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008353 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008354 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008355 * (or clients) writing to the same RAID stripe except when
8356 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008357 *
8358 * Treat it like we would RECALLCONFLICT -- we retry for a little
8359 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008360 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008361 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008362 if (lgp->args.minlength == 0) {
8363 status = -EOVERFLOW;
8364 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008365 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008366 status = -EBUSY;
8367 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008368 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008369 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008370 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008371 case -NFS4ERR_DELEG_REVOKED:
8372 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008373 case -NFS4ERR_EXPIRED:
8374 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008375 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008376 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008377 lo = NFS_I(inode)->layout;
8378 /* If the open stateid was bad, then recover it. */
8379 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8380 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008381 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008382 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008383 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008384 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008385 break;
8386 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008387
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008388 /*
8389 * Mark the bad layout state as invalid, then retry
8390 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008391 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008392 spin_unlock(&inode->i_lock);
8393 pnfs_free_lseg_list(&head);
8394 status = -EAGAIN;
8395 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008396 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008397
Trond Myklebust0a702352017-01-23 22:44:12 -05008398 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008399 err = nfs4_handle_exception(server, nfs4err, exception);
8400 if (!status) {
8401 if (exception->retry)
8402 status = -EAGAIN;
8403 else
8404 status = err;
8405 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008406out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008407 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008408 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008409}
8410
Idan Kedar85541162012-08-02 11:47:10 +03008411static size_t max_response_pages(struct nfs_server *server)
8412{
8413 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8414 return nfs_page_array_len(0, max_resp_sz);
8415}
8416
8417static void nfs4_free_pages(struct page **pages, size_t size)
8418{
8419 int i;
8420
8421 if (!pages)
8422 return;
8423
8424 for (i = 0; i < size; i++) {
8425 if (!pages[i])
8426 break;
8427 __free_page(pages[i]);
8428 }
8429 kfree(pages);
8430}
8431
8432static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8433{
8434 struct page **pages;
8435 int i;
8436
8437 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8438 if (!pages) {
8439 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8440 return NULL;
8441 }
8442
8443 for (i = 0; i < size; i++) {
8444 pages[i] = alloc_page(gfp_flags);
8445 if (!pages[i]) {
8446 dprintk("%s: failed to allocate page\n", __func__);
8447 nfs4_free_pages(pages, size);
8448 return NULL;
8449 }
8450 }
8451
8452 return pages;
8453}
8454
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008455static void nfs4_layoutget_release(void *calldata)
8456{
8457 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008458 struct inode *inode = lgp->args.inode;
8459 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008460 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008461
8462 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008463 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008464 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008465 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008466 put_nfs_open_context(lgp->args.ctx);
8467 kfree(calldata);
8468 dprintk("<-- %s\n", __func__);
8469}
8470
8471static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8472 .rpc_call_prepare = nfs4_layoutget_prepare,
8473 .rpc_call_done = nfs4_layoutget_done,
8474 .rpc_release = nfs4_layoutget_release,
8475};
8476
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008477struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008478nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008479{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008480 struct inode *inode = lgp->args.inode;
8481 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008482 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008483 struct rpc_task *task;
8484 struct rpc_message msg = {
8485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8486 .rpc_argp = &lgp->args,
8487 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008488 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008489 };
8490 struct rpc_task_setup task_setup_data = {
8491 .rpc_client = server->client,
8492 .rpc_message = &msg,
8493 .callback_ops = &nfs4_layoutget_call_ops,
8494 .callback_data = lgp,
8495 .flags = RPC_TASK_ASYNC,
8496 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008497 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008498 struct nfs4_exception exception = {
8499 .inode = inode,
8500 .timeout = *timeout,
8501 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008502 int status = 0;
8503
8504 dprintk("--> %s\n", __func__);
8505
Peng Tao4bd5a982014-11-17 11:05:17 +08008506 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8507 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8508
Idan Kedar85541162012-08-02 11:47:10 +03008509 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8510 if (!lgp->args.layout.pages) {
8511 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008512 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008513 }
8514 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8515
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008516 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008517 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008518 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008519
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008520 task = rpc_run_task(&task_setup_data);
8521 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008522 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008523 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008524 if (status == 0) {
8525 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8526 *timeout = exception.timeout;
8527 }
8528
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008529 trace_nfs4_layoutget(lgp->args.ctx,
8530 &lgp->args.range,
8531 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008532 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008533 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008534
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008535 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8536 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008537 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008538 rpc_put_task(task);
8539 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008540 if (status)
8541 return ERR_PTR(status);
8542 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008543}
8544
Benny Halevycbe82602011-05-22 19:52:37 +03008545static void
8546nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8547{
8548 struct nfs4_layoutreturn *lrp = calldata;
8549
8550 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008551 nfs41_setup_sequence(lrp->clp->cl_session,
8552 &lrp->args.seq_args,
8553 &lrp->res.seq_res,
8554 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008555}
8556
8557static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8558{
8559 struct nfs4_layoutreturn *lrp = calldata;
8560 struct nfs_server *server;
8561
8562 dprintk("--> %s\n", __func__);
8563
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008564 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008565 return;
8566
8567 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008568 switch (task->tk_status) {
8569 default:
8570 task->tk_status = 0;
8571 case 0:
8572 break;
8573 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008574 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008575 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008576 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008577 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008578 return;
8579 }
Benny Halevycbe82602011-05-22 19:52:37 +03008580 dprintk("<-- %s\n", __func__);
8581}
8582
8583static void nfs4_layoutreturn_release(void *calldata)
8584{
8585 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008586 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008587 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008588
8589 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008590 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008591 if (lrp->res.lrs_present) {
8592 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8593 &lrp->args.range,
8594 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008595 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008596 } else
8597 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008598 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008599 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008600 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008601 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008602 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008603 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008604 kfree(calldata);
8605 dprintk("<-- %s\n", __func__);
8606}
8607
8608static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8609 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8610 .rpc_call_done = nfs4_layoutreturn_done,
8611 .rpc_release = nfs4_layoutreturn_release,
8612};
8613
Peng Tao6c166052014-11-17 09:30:40 +08008614int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008615{
8616 struct rpc_task *task;
8617 struct rpc_message msg = {
8618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8619 .rpc_argp = &lrp->args,
8620 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008621 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008622 };
8623 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008624 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008625 .rpc_message = &msg,
8626 .callback_ops = &nfs4_layoutreturn_call_ops,
8627 .callback_data = lrp,
8628 };
Peng Tao6c166052014-11-17 09:30:40 +08008629 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008630
Andrew Elble99ade3c2015-12-02 09:39:51 -05008631 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8632 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8633 &task_setup_data.rpc_client, &msg);
8634
Benny Halevycbe82602011-05-22 19:52:37 +03008635 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008636 if (!sync) {
8637 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8638 if (!lrp->inode) {
8639 nfs4_layoutreturn_release(lrp);
8640 return -EAGAIN;
8641 }
8642 task_setup_data.flags |= RPC_TASK_ASYNC;
8643 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008644 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008645 task = rpc_run_task(&task_setup_data);
8646 if (IS_ERR(task))
8647 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008648 if (sync)
8649 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008650 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008651 dprintk("<-- %s status=%d\n", __func__, status);
8652 rpc_put_task(task);
8653 return status;
8654}
8655
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008656static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008657_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8658 struct pnfs_device *pdev,
8659 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008660{
8661 struct nfs4_getdeviceinfo_args args = {
8662 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008663 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8664 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008665 };
8666 struct nfs4_getdeviceinfo_res res = {
8667 .pdev = pdev,
8668 };
8669 struct rpc_message msg = {
8670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8671 .rpc_argp = &args,
8672 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008673 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008674 };
8675 int status;
8676
8677 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008678 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008679 if (res.notification & ~args.notify_types)
8680 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008681 if (res.notification != args.notify_types)
8682 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008683
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008684 dprintk("<-- %s status=%d\n", __func__, status);
8685
8686 return status;
8687}
8688
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008689int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8690 struct pnfs_device *pdev,
8691 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008692{
8693 struct nfs4_exception exception = { };
8694 int err;
8695
8696 do {
8697 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008698 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008699 &exception);
8700 } while (exception.retry);
8701 return err;
8702}
8703EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8704
Andy Adamson863a3c62011-03-23 13:27:54 +00008705static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8706{
8707 struct nfs4_layoutcommit_data *data = calldata;
8708 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008709 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008710
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008711 nfs41_setup_sequence(session,
8712 &data->args.seq_args,
8713 &data->res.seq_res,
8714 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008715}
8716
8717static void
8718nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8719{
8720 struct nfs4_layoutcommit_data *data = calldata;
8721 struct nfs_server *server = NFS_SERVER(data->args.inode);
8722
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008723 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008724 return;
8725
8726 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008727 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8728 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8729 case -NFS4ERR_BADLAYOUT: /* no layout */
8730 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008731 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008732 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008733 break;
8734 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008735 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008736 rpc_restart_call_prepare(task);
8737 return;
8738 }
8739 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008740}
8741
8742static void nfs4_layoutcommit_release(void *calldata)
8743{
8744 struct nfs4_layoutcommit_data *data = calldata;
8745
Andy Adamsondb29c082011-07-30 20:52:38 -04008746 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008747 nfs_post_op_update_inode_force_wcc(data->args.inode,
8748 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008749 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008750 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008751 kfree(data);
8752}
8753
8754static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8755 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8756 .rpc_call_done = nfs4_layoutcommit_done,
8757 .rpc_release = nfs4_layoutcommit_release,
8758};
8759
8760int
Andy Adamsonef311532011-03-12 02:58:10 -05008761nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008762{
8763 struct rpc_message msg = {
8764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8765 .rpc_argp = &data->args,
8766 .rpc_resp = &data->res,
8767 .rpc_cred = data->cred,
8768 };
8769 struct rpc_task_setup task_setup_data = {
8770 .task = &data->task,
8771 .rpc_client = NFS_CLIENT(data->args.inode),
8772 .rpc_message = &msg,
8773 .callback_ops = &nfs4_layoutcommit_ops,
8774 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008775 };
8776 struct rpc_task *task;
8777 int status = 0;
8778
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008779 dprintk("NFS: initiating layoutcommit call. sync %d "
8780 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008781 data->args.lastbytewritten,
8782 data->args.inode->i_ino);
8783
Trond Myklebust472e2592015-02-05 16:50:30 -05008784 if (!sync) {
8785 data->inode = nfs_igrab_and_active(data->args.inode);
8786 if (data->inode == NULL) {
8787 nfs4_layoutcommit_release(data);
8788 return -EAGAIN;
8789 }
8790 task_setup_data.flags = RPC_TASK_ASYNC;
8791 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008792 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008793 task = rpc_run_task(&task_setup_data);
8794 if (IS_ERR(task))
8795 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008796 if (sync)
8797 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008798 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008799 dprintk("%s: status %d\n", __func__, status);
8800 rpc_put_task(task);
8801 return status;
8802}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008803
Andy Adamson97431202013-08-08 10:57:56 -04008804/**
8805 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8806 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8807 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008808static int
8809_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008810 struct nfs_fsinfo *info,
8811 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008812{
8813 struct nfs41_secinfo_no_name_args args = {
8814 .style = SECINFO_STYLE_CURRENT_FH,
8815 };
8816 struct nfs4_secinfo_res res = {
8817 .flavors = flavors,
8818 };
8819 struct rpc_message msg = {
8820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8821 .rpc_argp = &args,
8822 .rpc_resp = &res,
8823 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008824 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008825 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008826 int status;
8827
8828 if (use_integrity) {
8829 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008830 cred = nfs4_get_clid_cred(server->nfs_client);
8831 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008832 }
8833
8834 dprintk("--> %s\n", __func__);
8835 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8836 &res.seq_res, 0);
8837 dprintk("<-- %s status=%d\n", __func__, status);
8838
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008839 if (cred)
8840 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008841
8842 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008843}
8844
8845static int
8846nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8847 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8848{
8849 struct nfs4_exception exception = { };
8850 int err;
8851 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008852 /* first try using integrity protection */
8853 err = -NFS4ERR_WRONGSEC;
8854
8855 /* try to use integrity protection with machine cred */
8856 if (_nfs4_is_integrity_protected(server->nfs_client))
8857 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8858 flavors, true);
8859
8860 /*
8861 * if unable to use integrity protection, or SECINFO with
8862 * integrity protection returns NFS4ERR_WRONGSEC (which is
8863 * disallowed by spec, but exists in deployed servers) use
8864 * the current filesystem's rpc_client and the user cred.
8865 */
8866 if (err == -NFS4ERR_WRONGSEC)
8867 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8868 flavors, false);
8869
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008870 switch (err) {
8871 case 0:
8872 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008873 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008874 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008875 default:
8876 err = nfs4_handle_exception(server, err, &exception);
8877 }
8878 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008879out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008880 return err;
8881}
8882
8883static int
8884nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8885 struct nfs_fsinfo *info)
8886{
8887 int err;
8888 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008889 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008890 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008891 struct nfs4_secinfo4 *secinfo;
8892 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008893
8894 page = alloc_page(GFP_KERNEL);
8895 if (!page) {
8896 err = -ENOMEM;
8897 goto out;
8898 }
8899
8900 flavors = page_address(page);
8901 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8902
8903 /*
8904 * Fall back on "guess and check" method if
8905 * the server doesn't support SECINFO_NO_NAME
8906 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008907 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008908 err = nfs4_find_root_sec(server, fhandle, info);
8909 goto out_freepage;
8910 }
8911 if (err)
8912 goto out_freepage;
8913
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008914 for (i = 0; i < flavors->num_flavors; i++) {
8915 secinfo = &flavors->flavors[i];
8916
8917 switch (secinfo->flavor) {
8918 case RPC_AUTH_NULL:
8919 case RPC_AUTH_UNIX:
8920 case RPC_AUTH_GSS:
8921 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8922 &secinfo->flavor_info);
8923 break;
8924 default:
8925 flavor = RPC_AUTH_MAXFLAVOR;
8926 break;
8927 }
8928
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008929 if (!nfs_auth_info_match(&server->auth_info, flavor))
8930 flavor = RPC_AUTH_MAXFLAVOR;
8931
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008932 if (flavor != RPC_AUTH_MAXFLAVOR) {
8933 err = nfs4_lookup_root_sec(server, fhandle,
8934 info, flavor);
8935 if (!err)
8936 break;
8937 }
8938 }
8939
8940 if (flavor == RPC_AUTH_MAXFLAVOR)
8941 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008942
8943out_freepage:
8944 put_page(page);
8945 if (err == -EACCES)
8946 return -EPERM;
8947out:
8948 return err;
8949}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008950
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008951static int _nfs41_test_stateid(struct nfs_server *server,
8952 nfs4_stateid *stateid,
8953 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008954{
8955 int status;
8956 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008957 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008958 };
8959 struct nfs41_test_stateid_res res;
8960 struct rpc_message msg = {
8961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8962 .rpc_argp = &args,
8963 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008964 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008965 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008966 struct rpc_clnt *rpc_client = server->client;
8967
8968 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8969 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008970
Chuck Lever38527b12012-07-11 16:30:23 -04008971 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008972 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008973 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008974 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008975 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008976 if (status != NFS_OK) {
8977 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008978 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008979 }
8980 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008981 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008982}
8983
Trond Myklebust43912bb2016-09-22 13:38:56 -04008984static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8985 int err, struct nfs4_exception *exception)
8986{
8987 exception->retry = 0;
8988 switch(err) {
8989 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008990 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008991 nfs4_handle_exception(server, err, exception);
8992 break;
8993 case -NFS4ERR_BADSESSION:
8994 case -NFS4ERR_BADSLOT:
8995 case -NFS4ERR_BAD_HIGH_SLOT:
8996 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8997 case -NFS4ERR_DEADSESSION:
8998 nfs4_do_handle_exception(server, err, exception);
8999 }
9000}
9001
Chuck Lever38527b12012-07-11 16:30:23 -04009002/**
9003 * nfs41_test_stateid - perform a TEST_STATEID operation
9004 *
9005 * @server: server / transport on which to perform the operation
9006 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009007 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009008 *
9009 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9010 * Otherwise a negative NFS4ERR value is returned if the operation
9011 * failed or the state ID is not currently valid.
9012 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009013static int nfs41_test_stateid(struct nfs_server *server,
9014 nfs4_stateid *stateid,
9015 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009016{
9017 struct nfs4_exception exception = { };
9018 int err;
9019 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009020 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009021 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009022 } while (exception.retry);
9023 return err;
9024}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009025
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009026struct nfs_free_stateid_data {
9027 struct nfs_server *server;
9028 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009029 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009030};
9031
9032static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9033{
9034 struct nfs_free_stateid_data *data = calldata;
9035 nfs41_setup_sequence(nfs4_get_session(data->server),
9036 &data->args.seq_args,
9037 &data->res.seq_res,
9038 task);
9039}
9040
9041static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9042{
9043 struct nfs_free_stateid_data *data = calldata;
9044
9045 nfs41_sequence_done(task, &data->res.seq_res);
9046
9047 switch (task->tk_status) {
9048 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009049 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009050 rpc_restart_call_prepare(task);
9051 }
9052}
9053
9054static void nfs41_free_stateid_release(void *calldata)
9055{
9056 kfree(calldata);
9057}
9058
Trond Myklebust17f26b12013-08-21 15:48:42 -04009059static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009060 .rpc_call_prepare = nfs41_free_stateid_prepare,
9061 .rpc_call_done = nfs41_free_stateid_done,
9062 .rpc_release = nfs41_free_stateid_release,
9063};
9064
9065static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009066 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009067 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009068 bool privileged)
9069{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009070 struct rpc_message msg = {
9071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009072 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009073 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009074 struct rpc_task_setup task_setup = {
9075 .rpc_client = server->client,
9076 .rpc_message = &msg,
9077 .callback_ops = &nfs41_free_stateid_ops,
9078 .flags = RPC_TASK_ASYNC,
9079 };
9080 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009081
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009082 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9083 &task_setup.rpc_client, &msg);
9084
Chuck Lever38527b12012-07-11 16:30:23 -04009085 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009086 data = kmalloc(sizeof(*data), GFP_NOFS);
9087 if (!data)
9088 return ERR_PTR(-ENOMEM);
9089 data->server = server;
9090 nfs4_stateid_copy(&data->args.stateid, stateid);
9091
9092 task_setup.callback_data = data;
9093
9094 msg.rpc_argp = &data->args;
9095 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009096 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009097 if (privileged)
9098 nfs4_set_sequence_privileged(&data->args.seq_args);
9099
9100 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009101}
9102
Chuck Lever38527b12012-07-11 16:30:23 -04009103/**
9104 * nfs41_free_stateid - perform a FREE_STATEID operation
9105 *
9106 * @server: server / transport on which to perform the operation
9107 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009108 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009109 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009110 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009111 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009112 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009113static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009114 const nfs4_stateid *stateid,
9115 struct rpc_cred *cred,
9116 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009117{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009118 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009119
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009120 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009121 if (IS_ERR(task))
9122 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009123 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009124 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009125}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009126
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009127static void
9128nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009129{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009130 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009131
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009132 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009133 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009134}
9135
Trond Myklebust36281ca2012-03-04 18:13:56 -05009136static bool nfs41_match_stateid(const nfs4_stateid *s1,
9137 const nfs4_stateid *s2)
9138{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009139 if (s1->type != s2->type)
9140 return false;
9141
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009142 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009143 return false;
9144
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009145 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009146 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009147 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009148 return true;
9149
9150 return false;
9151}
9152
Andy Adamson557134a2009-04-01 09:21:53 -04009153#endif /* CONFIG_NFS_V4_1 */
9154
Trond Myklebust36281ca2012-03-04 18:13:56 -05009155static bool nfs4_match_stateid(const nfs4_stateid *s1,
9156 const nfs4_stateid *s2)
9157{
Trond Myklebustf597c532012-03-04 18:13:56 -05009158 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009159}
9160
9161
Trond Myklebust17280172012-03-11 13:11:00 -04009162static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009163 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009164 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165 .recover_open = nfs4_open_reclaim,
9166 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009167 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009168 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009169};
9170
Andy Adamson591d71c2009-04-01 09:22:47 -04009171#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009172static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009173 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9174 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9175 .recover_open = nfs4_open_reclaim,
9176 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009177 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009178 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009179 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009180};
9181#endif /* CONFIG_NFS_V4_1 */
9182
Trond Myklebust17280172012-03-11 13:11:00 -04009183static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009184 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009185 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009186 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009188 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009189};
9190
Andy Adamson591d71c2009-04-01 09:22:47 -04009191#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009192static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009193 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9194 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009195 .recover_open = nfs41_open_expired,
9196 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009197 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009198};
9199#endif /* CONFIG_NFS_V4_1 */
9200
Trond Myklebust17280172012-03-11 13:11:00 -04009201static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009202 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009203 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009204 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009205};
9206
9207#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009208static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009209 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009210 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009211 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009212};
9213#endif
9214
Chuck Leverec011fe2013-10-17 14:12:39 -04009215static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009216 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009217 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009218};
9219
9220#if defined(CONFIG_NFS_V4_1)
9221static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009222 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009223 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009224};
9225#endif /* CONFIG_NFS_V4_1 */
9226
Trond Myklebust97dc1352010-06-16 09:52:26 -04009227static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9228 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009229 .init_caps = NFS_CAP_READDIRPLUS
9230 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009231 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009232 .init_client = nfs40_init_client,
9233 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009234 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009235 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009236 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009237 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009238 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009239 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009240 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9241 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9242 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009243 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009244};
9245
9246#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009247static struct nfs_seqid *
9248nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9249{
9250 return NULL;
9251}
9252
Trond Myklebust97dc1352010-06-16 09:52:26 -04009253static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9254 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009255 .init_caps = NFS_CAP_READDIRPLUS
9256 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009257 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009258 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009259 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009260 .init_client = nfs41_init_client,
9261 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009262 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009263 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009264 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009265 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009266 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009267 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009268 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009269 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9270 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9271 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009272 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009273};
9274#endif
9275
Steve Dickson42c2c422013-05-22 12:50:38 -04009276#if defined(CONFIG_NFS_V4_2)
9277static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9278 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009279 .init_caps = NFS_CAP_READDIRPLUS
9280 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009281 | NFS_CAP_POSIX_LOCK
9282 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009283 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009284 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009285 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009286 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009287 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009288 | NFS_CAP_LAYOUTSTATS
9289 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009290 .init_client = nfs41_init_client,
9291 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009292 .match_stateid = nfs41_match_stateid,
9293 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009294 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009295 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009296 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009297 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009298 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009299 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9300 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9301 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009302 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009303};
9304#endif
9305
Trond Myklebust97dc1352010-06-16 09:52:26 -04009306const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9307 [0] = &nfs_v4_0_minor_ops,
9308#if defined(CONFIG_NFS_V4_1)
9309 [1] = &nfs_v4_1_minor_ops,
9310#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009311#if defined(CONFIG_NFS_V4_2)
9312 [2] = &nfs_v4_2_minor_ops,
9313#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009314};
9315
Trond Myklebust13997822016-07-24 17:10:52 -04009316static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009317{
9318 ssize_t error, error2;
9319
9320 error = generic_listxattr(dentry, list, size);
9321 if (error < 0)
9322 return error;
9323 if (list) {
9324 list += error;
9325 size -= error;
9326 }
9327
9328 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9329 if (error2 < 0)
9330 return error2;
9331 return error + error2;
9332}
9333
Trond Myklebust17f26b12013-08-21 15:48:42 -04009334static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009335 .create = nfs_create,
9336 .lookup = nfs_lookup,
9337 .atomic_open = nfs_atomic_open,
9338 .link = nfs_link,
9339 .unlink = nfs_unlink,
9340 .symlink = nfs_symlink,
9341 .mkdir = nfs_mkdir,
9342 .rmdir = nfs_rmdir,
9343 .mknod = nfs_mknod,
9344 .rename = nfs_rename,
9345 .permission = nfs_permission,
9346 .getattr = nfs_getattr,
9347 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009348 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009349};
9350
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009351static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009352 .permission = nfs_permission,
9353 .getattr = nfs_getattr,
9354 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009355 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009356};
9357
David Howells509de812006-08-22 20:06:11 -04009358const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359 .version = 4, /* protocol version */
9360 .dentry_ops = &nfs4_dentry_operations,
9361 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009362 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009363 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009365 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009366 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 .getattr = nfs4_proc_getattr,
9368 .setattr = nfs4_proc_setattr,
9369 .lookup = nfs4_proc_lookup,
9370 .access = nfs4_proc_access,
9371 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009372 .create = nfs4_proc_create,
9373 .remove = nfs4_proc_remove,
9374 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009375 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009377 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009378 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009379 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380 .link = nfs4_proc_link,
9381 .symlink = nfs4_proc_symlink,
9382 .mkdir = nfs4_proc_mkdir,
9383 .rmdir = nfs4_proc_remove,
9384 .readdir = nfs4_proc_readdir,
9385 .mknod = nfs4_proc_mknod,
9386 .statfs = nfs4_proc_statfs,
9387 .fsinfo = nfs4_proc_fsinfo,
9388 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009389 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009391 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009393 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009395 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009397 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009398 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009400 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009401 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009402 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009403 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009404 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009405 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009406 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009407 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009408 .create_server = nfs4_create_server,
9409 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410};
9411
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009412static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009413 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009414 .list = nfs4_xattr_list_nfs4_acl,
9415 .get = nfs4_xattr_get_nfs4_acl,
9416 .set = nfs4_xattr_set_nfs4_acl,
9417};
9418
9419const struct xattr_handler *nfs4_xattr_handlers[] = {
9420 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009421#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9422 &nfs4_xattr_nfs4_label_handler,
9423#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009424 NULL
9425};
9426
Linus Torvalds1da177e2005-04-16 15:20:36 -07009427/*
9428 * Local variables:
9429 * c-basic-offset: 8
9430 * End:
9431 */