blob: eb55ab6930b55d241aabaeed459282c0a26bdcf8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040096 struct nfs4_state *state, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
228 | FATTR4_WORD0_CHANGE
229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500258 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500263 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD1_OWNER_GROUP
265 | FATTR4_WORD1_RAWDEV
266 | FATTR4_WORD1_SPACE_USED
267 | FATTR4_WORD1_TIME_ACCESS
268 | FATTR4_WORD1_TIME_METADATA
269 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400270 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400271};
272
Al Virobc4785c2006-10-19 23:28:51 -0700273static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct nfs4_readdir_arg *readdir)
275{
Al Viro0dbb4c62006-10-19 23:28:49 -0700276 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000279 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
281 return;
282 }
283
284 readdir->cookie = 0;
285 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
286 if (cookie == 2)
287 return;
288
289 /*
290 * NFSv4 servers do not return entries for '.' and '..'
291 * Therefore, we fake these entries here. We let '.'
292 * have cookie 0 and '..' have cookie 1. Note that
293 * when talking to the server, we always send cookie 0
294 * instead of 1 or 2.
295 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800296 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (cookie == 0) {
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_one; /* cookie, second word */
302 *p++ = xdr_one; /* entry len */
303 memcpy(p, ".\0\0\0", 4); /* entry */
304 p++;
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000308 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 *p++ = xdr_one; /* next */
312 *p++ = xdr_zero; /* cookie, first word */
313 *p++ = xdr_two; /* cookie, second word */
314 *p++ = xdr_two; /* entry len */
315 memcpy(p, "..\0\0", 4); /* entry */
316 p++;
317 *p++ = xdr_one; /* bitmap length */
318 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
319 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000320 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 readdir->pgbase = (char *)p - (char *)start;
323 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800324 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Trond Myklebust26d36302016-09-22 13:39:05 -0400327static void nfs4_test_and_free_stateid(struct nfs_server *server,
328 nfs4_stateid *stateid,
329 struct rpc_cred *cred)
330{
331 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
332
333 ops->test_and_free_expired(server, stateid, cred);
334}
335
336static void __nfs4_free_revoked_stateid(struct nfs_server *server,
337 nfs4_stateid *stateid,
338 struct rpc_cred *cred)
339{
340 stateid->type = NFS4_REVOKED_STATEID_TYPE;
341 nfs4_test_and_free_stateid(server, stateid, cred);
342}
343
344static void nfs4_free_revoked_stateid(struct nfs_server *server,
345 const nfs4_stateid *stateid,
346 struct rpc_cred *cred)
347{
348 nfs4_stateid tmp;
349
350 nfs4_stateid_copy(&tmp, stateid);
351 __nfs4_free_revoked_stateid(server, &tmp, cred);
352}
353
NeilBrown8478eaa2014-09-18 16:09:27 +1000354static long nfs4_update_delay(long *timeout)
355{
356 long ret;
357 if (!timeout)
358 return NFS4_POLL_RETRY_MAX;
359 if (*timeout <= 0)
360 *timeout = NFS4_POLL_RETRY_MIN;
361 if (*timeout > NFS4_POLL_RETRY_MAX)
362 *timeout = NFS4_POLL_RETRY_MAX;
363 ret = *timeout;
364 *timeout <<= 1;
365 return ret;
366}
367
Trond Myklebust65de8722008-12-23 15:21:44 -0500368static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
369{
370 int res = 0;
371
372 might_sleep();
373
NeilBrown8478eaa2014-09-18 16:09:27 +1000374 freezable_schedule_timeout_killable_unsafe(
375 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500376 if (fatal_signal_pending(current))
377 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 return res;
379}
380
381/* This is the error handling routine for processes that are allowed
382 * to sleep.
383 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400384static int nfs4_do_handle_exception(struct nfs_server *server,
385 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500386{
387 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500388 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400389 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500390 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500391 int ret = errorcode;
392
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400393 exception->delay = 0;
394 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400396
397 if (stateid == NULL && state != NULL)
398 stateid = &state->stateid;
399
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 switch(errorcode) {
401 case 0:
402 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400403 case -NFS4ERR_DELEG_REVOKED:
404 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400405 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 if (inode != NULL && stateid != NULL) {
408 nfs_inode_find_state_and_recover(inode,
409 stateid);
410 goto wait_on_recovery;
411 }
412 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400413 if (inode) {
414 int err;
415
416 err = nfs_async_inode_return_delegation(inode,
417 stateid);
418 if (err == 0)
419 goto wait_on_recovery;
420 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
421 exception->retry = 1;
422 break;
423 }
424 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 if (state == NULL)
426 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400427 ret = nfs4_schedule_stateid_recovery(server, state);
428 if (ret < 0)
429 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500430 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500431 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500433 nfs4_schedule_lease_recovery(clp);
434 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400435 case -NFS4ERR_MOVED:
436 ret = nfs4_schedule_migration_recovery(server);
437 if (ret < 0)
438 break;
439 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400440 case -NFS4ERR_LEASE_MOVED:
441 nfs4_schedule_lease_moved_recovery(clp);
442 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500443#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400444 case -NFS4ERR_BADSESSION:
445 case -NFS4ERR_BADSLOT:
446 case -NFS4ERR_BAD_HIGH_SLOT:
447 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
448 case -NFS4ERR_DEADSESSION:
449 case -NFS4ERR_SEQ_FALSE_RETRY:
450 case -NFS4ERR_SEQ_MISORDERED:
451 dprintk("%s ERROR: %d Reset session\n", __func__,
452 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400453 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500457 if (exception->timeout > HZ) {
458 /* We have retried a decent amount, time to
459 * fail
460 */
461 ret = -EBUSY;
462 break;
463 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400465 nfs_inc_server_stats(server, NFSIOS_DELAY);
466 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400467 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400468 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400469 exception->delay = 1;
470 return 0;
471
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400472 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_OLD_STATEID:
474 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800475 break;
476 case -NFS4ERR_BADOWNER:
477 /* The following works around a Linux server bug! */
478 case -NFS4ERR_BADNAME:
479 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
480 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
481 exception->retry = 1;
482 printk(KERN_WARNING "NFS: v4 server %s "
483 "does not accept raw "
484 "uid/gids. "
485 "Reenabling the idmapper.\n",
486 server->nfs_client->cl_hostname);
487 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500488 }
489 /* We failed to handle the error */
490 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500491wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400492 exception->recovering = 1;
493 return 0;
494}
495
496/* This is the error handling routine for processes that are allowed
497 * to sleep.
498 */
499int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
500{
501 struct nfs_client *clp = server->nfs_client;
502 int ret;
503
504 ret = nfs4_do_handle_exception(server, errorcode, exception);
505 if (exception->delay) {
506 ret = nfs4_delay(server->client, &exception->timeout);
507 goto out_retry;
508 }
509 if (exception->recovering) {
510 ret = nfs4_wait_clnt_recover(clp);
511 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
512 return -EIO;
513 goto out_retry;
514 }
515 return ret;
516out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500520}
521
Trond Myklebust037fc982015-09-20 15:51:00 -0400522static int
523nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
524 int errorcode, struct nfs4_exception *exception)
525{
526 struct nfs_client *clp = server->nfs_client;
527 int ret;
528
529 ret = nfs4_do_handle_exception(server, errorcode, exception);
530 if (exception->delay) {
531 rpc_delay(task, nfs4_update_delay(&exception->timeout));
532 goto out_retry;
533 }
534 if (exception->recovering) {
535 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
536 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
537 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
538 goto out_retry;
539 }
540 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
541 ret = -EIO;
542 return ret;
543out_retry:
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500544 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400545 exception->retry = 1;
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500546 /*
547 * For NFS4ERR_MOVED, the client transport will need to
548 * be recomputed after migration recovery has completed.
549 */
550 if (errorcode == -NFS4ERR_MOVED)
551 rpc_task_release_transport(task);
552 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
585
586 if (flavor == RPC_AUTH_GSS_KRB5I ||
587 flavor == RPC_AUTH_GSS_KRB5P)
588 return true;
589
590 return false;
591}
Trond Myklebust65de8722008-12-23 15:21:44 -0500592
Trond Myklebust452e9352010-07-31 14:29:06 -0400593static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&clp->cl_lock);
596 if (time_before(clp->cl_last_renewal,timestamp))
597 clp->cl_last_renewal = timestamp;
598 spin_unlock(&clp->cl_lock);
599}
600
Trond Myklebust452e9352010-07-31 14:29:06 -0400601static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
602{
Trond Myklebustbe824162015-07-05 14:50:46 -0400603 struct nfs_client *clp = server->nfs_client;
604
605 if (!nfs4_has_session(clp))
606 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400607}
608
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400609struct nfs4_call_sync_data {
610 const struct nfs_server *seq_server;
611 struct nfs4_sequence_args *seq_args;
612 struct nfs4_sequence_res *seq_res;
613};
614
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800615void nfs4_init_sequence(struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400617{
618 args->sa_slot = NULL;
619 args->sa_cache_this = cache_reply;
620 args->sa_privileged = 0;
621
622 res->sr_slot = NULL;
623}
624
625static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
626{
627 args->sa_privileged = 1;
628}
629
Peng Taocb04ad22014-06-11 05:24:15 +0800630int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
631 struct nfs4_sequence_args *args,
632 struct nfs4_sequence_res *res,
633 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400634{
Chuck Lever3bd23842013-08-09 12:49:19 -0400635 struct nfs4_slot *slot;
636
637 /* slot already allocated? */
638 if (res->sr_slot != NULL)
639 goto out_start;
640
641 spin_lock(&tbl->slot_tbl_lock);
642 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
643 goto out_sleep;
644
645 slot = nfs4_alloc_slot(tbl);
646 if (IS_ERR(slot)) {
647 if (slot == ERR_PTR(-ENOMEM))
648 task->tk_timeout = HZ >> 2;
649 goto out_sleep;
650 }
651 spin_unlock(&tbl->slot_tbl_lock);
652
Trond Myklebust0a014a42016-09-22 13:38:51 -0400653 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400654 args->sa_slot = slot;
655 res->sr_slot = slot;
656
657out_start:
658 rpc_call_start(task);
659 return 0;
660
661out_sleep:
662 if (args->sa_privileged)
663 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
664 NULL, RPC_PRIORITY_PRIVILEGED);
665 else
666 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
667 spin_unlock(&tbl->slot_tbl_lock);
668 return -EAGAIN;
669}
Peng Taocb04ad22014-06-11 05:24:15 +0800670EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400671
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400672static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400673{
674 struct nfs4_slot *slot = res->sr_slot;
675 struct nfs4_slot_table *tbl;
676
Chuck Lever3bd23842013-08-09 12:49:19 -0400677 tbl = slot->table;
678 spin_lock(&tbl->slot_tbl_lock);
679 if (!nfs41_wake_and_assign_slot(tbl, slot))
680 nfs4_free_slot(tbl, slot);
681 spin_unlock(&tbl->slot_tbl_lock);
682
683 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400684}
685
686static int nfs40_sequence_done(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
688{
689 if (res->sr_slot != NULL)
690 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400691 return 1;
692}
693
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694#if defined(CONFIG_NFS_V4_1)
695
Trond Myklebustd185a332010-06-16 09:52:25 -0400696static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400699 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500701 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500705
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400706 /* Bump the slot sequence number */
707 if (slot->seq_done)
708 slot->seq_nr++;
709 slot->seq_done = 0;
710
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500711 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500712 /* Be nice to the server: try to ensure that the last transmitted
713 * value for highest_user_slotid <= target_highest_slotid
714 */
715 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
716 send_new_highest_used_slotid = true;
717
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719 send_new_highest_used_slotid = false;
720 goto out_unlock;
721 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500722 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723
724 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
725 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500726out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500727 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400728 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500729 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400730 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400731 if (waitqueue_active(&tbl->slot_waitq))
732 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400733}
734
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400735static int nfs41_sequence_process(struct rpc_task *task,
736 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400737{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500738 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500741 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500742 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400743
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500744 if (slot == NULL)
745 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400746 /* don't increment the sequence number if the task wasn't sent */
747 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400748 goto out;
749
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500750 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500751
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 if (slot->interrupted) {
753 slot->interrupted = 0;
754 interrupted = true;
755 }
756
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400757 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500758 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400759 switch (res->sr_status) {
760 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400761 /* If previous op on slot was interrupted and we reused
762 * the seq# and got a reply from the cache, then retry
763 */
764 if (task->tk_status == -EREMOTEIO && interrupted) {
765 ++slot->seq_nr;
766 goto retry_nowait;
767 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400768 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400769 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500770 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500771 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500772 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400773 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
774 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500775 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500777 case 1:
778 /*
779 * sr_status remains 1 if an RPC level error occurred.
780 * The server may or may not have processed the sequence
781 * operation..
782 * Mark the slot as having hosted an interrupted RPC call.
783 */
784 slot->interrupted = 1;
785 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400786 case -NFS4ERR_DELAY:
787 /* The server detected a resend of the RPC call and
788 * returned NFS4ERR_DELAY as per Section 2.10.6.2
789 * of RFC5661.
790 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500791 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400792 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500793 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500794 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400795 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500796 case -NFS4ERR_BADSLOT:
797 /*
798 * The slot id we used was probably retired. Try again
799 * using a different slot id.
800 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500801 goto retry_nowait;
802 case -NFS4ERR_SEQ_MISORDERED:
803 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500804 * Was the last operation on this sequence interrupted?
805 * If so, retry after bumping the sequence number.
806 */
807 if (interrupted) {
808 ++slot->seq_nr;
809 goto retry_nowait;
810 }
811 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500812 * Could this slot have been previously retired?
813 * If so, then the server may be expecting seq_nr = 1!
814 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500815 if (slot->seq_nr != 1) {
816 slot->seq_nr = 1;
817 goto retry_nowait;
818 }
819 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500820 case -NFS4ERR_SEQ_FALSE_RETRY:
821 ++slot->seq_nr;
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168a2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168a2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001112 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1115 struct iattr attrs;
1116 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001118 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001119 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int cancelled;
1122};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001124static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1125 int err, struct nfs4_exception *exception)
1126{
1127 if (err != -EINVAL)
1128 return false;
1129 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1130 return false;
1131 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1132 exception->retry = 1;
1133 return true;
1134}
1135
Trond Myklebust6ae37332015-01-30 14:21:14 -05001136static u32
1137nfs4_map_atomic_open_share(struct nfs_server *server,
1138 fmode_t fmode, int openflags)
1139{
1140 u32 res = 0;
1141
1142 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1143 case FMODE_READ:
1144 res = NFS4_SHARE_ACCESS_READ;
1145 break;
1146 case FMODE_WRITE:
1147 res = NFS4_SHARE_ACCESS_WRITE;
1148 break;
1149 case FMODE_READ|FMODE_WRITE:
1150 res = NFS4_SHARE_ACCESS_BOTH;
1151 }
1152 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1153 goto out;
1154 /* Want no delegation if we're using O_DIRECT */
1155 if (openflags & O_DIRECT)
1156 res |= NFS4_SHARE_WANT_NO_DELEG;
1157out:
1158 return res;
1159}
1160
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001161static enum open_claim_type4
1162nfs4_map_atomic_open_claim(struct nfs_server *server,
1163 enum open_claim_type4 claim)
1164{
1165 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1166 return claim;
1167 switch (claim) {
1168 default:
1169 return claim;
1170 case NFS4_OPEN_CLAIM_FH:
1171 return NFS4_OPEN_CLAIM_NULL;
1172 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1173 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1174 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1175 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1176 }
1177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180{
1181 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001182 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183 p->o_res.seqid = p->o_arg.seqid;
1184 p->c_res.seqid = p->c_arg.seqid;
1185 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001186 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001188 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189}
1190
Al Viro82a2c1b2011-06-22 18:30:55 -04001191static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001193 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001194 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001195 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001196 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197{
Al Viro82a2c1b2011-06-22 18:30:55 -04001198 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001199 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001201 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 struct nfs4_opendata *p;
1203
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001204 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 if (p == NULL)
1206 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208 p->f_label = nfs4_label_alloc(server, gfp_mask);
1209 if (IS_ERR(p->f_label))
1210 goto err_free_p;
1211
Kinglong Meea49c2692015-07-27 15:31:38 +08001212 p->a_label = nfs4_label_alloc(server, gfp_mask);
1213 if (IS_ERR(p->a_label))
1214 goto err_free_f;
1215
Trond Myklebust63f5f792015-01-23 19:19:25 -05001216 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1217 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001218 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001219 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 nfs_sb_active(dentry->d_sb);
1221 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 p->dir = parent;
1223 p->owner = sp;
1224 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001225 p->o_arg.open_flags = flags;
1226 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001227 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1228 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001229 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1230 * will return permission denied for all bits until close */
1231 if (!(flags & O_EXCL)) {
1232 /* ask server to check for all possible rights as results
1233 * are cached */
1234 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1235 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001236 }
David Howells7539bba2006-08-22 20:06:09 -04001237 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001238 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1239 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001240 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001241 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001242 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001243 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001244 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001245 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1246 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001247 case NFS4_OPEN_CLAIM_NULL:
1248 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1249 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1250 p->o_arg.fh = NFS_FH(dir);
1251 break;
1252 case NFS4_OPEN_CLAIM_PREVIOUS:
1253 case NFS4_OPEN_CLAIM_FH:
1254 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1255 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001256 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001257 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001258 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001259 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001260
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001261 p->o_arg.u.attrs = &p->attrs;
1262 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001263
1264 verf[0] = jiffies;
1265 verf[1] = current->pid;
1266 memcpy(p->o_arg.u.verifier.data, verf,
1267 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001269 p->c_arg.fh = &p->o_res.fh;
1270 p->c_arg.stateid = &p->o_res.stateid;
1271 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001272 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001275
1276err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001277 nfs4_label_free(p->a_label);
1278err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001279 nfs4_label_free(p->f_label);
1280err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001281 kfree(p);
1282err:
1283 dput(parent);
1284 return NULL;
1285}
1286
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289 struct nfs4_opendata *p = container_of(kref,
1290 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001291 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292
1293 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001294 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001295 if (p->state != NULL)
1296 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001298
Kinglong Meea49c2692015-07-27 15:31:38 +08001299 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001300 nfs4_label_free(p->f_label);
1301
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001302 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001303 dput(p->dentry);
1304 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001305 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001306 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307 kfree(p);
1308}
1309
1310static void nfs4_opendata_put(struct nfs4_opendata *p)
1311{
1312 if (p != NULL)
1313 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001314}
1315
Trond Myklebust06f814a2006-01-03 09:55:07 +01001316static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1317{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318 int ret;
1319
Trond Myklebust06f814a2006-01-03 09:55:07 +01001320 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001321 return ret;
1322}
1323
Trond Myklebust24311f82015-09-20 10:50:17 -04001324static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1325 fmode_t fmode)
1326{
1327 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1328 case FMODE_READ|FMODE_WRITE:
1329 return state->n_rdwr != 0;
1330 case FMODE_WRITE:
1331 return state->n_wronly != 0;
1332 case FMODE_READ:
1333 return state->n_rdonly != 0;
1334 }
1335 WARN_ON_ONCE(1);
1336 return false;
1337}
1338
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001339static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001340{
1341 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342
Trond Myklebust536e43d2012-01-17 22:04:26 -05001343 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344 goto out;
1345 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001346 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001347 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1348 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349 break;
1350 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001351 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1352 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001353 break;
1354 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001355 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1356 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001357 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 return ret;
1360}
1361
Trond Myklebust2a606182015-08-19 22:30:00 -05001362static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1363 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001364{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001365 if (delegation == NULL)
1366 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001369 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1370 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001371 switch (claim) {
1372 case NFS4_OPEN_CLAIM_NULL:
1373 case NFS4_OPEN_CLAIM_FH:
1374 break;
1375 case NFS4_OPEN_CLAIM_PREVIOUS:
1376 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1377 break;
1378 default:
1379 return 0;
1380 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001381 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001382 return 1;
1383}
1384
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001385static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001386{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001388 case FMODE_WRITE:
1389 state->n_wronly++;
1390 break;
1391 case FMODE_READ:
1392 state->n_rdonly++;
1393 break;
1394 case FMODE_READ|FMODE_WRITE:
1395 state->n_rdwr++;
1396 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001397 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001398}
1399
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001400#ifdef CONFIG_NFS_V4_1
1401static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1402{
1403 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1404 return true;
1405 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1406 return true;
1407 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1408 return true;
1409 return false;
1410}
1411#endif /* CONFIG_NFS_V4_1 */
1412
Trond Myklebust4f14c192014-02-12 19:15:06 -05001413static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001414{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001415 struct nfs_client *clp = state->owner->so_server->nfs_client;
1416 bool need_recover = false;
1417
1418 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1419 need_recover = true;
1420 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1421 need_recover = true;
1422 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1423 need_recover = true;
1424 if (need_recover)
1425 nfs4_state_mark_reclaim_nograce(clp, state);
1426}
1427
Trond Myklebuste999e802014-02-10 18:20:47 -05001428static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001429 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001430{
1431 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1432 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001433 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001434 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001436 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001438 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1439 return true;
1440 return false;
1441}
1442
Trond Myklebustf95549c2015-01-23 18:06:09 -05001443static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1444{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001445 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1446 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447 if (state->n_wronly)
1448 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1449 if (state->n_rdonly)
1450 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1451 if (state->n_rdwr)
1452 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001453 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001454}
1455
Trond Myklebust226056c2014-02-11 10:41:07 -05001456static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1457 nfs4_stateid *stateid, fmode_t fmode)
1458{
1459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1460 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1461 case FMODE_WRITE:
1462 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1463 break;
1464 case FMODE_READ:
1465 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 break;
1467 case 0:
1468 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 clear_bit(NFS_OPEN_STATE, &state->flags);
1471 }
1472 if (stateid == NULL)
1473 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001474 /* Handle OPEN+OPEN_DOWNGRADE races */
1475 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1476 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001477 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001478 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001479 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001481 nfs4_stateid_copy(&state->stateid, stateid);
1482 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001483}
1484
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001485static void nfs_clear_open_stateid(struct nfs4_state *state,
1486 nfs4_stateid *arg_stateid,
1487 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001488{
1489 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001490 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1491 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1492 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001493 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001494 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1495 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001496}
1497
Trond Myklebust1393d962016-09-22 13:39:13 -04001498static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1499 const nfs4_stateid *stateid, fmode_t fmode,
1500 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001501{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001503 case FMODE_READ:
1504 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1505 break;
1506 case FMODE_WRITE:
1507 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1508 break;
1509 case FMODE_READ|FMODE_WRITE:
1510 set_bit(NFS_O_RDWR_STATE, &state->flags);
1511 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001512 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001513 return;
1514 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1515 nfs4_stateid_copy(&state->stateid, stateid);
1516 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001517}
1518
Trond Myklebust1393d962016-09-22 13:39:13 -04001519static void __update_open_stateid(struct nfs4_state *state,
1520 const nfs4_stateid *open_stateid,
1521 const nfs4_stateid *deleg_stateid,
1522 fmode_t fmode,
1523 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001525 /*
1526 * Protect the call to nfs4_state_set_mode_locked and
1527 * serialise the stateid update
1528 */
Andrew Elble361cad32015-12-02 09:20:57 -05001529 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001530 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001531 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001532 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001533 set_bit(NFS_DELEGATED_STATE, &state->flags);
1534 }
1535 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001536 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001537 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001539 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Trond Myklebust1393d962016-09-22 13:39:13 -04001542static int update_open_stateid(struct nfs4_state *state,
1543 const nfs4_stateid *open_stateid,
1544 const nfs4_stateid *delegation,
1545 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001546{
Trond Myklebust1393d962016-09-22 13:39:13 -04001547 struct nfs_server *server = NFS_SERVER(state->inode);
1548 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001549 struct nfs_inode *nfsi = NFS_I(state->inode);
1550 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001551 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001552 int ret = 0;
1553
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001554 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001555
1556 rcu_read_lock();
1557 deleg_cur = rcu_dereference(nfsi->delegation);
1558 if (deleg_cur == NULL)
1559 goto no_delegation;
1560
1561 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001562 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001563 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001564 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001565 goto no_delegation_unlock;
1566
1567 if (delegation == NULL)
1568 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001569 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001570 goto no_delegation_unlock;
1571
Trond Myklebustb7391f42008-12-23 15:21:52 -05001572 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001573 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1574 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001575 ret = 1;
1576no_delegation_unlock:
1577 spin_unlock(&deleg_cur->lock);
1578no_delegation:
1579 rcu_read_unlock();
1580
1581 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001582 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001583 ret = 1;
1584 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001585 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001586 nfs4_schedule_state_manager(clp);
1587 if (freeme.type != 0)
1588 nfs4_test_and_free_stateid(server, &freeme,
1589 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001590
1591 return ret;
1592}
1593
Trond Myklebust39071e62015-01-24 15:07:56 -05001594static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1595 const nfs4_stateid *stateid)
1596{
1597 struct nfs4_state *state = lsp->ls_state;
1598 bool ret = false;
1599
1600 spin_lock(&state->state_lock);
1601 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1602 goto out_noupdate;
1603 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1604 goto out_noupdate;
1605 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1606 ret = true;
1607out_noupdate:
1608 spin_unlock(&state->state_lock);
1609 return ret;
1610}
Trond Myklebust34310432008-12-23 15:21:38 -05001611
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001612static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001613{
1614 struct nfs_delegation *delegation;
1615
1616 rcu_read_lock();
1617 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001618 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001619 rcu_read_unlock();
1620 return;
1621 }
1622 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001623 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001624}
1625
Trond Myklebust6ee41262007-07-08 14:11:36 -04001626static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627{
1628 struct nfs4_state *state = opendata->state;
1629 struct nfs_inode *nfsi = NFS_I(state->inode);
1630 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001631 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001632 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001633 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001634 nfs4_stateid stateid;
1635 int ret = -EAGAIN;
1636
Trond Myklebustaac00a82007-07-05 19:02:21 -04001637 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001638 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001639 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001640 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001641 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001642 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001643 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001644 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001645 rcu_read_lock();
1646 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001647 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001648 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001649 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001650 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001652 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001654 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001655 if (!opendata->is_recover) {
1656 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1657 if (ret != 0)
1658 goto out;
1659 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001660 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001661
1662 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001663 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001664 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001665 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001666out:
1667 return ERR_PTR(ret);
1668out_return_state:
1669 atomic_inc(&state->count);
1670 return state;
1671}
1672
Andy Adamsone23008e2012-10-02 21:07:32 -04001673static void
1674nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1675{
1676 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1677 struct nfs_delegation *delegation;
1678 int delegation_flags = 0;
1679
1680 rcu_read_lock();
1681 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1682 if (delegation)
1683 delegation_flags = delegation->flags;
1684 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001685 switch (data->o_arg.claim) {
1686 default:
1687 break;
1688 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1689 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001690 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1691 "returning a delegation for "
1692 "OPEN(CLAIM_DELEGATE_CUR)\n",
1693 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001694 return;
1695 }
1696 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001697 nfs_inode_set_delegation(state->inode,
1698 data->owner->so_cred,
1699 &data->o_res);
1700 else
1701 nfs_inode_reclaim_delegation(state->inode,
1702 data->owner->so_cred,
1703 &data->o_res);
1704}
1705
1706/*
1707 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1708 * and update the nfs4_state.
1709 */
1710static struct nfs4_state *
1711_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1712{
1713 struct inode *inode = data->state->inode;
1714 struct nfs4_state *state = data->state;
1715 int ret;
1716
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001717 if (!data->rpc_done) {
1718 if (data->rpc_status) {
1719 ret = data->rpc_status;
1720 goto err;
1721 }
1722 /* cached opens have already been processed */
1723 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001724 }
1725
Andy Adamsone23008e2012-10-02 21:07:32 -04001726 ret = nfs_refresh_inode(inode, &data->f_attr);
1727 if (ret)
1728 goto err;
1729
1730 if (data->o_res.delegation_type != 0)
1731 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001732update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001733 update_open_stateid(state, &data->o_res.stateid, NULL,
1734 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001735 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001736
1737 return state;
1738err:
1739 return ERR_PTR(ret);
1740
1741}
1742
1743static struct nfs4_state *
1744_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001745{
1746 struct inode *inode;
1747 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001748 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001749
Trond Myklebustaac00a82007-07-05 19:02:21 -04001750 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001751 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001752 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001753 goto out;
1754 }
1755
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001756 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001758 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001759 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001761 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001762 goto err;
1763 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001764 state = nfs4_get_open_state(inode, data->owner);
1765 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001766 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001767 if (data->o_res.delegation_type != 0)
1768 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001769 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001770 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001772out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001773 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001775err_put_inode:
1776 iput(inode);
1777err:
1778 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779}
1780
Andy Adamsone23008e2012-10-02 21:07:32 -04001781static struct nfs4_state *
1782nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1783{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001784 struct nfs4_state *ret;
1785
Andy Adamsone23008e2012-10-02 21:07:32 -04001786 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001787 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1788 else
1789 ret = _nfs4_opendata_to_nfs4_state(data);
1790 nfs4_sequence_free_slot(&data->o_res.seq_res);
1791 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001792}
1793
Trond Myklebust864472e2006-01-03 09:55:15 +01001794static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1795{
1796 struct nfs_inode *nfsi = NFS_I(state->inode);
1797 struct nfs_open_context *ctx;
1798
1799 spin_lock(&state->inode->i_lock);
1800 list_for_each_entry(ctx, &nfsi->open_files, list) {
1801 if (ctx->state != state)
1802 continue;
1803 get_nfs_open_context(ctx);
1804 spin_unlock(&state->inode->i_lock);
1805 return ctx;
1806 }
1807 spin_unlock(&state->inode->i_lock);
1808 return ERR_PTR(-ENOENT);
1809}
1810
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001811static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1812 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001813{
1814 struct nfs4_opendata *opendata;
1815
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001816 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001817 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001818 if (opendata == NULL)
1819 return ERR_PTR(-ENOMEM);
1820 opendata->state = state;
1821 atomic_inc(&state->count);
1822 return opendata;
1823}
1824
Trond Myklebust24311f82015-09-20 10:50:17 -04001825static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1826 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001827{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001828 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001829 int ret;
1830
Trond Myklebust24311f82015-09-20 10:50:17 -04001831 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001832 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001833 opendata->o_arg.open_flags = 0;
1834 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001835 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1836 NFS_SB(opendata->dentry->d_sb),
1837 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001838 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1839 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1840 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001841 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001842 if (ret != 0)
1843 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001844 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001845 if (IS_ERR(newstate))
1846 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001847 if (newstate != opendata->state)
1848 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001849 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001850 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001851}
1852
1853static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1854{
Trond Myklebust864472e2006-01-03 09:55:15 +01001855 int ret;
1856
Trond Myklebust4f14c192014-02-12 19:15:06 -05001857 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1858 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1859 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1860 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001861 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001862 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001863 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001864 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001865 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1866 if (ret != 0)
1867 return ret;
1868 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1869 if (ret != 0)
1870 return ret;
1871 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1872 if (ret != 0)
1873 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001874 /*
1875 * We may have performed cached opens for all three recoveries.
1876 * Check if we need to update the current stateid.
1877 */
1878 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001879 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001880 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001881 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001882 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001883 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001884 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001885 return 0;
1886}
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888/*
1889 * OPEN_RECLAIM:
1890 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001892static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001894 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001895 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001896 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int status;
1898
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001899 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1900 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001901 if (IS_ERR(opendata))
1902 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001903 rcu_read_lock();
1904 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001905 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001906 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001907 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001908 opendata->o_arg.u.delegation_type = delegation_type;
1909 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001910 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return status;
1912}
1913
Trond Myklebust539cd032007-06-05 11:46:42 -04001914static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 struct nfs_server *server = NFS_SERVER(state->inode);
1917 struct nfs4_exception exception = { };
1918 int err;
1919 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001920 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001921 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001922 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1923 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001924 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001925 break;
1926 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 } while (exception.retry);
1928 return err;
1929}
1930
Trond Myklebust864472e2006-01-03 09:55:15 +01001931static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1932{
1933 struct nfs_open_context *ctx;
1934 int ret;
1935
1936 ctx = nfs4_state_find_open_context(state);
1937 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001938 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001939 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001940 put_nfs_open_context(ctx);
1941 return ret;
1942}
1943
NeilBrown70f254a2017-12-13 09:57:09 +11001944static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001946 switch (err) {
1947 default:
1948 printk(KERN_ERR "NFS: %s: unhandled error "
1949 "%d.\n", __func__, err);
1950 case 0:
1951 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001952 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001953 case -ESTALE:
1954 break;
1955 case -NFS4ERR_BADSESSION:
1956 case -NFS4ERR_BADSLOT:
1957 case -NFS4ERR_BAD_HIGH_SLOT:
1958 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1959 case -NFS4ERR_DEADSESSION:
1960 set_bit(NFS_DELEGATED_STATE, &state->flags);
1961 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1962 return -EAGAIN;
1963 case -NFS4ERR_STALE_CLIENTID:
1964 case -NFS4ERR_STALE_STATEID:
1965 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001966 /* Don't recall a delegation if it was lost */
1967 nfs4_schedule_lease_recovery(server->nfs_client);
1968 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001969 case -NFS4ERR_MOVED:
1970 nfs4_schedule_migration_recovery(server);
1971 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001972 case -NFS4ERR_LEASE_MOVED:
1973 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1974 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001975 case -NFS4ERR_DELEG_REVOKED:
1976 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001977 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001978 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001979 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001980 nfs_inode_find_state_and_recover(state->inode,
1981 stateid);
1982 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001983 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001984 case -NFS4ERR_DELAY:
1985 case -NFS4ERR_GRACE:
1986 set_bit(NFS_DELEGATED_STATE, &state->flags);
1987 ssleep(1);
1988 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001989 case -ENOMEM:
1990 case -NFS4ERR_DENIED:
NeilBrown70f254a2017-12-13 09:57:09 +11001991 if (fl) {
1992 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
1993 if (lsp)
1994 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1995 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001996 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return err;
1999}
2000
Trond Myklebust24311f82015-09-20 10:50:17 -04002001int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2002 struct nfs4_state *state, const nfs4_stateid *stateid,
2003 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002004{
2005 struct nfs_server *server = NFS_SERVER(state->inode);
2006 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002007 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002008
2009 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2010 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2011 if (IS_ERR(opendata))
2012 return PTR_ERR(opendata);
2013 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002014 write_seqlock(&state->seqlock);
2015 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2016 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002017 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2018 switch (type & (FMODE_READ|FMODE_WRITE)) {
2019 case FMODE_READ|FMODE_WRITE:
2020 case FMODE_WRITE:
2021 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2022 if (err)
2023 break;
2024 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2025 if (err)
2026 break;
2027 case FMODE_READ:
2028 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2029 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002030 nfs4_opendata_put(opendata);
NeilBrown70f254a2017-12-13 09:57:09 +11002031 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002032}
2033
Chuck Leverbe05c862013-08-09 12:49:47 -04002034static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2035{
2036 struct nfs4_opendata *data = calldata;
2037
Peng Taocb04ad22014-06-11 05:24:15 +08002038 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2039 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002040}
2041
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002042static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2043{
2044 struct nfs4_opendata *data = calldata;
2045
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002046 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002047
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002048 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002049 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002050 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002051 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002052 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002053 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002054 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002055}
2056
2057static void nfs4_open_confirm_release(void *calldata)
2058{
2059 struct nfs4_opendata *data = calldata;
2060 struct nfs4_state *state = NULL;
2061
2062 /* If this request hasn't been cancelled, do nothing */
2063 if (data->cancelled == 0)
2064 goto out_free;
2065 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002066 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002067 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002068 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002069 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002070 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002071out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002072 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073}
2074
2075static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002076 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002077 .rpc_call_done = nfs4_open_confirm_done,
2078 .rpc_release = nfs4_open_confirm_release,
2079};
2080
2081/*
2082 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2083 */
2084static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2085{
David Howells2b0143b2015-03-17 22:25:59 +00002086 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002087 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002088 struct rpc_message msg = {
2089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2090 .rpc_argp = &data->c_arg,
2091 .rpc_resp = &data->c_res,
2092 .rpc_cred = data->owner->so_cred,
2093 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002094 struct rpc_task_setup task_setup_data = {
2095 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002096 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002097 .callback_ops = &nfs4_open_confirm_ops,
2098 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002099 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002100 .flags = RPC_TASK_ASYNC,
2101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 int status;
2103
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002104 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002105 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002106 data->rpc_done = 0;
2107 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002108 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002109 if (data->is_recover)
2110 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002111 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002112 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002113 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002114 status = nfs4_wait_for_completion_rpc_task(task);
2115 if (status != 0) {
2116 data->cancelled = 1;
2117 smp_wmb();
2118 } else
2119 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002120 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return status;
2122}
2123
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002126 struct nfs4_opendata *data = calldata;
2127 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002128 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002129 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002130
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002132 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002133 /*
2134 * Check if we still need to send an OPEN call, or if we can use
2135 * a delegation instead.
2136 */
2137 if (data->state != NULL) {
2138 struct nfs_delegation *delegation;
2139
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002140 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002141 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002142 rcu_read_lock();
2143 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002144 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002145 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002146 rcu_read_unlock();
2147 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002148 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002149 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002150 switch (claim) {
2151 default:
2152 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002153 case NFS4_OPEN_CLAIM_PREVIOUS:
2154 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2155 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002156 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002157 case NFS4_OPEN_CLAIM_FH:
2158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002159 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2160 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002161 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002162 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002163 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002164 &data->o_res.seq_res,
2165 task) != 0)
2166 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002167
2168 /* Set the create mode (note dependency on the session type) */
2169 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2170 if (data->o_arg.open_flags & O_EXCL) {
2171 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2172 if (nfs4_has_persistent_session(clp))
2173 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2174 else if (clp->cl_mvops->minor_version > 0)
2175 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2176 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002177 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002178unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002179 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002180 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002181out_no_action:
2182 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002183out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002184 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002185}
2186
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187static void nfs4_open_done(struct rpc_task *task, void *calldata)
2188{
2189 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002192
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002193 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002194 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002195
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002196 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002197 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2198 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002199 case S_IFREG:
2200 break;
2201 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 break;
2204 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002206 break;
2207 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002209 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002211 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002212 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2213 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002214 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002215 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002216}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002217
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218static void nfs4_open_release(void *calldata)
2219{
2220 struct nfs4_opendata *data = calldata;
2221 struct nfs4_state *state = NULL;
2222
2223 /* If this request hasn't been cancelled, do nothing */
2224 if (data->cancelled == 0)
2225 goto out_free;
2226 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002227 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228 goto out_free;
2229 /* In case we need an open_confirm, no cleanup! */
2230 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2231 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002233 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002234 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002235out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002236 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237}
2238
2239static const struct rpc_call_ops nfs4_open_ops = {
2240 .rpc_call_prepare = nfs4_open_prepare,
2241 .rpc_call_done = nfs4_open_done,
2242 .rpc_release = nfs4_open_release,
2243};
2244
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002245static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002246{
David Howells2b0143b2015-03-17 22:25:59 +00002247 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248 struct nfs_server *server = NFS_SERVER(dir);
2249 struct nfs_openargs *o_arg = &data->o_arg;
2250 struct nfs_openres *o_res = &data->o_res;
2251 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002252 struct rpc_message msg = {
2253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2254 .rpc_argp = o_arg,
2255 .rpc_resp = o_res,
2256 .rpc_cred = data->owner->so_cred,
2257 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 struct rpc_task_setup task_setup_data = {
2259 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002260 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002261 .callback_ops = &nfs4_open_ops,
2262 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002263 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002264 .flags = RPC_TASK_ASYNC,
2265 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266 int status;
2267
Chuck Levera9c92d62013-08-09 12:48:18 -04002268 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002269 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002270 data->rpc_done = 0;
2271 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002272 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002273 data->is_recover = 0;
2274 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002275 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002276 data->is_recover = 1;
2277 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002278 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002279 if (IS_ERR(task))
2280 return PTR_ERR(task);
2281 status = nfs4_wait_for_completion_rpc_task(task);
2282 if (status != 0) {
2283 data->cancelled = 1;
2284 smp_wmb();
2285 } else
2286 status = data->rpc_status;
2287 rpc_put_task(task);
2288
2289 return status;
2290}
2291
2292static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2293{
David Howells2b0143b2015-03-17 22:25:59 +00002294 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002295 struct nfs_openres *o_res = &data->o_res;
2296 int status;
2297
2298 status = nfs4_run_open_task(data, 1);
2299 if (status != 0 || !data->rpc_done)
2300 return status;
2301
Trond Myklebust6926afd2012-01-07 13:22:46 -05002302 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2303
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002304 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2305 status = _nfs4_proc_open_confirm(data);
2306 if (status != 0)
2307 return status;
2308 }
2309
2310 return status;
2311}
2312
Trond Myklebustf3792d62014-07-10 08:54:32 -04002313/*
2314 * Additional permission checks in order to distinguish between an
2315 * open for read, and an open for execute. This works around the
2316 * fact that NFSv4 OPEN treats read and execute permissions as being
2317 * the same.
2318 * Note that in the non-execute case, we want to turn off permission
2319 * checking if we just created a new file (POSIX open() semantics).
2320 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002321static int nfs4_opendata_access(struct rpc_cred *cred,
2322 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002323 struct nfs4_state *state, fmode_t fmode,
2324 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002325{
2326 struct nfs_access_entry cache;
2327 u32 mask;
2328
2329 /* access call failed or for some reason the server doesn't
2330 * support any access modes -- defer access call until later */
2331 if (opendata->o_res.access_supported == 0)
2332 return 0;
2333
2334 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002335 /*
2336 * Use openflags to check for exec, because fmode won't
2337 * always have FMODE_EXEC set when file open for exec.
2338 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002339 if (openflags & __FMODE_EXEC) {
2340 /* ONLY check for exec rights */
2341 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002342 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002343 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002344
2345 cache.cred = cred;
2346 cache.jiffies = jiffies;
2347 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2348 nfs_access_add_cache(state->inode, &cache);
2349
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002350 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002351 return 0;
2352
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002353 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002354}
2355
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002356/*
2357 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2358 */
2359static int _nfs4_proc_open(struct nfs4_opendata *data)
2360{
David Howells2b0143b2015-03-17 22:25:59 +00002361 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002362 struct nfs_server *server = NFS_SERVER(dir);
2363 struct nfs_openargs *o_arg = &data->o_arg;
2364 struct nfs_openres *o_res = &data->o_res;
2365 int status;
2366
2367 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002368 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002369 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002370 if (status != 0) {
2371 if (status == -NFS4ERR_BADNAME &&
2372 !(o_arg->open_flags & O_CREAT))
2373 return -ENOENT;
2374 return status;
2375 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002376
Trond Myklebust6926afd2012-01-07 13:22:46 -05002377 nfs_fattr_map_and_free_names(server, &data->f_attr);
2378
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002379 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002380 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002381 if (o_arg->open_flags & O_EXCL)
2382 data->file_created = 1;
2383 else if (o_res->cinfo.before != o_res->cinfo.after)
2384 data->file_created = 1;
2385 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002386 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2387 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002389 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002391 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002393 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2394 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002395 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002396 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Andy Adamsond83217c2011-03-01 01:34:17 +00002400static int nfs4_recover_expired_lease(struct nfs_server *server)
2401{
2402 return nfs4_client_recover_expired_lease(server->nfs_client);
2403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405/*
2406 * OPEN_EXPIRED:
2407 * reclaim state on the server after a network partition.
2408 * Assumes caller holds the appropriate lock
2409 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002410static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002412 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002413 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002415 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002416 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002417 if (IS_ERR(opendata))
2418 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002419 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002420 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002421 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002422 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002426static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002427{
Trond Myklebust539cd032007-06-05 11:46:42 -04002428 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002429 struct nfs4_exception exception = { };
2430 int err;
2431
2432 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002433 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002434 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002435 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2436 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002437 switch (err) {
2438 default:
2439 goto out;
2440 case -NFS4ERR_GRACE:
2441 case -NFS4ERR_DELAY:
2442 nfs4_handle_exception(server, err, &exception);
2443 err = 0;
2444 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002445 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002446out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002447 return err;
2448}
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002453 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Trond Myklebust864472e2006-01-03 09:55:15 +01002455 ctx = nfs4_state_find_open_context(state);
2456 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002457 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002458 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002459 put_nfs_open_context(ctx);
2460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Trond Myklebust41020b62016-09-22 13:38:58 -04002463static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2464 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002465{
Trond Myklebust41020b62016-09-22 13:38:58 -04002466 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002467 write_seqlock(&state->seqlock);
2468 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2469 write_sequnlock(&state->seqlock);
2470 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2471}
2472
2473static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2474{
2475 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002476 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002477}
2478
2479static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2480{
2481 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2482 nfs40_clear_delegation_stateid(state);
2483 return nfs4_open_expired(sp, state);
2484}
2485
Trond Myklebust45870d62016-09-22 13:38:59 -04002486static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2487 nfs4_stateid *stateid,
2488 struct rpc_cred *cred)
2489{
2490 return -NFS4ERR_BAD_STATEID;
2491}
2492
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002493#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002494static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2495 nfs4_stateid *stateid,
2496 struct rpc_cred *cred)
2497{
2498 int status;
2499
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002500 switch (stateid->type) {
2501 default:
2502 break;
2503 case NFS4_INVALID_STATEID_TYPE:
2504 case NFS4_SPECIAL_STATEID_TYPE:
2505 return -NFS4ERR_BAD_STATEID;
2506 case NFS4_REVOKED_STATEID_TYPE:
2507 goto out_free;
2508 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002509
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002510 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002511 switch (status) {
2512 case -NFS4ERR_EXPIRED:
2513 case -NFS4ERR_ADMIN_REVOKED:
2514 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002515 break;
2516 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002517 return status;
2518 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002519out_free:
2520 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002521 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002522 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002523}
2524
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002525static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002526{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002527 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002528 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002529 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002530 struct rpc_cred *cred;
2531 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002532
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 /* Get the delegation credential for use by test/free_stateid */
2534 rcu_read_lock();
2535 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002536 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002537 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002538 return;
2539 }
2540
2541 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaf412412018-09-05 14:07:14 -04002542 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002543 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002544 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002545 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002546 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002547
Trond Myklebustaf412412018-09-05 14:07:14 -04002548 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2549 &delegation->flags)) {
2550 rcu_read_unlock();
2551 return;
2552 }
2553
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002554 cred = get_rpccred(delegation->cred);
2555 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002556 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002557 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002558 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002559 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002560
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002561 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002562}
2563
2564/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002565 * nfs41_check_expired_locks - possibly free a lock stateid
2566 *
2567 * @state: NFSv4 state for an inode
2568 *
2569 * Returns NFS_OK if recovery for this stateid is now finished.
2570 * Otherwise a negative NFS4ERR value is returned.
2571 */
2572static int nfs41_check_expired_locks(struct nfs4_state *state)
2573{
2574 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002575 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002576 struct nfs_server *server = NFS_SERVER(state->inode);
2577
2578 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2579 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002580
2581 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002582 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2583 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2584 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2585
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002586 atomic_inc(&lsp->ls_count);
2587 spin_unlock(&state->state_lock);
2588
2589 nfs4_put_lock_state(prev);
2590 prev = lsp;
2591
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002592 status = nfs41_test_and_free_expired_stateid(server,
2593 &lsp->ls_stateid,
2594 cred);
2595 trace_nfs4_test_lock_stateid(state, lsp, status);
2596 if (status == -NFS4ERR_EXPIRED ||
2597 status == -NFS4ERR_BAD_STATEID) {
2598 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002599 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002600 if (!recover_lost_locks)
2601 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2602 } else if (status != NFS_OK) {
2603 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002604 nfs4_put_lock_state(prev);
2605 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002606 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002607 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002608 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002609 }
2610 spin_unlock(&state->state_lock);
2611 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002612out:
2613 return ret;
2614}
2615
2616/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002617 * nfs41_check_open_stateid - possibly free an open stateid
2618 *
2619 * @state: NFSv4 state for an inode
2620 *
2621 * Returns NFS_OK if recovery for this stateid is now finished.
2622 * Otherwise a negative NFS4ERR value is returned.
2623 */
2624static int nfs41_check_open_stateid(struct nfs4_state *state)
2625{
2626 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002627 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002628 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002629 int status;
2630
Trond Myklebustb134fc42016-09-22 13:39:16 -04002631 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002632 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2633 if (nfs4_have_delegation(state->inode, state->state))
2634 return NFS_OK;
2635 return -NFS4ERR_OPENMODE;
2636 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002637 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002638 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002639 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002640 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002641 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002642 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2643 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2644 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002645 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002646 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002647 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002648 if (status != NFS_OK)
2649 return status;
2650 if (nfs_open_stateid_recover_openmode(state))
2651 return -NFS4ERR_OPENMODE;
2652 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002653}
2654
2655static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2656{
Chuck Levereb64cf92012-07-11 16:30:05 -04002657 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002658
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002659 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002660 status = nfs41_check_expired_locks(state);
2661 if (status != NFS_OK)
2662 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002663 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002664 if (status != NFS_OK)
2665 status = nfs4_open_expired(sp, state);
2666 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002667}
2668#endif
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002671 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2672 * fields corresponding to attributes that were used to store the verifier.
2673 * Make sure we clobber those fields in the later setattr call
2674 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002675static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2676 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002677{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002678 const u32 *attrset = opendata->o_res.attrset;
2679
2680 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002681 !(sattr->ia_valid & ATTR_ATIME_SET))
2682 sattr->ia_valid |= ATTR_ATIME;
2683
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002684 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002685 !(sattr->ia_valid & ATTR_MTIME_SET))
2686 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002687
2688 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddington56379492017-01-24 11:34:20 -05002689 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2690 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002691 sattr->ia_valid &= ~ATTR_MODE;
2692
2693 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2694 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002695}
2696
Trond Myklebustc21443c2013-02-07 14:26:21 -05002697static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2698 fmode_t fmode,
2699 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002700 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002701{
2702 struct nfs4_state_owner *sp = opendata->owner;
2703 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002704 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002705 struct nfs4_state *state;
2706 unsigned int seq;
2707 int ret;
2708
2709 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2710
2711 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002712 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002713 goto out;
2714
Trond Myklebustb05c4602018-05-22 11:17:16 -04002715 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002716 ret = PTR_ERR(state);
2717 if (IS_ERR(state))
2718 goto out;
Trond Myklebust04653392017-02-08 11:29:46 -05002719 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002720 if (server->caps & NFS_CAP_POSIX_LOCK)
2721 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002722 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2723 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002724
Trond Myklebust275bb302013-05-29 13:11:28 -04002725 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002726 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002727 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002728 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002729 alias = d_exact_alias(dentry, state->inode);
2730 if (!alias)
2731 alias = d_splice_alias(igrab(state->inode), dentry);
2732 /* d_splice_alias() can't fail here - it's a non-directory */
2733 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002734 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002735 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002736 }
2737 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002738 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002739 }
2740
Trond Myklebustc21443c2013-02-07 14:26:21 -05002741 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2742 if (ret != 0)
2743 goto out;
2744
David Howells2b0143b2015-03-17 22:25:59 +00002745 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002746 nfs_inode_attach_open_context(ctx);
2747 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2748 nfs4_schedule_stateid_recovery(server, state);
2749 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002750out:
Trond Myklebustb05c4602018-05-22 11:17:16 -04002751 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002752 return ret;
2753}
2754
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002755/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002756 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 */
Andy Adamson82be4172012-05-23 05:02:35 -04002758static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002759 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002760 int flags,
2761 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002762 struct nfs4_label *label,
2763 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 struct nfs4_state_owner *sp;
2766 struct nfs4_state *state = NULL;
2767 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002768 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002769 struct dentry *dentry = ctx->dentry;
2770 struct rpc_cred *cred = ctx->cred;
2771 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2772 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002773 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002774 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002775 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002779 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2780 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2782 goto out_err;
2783 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002784 status = nfs4_recover_expired_lease(server);
2785 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002786 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002787 if (d_really_is_positive(dentry))
2788 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002789 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002790 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002791 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002792 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002793 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002794 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002795 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
David Quigley14c43f72013-05-22 12:50:43 -04002797 if (label) {
2798 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2799 if (IS_ERR(olabel)) {
2800 status = PTR_ERR(olabel);
2801 goto err_opendata_put;
2802 }
2803 }
2804
Trond Myklebuste911b812014-03-26 13:24:37 -07002805 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2806 if (!opendata->f_attr.mdsthreshold) {
2807 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2808 if (!opendata->f_attr.mdsthreshold)
2809 goto err_free_label;
2810 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002811 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002812 }
David Howells2b0143b2015-03-17 22:25:59 +00002813 if (d_really_is_positive(dentry))
2814 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002815
Trond Myklebust3efb9722013-05-29 13:17:04 -04002816 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002817 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002818 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002819 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002820
NeilBrownefcbc042015-07-30 13:00:56 +10002821 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002822 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002823 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002824 /*
2825 * send create attributes which was not set by open
2826 * with an extra setattr.
2827 */
2828 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2829 nfs_fattr_init(opendata->o_res.f_attr);
2830 status = nfs4_do_setattr(state->inode, cred,
2831 opendata->o_res.f_attr, sattr,
2832 state, label, olabel);
2833 if (status == 0) {
2834 nfs_setattr_update_inode(state->inode, sattr,
2835 opendata->o_res.f_attr);
2836 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2837 }
David Quigley1775fd32013-05-22 12:50:42 -04002838 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002839 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002840 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002841 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002842
Trond Myklebuste911b812014-03-26 13:24:37 -07002843 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002844 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002845 opendata->f_attr.mdsthreshold = NULL;
2846 }
Andy Adamson82be4172012-05-23 05:02:35 -04002847
David Quigley14c43f72013-05-22 12:50:43 -04002848 nfs4_label_free(olabel);
2849
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002850 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002853err_free_label:
2854 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002855err_opendata_put:
2856 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002857err_put_state_owner:
2858 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return status;
2861}
2862
2863
Andy Adamson82be4172012-05-23 05:02:35 -04002864static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002865 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002866 int flags,
2867 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002868 struct nfs4_label *label,
2869 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002871 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 struct nfs4_exception exception = { };
2873 struct nfs4_state *res;
2874 int status;
2875
2876 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002877 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002878 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002879 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (status == 0)
2881 break;
2882 /* NOTE: BAD_SEQID means the server and client disagree about the
2883 * book-keeping w.r.t. state-changing operations
2884 * (OPEN/CLOSE/LOCK/LOCKU...)
2885 * It is actually a sign of a bug on the client or on the server.
2886 *
2887 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002888 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 * have unhashed the old state_owner for us, and that we can
2890 * therefore safely retry using a new one. We should still warn
2891 * the user though...
2892 */
2893 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002894 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002895 " returned a bad sequence-id error!\n",
2896 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 exception.retry = 1;
2898 continue;
2899 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002900 /*
2901 * BAD_STATEID on OPEN means that the server cancelled our
2902 * state before it received the OPEN_CONFIRM.
2903 * Recover by retrying the request as per the discussion
2904 * on Page 181 of RFC3530.
2905 */
2906 if (status == -NFS4ERR_BAD_STATEID) {
2907 exception.retry = 1;
2908 continue;
2909 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002910 if (status == -EAGAIN) {
2911 /* We must have found a delegation */
2912 exception.retry = 1;
2913 continue;
2914 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002915 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2916 continue;
2917 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 status, &exception));
2919 } while (exception.retry);
2920 return res;
2921}
2922
Trond Myklebust8487c472016-06-26 08:44:35 -04002923static int _nfs4_do_setattr(struct inode *inode,
2924 struct nfs_setattrargs *arg,
2925 struct nfs_setattrres *res,
2926 struct rpc_cred *cred,
2927 struct nfs4_state *state)
2928{
2929 struct nfs_server *server = NFS_SERVER(inode);
2930 struct rpc_message msg = {
2931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2932 .rpc_argp = arg,
2933 .rpc_resp = res,
2934 .rpc_cred = cred,
2935 };
2936 struct rpc_cred *delegation_cred = NULL;
2937 unsigned long timestamp = jiffies;
2938 fmode_t fmode;
2939 bool truncate;
2940 int status;
2941
2942 nfs_fattr_init(res->fattr);
2943
2944 /* Servers should only apply open mode checks for file size changes */
2945 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2946 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2947
2948 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2949 /* Use that stateid */
2950 } else if (truncate && state != NULL) {
2951 struct nfs_lockowner lockowner = {
2952 .l_owner = current->files,
2953 .l_pid = current->tgid,
2954 };
2955 if (!nfs4_valid_open_stateid(state))
2956 return -EBADF;
2957 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2958 &arg->stateid, &delegation_cred) == -EIO)
2959 return -EBADF;
2960 } else
2961 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2962 if (delegation_cred)
2963 msg.rpc_cred = delegation_cred;
2964
2965 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2966
2967 put_rpccred(delegation_cred);
2968 if (status == 0 && state != NULL)
2969 renew_lease(server, timestamp);
2970 trace_nfs4_setattr(inode, &arg->stateid, status);
2971 return status;
2972}
2973
2974static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2975 struct nfs_fattr *fattr, struct iattr *sattr,
2976 struct nfs4_state *state, struct nfs4_label *ilabel,
2977 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002979 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002981 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 .iap = sattr,
2983 .server = server,
2984 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002985 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 };
2987 struct nfs_setattrres res = {
2988 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002989 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .server = server,
2991 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002992 struct nfs4_exception exception = {
2993 .state = state,
2994 .inode = inode,
2995 .stateid = &arg.stateid,
2996 };
2997 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
David Quigleyaa9c2662013-05-22 12:50:44 -04002999 arg.bitmask = nfs4_bitmask(server, ilabel);
3000 if (ilabel)
3001 arg.bitmask = nfs4_bitmask(server, olabel);
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003004 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003005 switch (err) {
3006 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003007 if (!(sattr->ia_valid & ATTR_SIZE)) {
3008 pr_warn_once("NFSv4: server %s is incorrectly "
3009 "applying open mode checks to "
3010 "a SETATTR that is not "
3011 "changing file size.\n",
3012 server->nfs_client->cl_hostname);
3013 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003014 if (state && !(state->state & FMODE_WRITE)) {
3015 err = -EBADF;
3016 if (sattr->ia_valid & ATTR_OPEN)
3017 err = -EACCES;
3018 goto out;
3019 }
3020 }
3021 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003023out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 return err;
3025}
3026
Peng Tao500d7012015-09-22 11:35:22 +08003027static bool
3028nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3029{
3030 if (inode == NULL || !nfs_have_layout(inode))
3031 return false;
3032
3033 return pnfs_wait_on_layoutreturn(inode, task);
3034}
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036struct nfs4_closedata {
3037 struct inode *inode;
3038 struct nfs4_state *state;
3039 struct nfs_closeargs arg;
3040 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003041 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003042 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003043 bool roc;
3044 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045};
3046
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003047static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003048{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003049 struct nfs4_closedata *calldata = data;
3050 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003051 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003052
Fred Isamanf7e89172011-01-06 11:36:32 +00003053 if (calldata->roc)
3054 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003055 nfs4_put_open_state(calldata->state);
3056 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003057 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003058 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003059 kfree(calldata);
3060}
3061
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003062static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003064 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003067 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Chuck Levera3ca5652012-03-01 17:00:40 -05003069 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003070 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3071 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003072 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* hmm. we are done with the inode, and in the process of freeing
3074 * the state_owner. we keep this around to process errors
3075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 switch (task->tk_status) {
3077 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003078 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003079 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003080 pnfs_roc_set_barrier(state->inode,
3081 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003082 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003083 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003084 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003086 case -NFS4ERR_EXPIRED:
3087 nfs4_free_revoked_stateid(server,
3088 &calldata->arg.stateid,
3089 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003090 case -NFS4ERR_OLD_STATEID:
3091 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003092 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003093 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003094 rpc_restart_call_prepare(task);
3095 goto out_release;
3096 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003097 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003100 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003101 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003102 goto out_release;
3103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003105 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3106 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003107out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003108 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003109 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003110 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003113static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003115 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003116 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003117 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003118 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003119 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003120
Chuck Levera3ca5652012-03-01 17:00:40 -05003121 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003122 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003123 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003124
Trond Myklebust88069f72009-12-08 08:33:16 -05003125 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003126 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003127 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3128 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3129 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003130 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003131 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003132 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003133 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003134 if (state->n_rdonly == 0)
3135 call_close |= is_rdonly;
3136 else if (is_rdonly)
3137 calldata->arg.fmode |= FMODE_READ;
3138 if (state->n_wronly == 0)
3139 call_close |= is_wronly;
3140 else if (is_wronly)
3141 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003142 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3143 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003144 } else if (is_rdwr)
3145 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3146
Trond Myklebust5cc78612016-11-14 11:19:56 -05003147 if (!nfs4_valid_open_stateid(state) ||
3148 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003149 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003150 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003151
3152 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003153 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003154 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003155 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003156
Peng Tao500d7012015-09-22 11:35:22 +08003157 if (nfs4_wait_on_layoutreturn(inode, task)) {
3158 nfs_release_seqid(calldata->arg.seqid);
3159 goto out_wait;
3160 }
3161
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003162 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003163 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003164 if (calldata->roc)
3165 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3166
Trond Myklebust6ae37332015-01-30 14:21:14 -05003167 calldata->arg.share_access =
3168 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3169 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003170
Trond Myklebust516a6af2005-10-27 22:12:41 -04003171 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003172 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003173 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003174 &calldata->arg.seq_args,
3175 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003176 task) != 0)
3177 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003178 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003179 return;
3180out_no_action:
3181 task->tk_action = NULL;
3182out_wait:
3183 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003186static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003187 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003188 .rpc_call_done = nfs4_close_done,
3189 .rpc_release = nfs4_free_closedata,
3190};
3191
Peng Taofe08c542014-07-03 13:05:01 +08003192static bool nfs4_roc(struct inode *inode)
3193{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003194 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003195 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003196 return pnfs_roc(inode);
3197}
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199/*
3200 * It is possible for data to be read/written from a mem-mapped file
3201 * after the sys_close call (which hits the vfs layer as a flush).
3202 * This means that we can't safely call nfsv4 close on a file until
3203 * the inode is cleared. This in turn means that we are not good
3204 * NFSv4 citizens - we do not indicate to the server to update the file's
3205 * share state even when we are done with one of the three share
3206 * stateid's in the inode.
3207 *
3208 * NOTE: Caller must be holding the sp->so_owner semaphore!
3209 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003210int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003212 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003213 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003215 struct nfs4_state_owner *sp = state->owner;
3216 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003217 struct rpc_message msg = {
3218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3219 .rpc_cred = state->owner->so_cred,
3220 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003221 struct rpc_task_setup task_setup_data = {
3222 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003223 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003224 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003225 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003226 .flags = RPC_TASK_ASYNC,
3227 };
Trond Myklebust95121352005-10-18 14:20:12 -07003228 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003230 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3231 &task_setup_data.rpc_client, &msg);
3232
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003233 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003235 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003236 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003237 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003239 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003241 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3242 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003243 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003244 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003245 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003246 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003247 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003248 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003249 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003250 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003251 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003252
Trond Myklebust1174dd12010-12-21 10:52:24 -05003253 msg.rpc_argp = &calldata->arg;
3254 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003255 task_setup_data.callback_data = calldata;
3256 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003257 if (IS_ERR(task))
3258 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003259 status = 0;
3260 if (wait)
3261 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003262 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003263 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003264out_free_calldata:
3265 kfree(calldata);
3266out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003267 nfs4_put_open_state(state);
3268 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003269 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Trond Myklebust2b484292010-09-17 10:56:51 -04003272static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003273nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3274 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003277 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3278
3279 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Trond Myklebust565277f2007-10-15 18:17:53 -04003281 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003282 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003283
3284 nfs4_label_release_security(label);
3285
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003286 if (IS_ERR(state))
3287 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003288 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Trond Myklebust1185a552009-12-03 15:54:02 -05003291static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003292{
3293 if (ctx->state == NULL)
3294 return;
3295 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003296 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003297 else
Al Viro643168c2011-06-22 18:20:23 -04003298 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
Trond Myklebustb944dba2013-11-04 15:20:20 -05003301#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3302#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003303#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3306{
Kinglong Mee8c612822015-08-26 21:12:58 +08003307 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003308 struct nfs4_server_caps_arg args = {
3309 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003310 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 struct nfs4_server_caps_res res = {};
3313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003315 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 .rpc_resp = &res,
3317 };
3318 int status;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003319 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Kinglong Mee8c612822015-08-26 21:12:58 +08003321 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3322 FATTR4_WORD0_FH_EXPIRE_TYPE |
3323 FATTR4_WORD0_LINK_SUPPORT |
3324 FATTR4_WORD0_SYMLINK_SUPPORT |
3325 FATTR4_WORD0_ACLSUPPORT;
3326 if (minorversion)
3327 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3328
Bryan Schumaker7c513052011-03-24 17:12:24 +00003329 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003331 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003332 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003333 case 0:
3334 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3335 res.attr_bitmask[2] = 0;
3336 break;
3337 case 1:
3338 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3339 break;
3340 case 2:
3341 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003344 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3345 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3346 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3347 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003348 NFS_CAP_CTIME|NFS_CAP_MTIME|
3349 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003350 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3351 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 server->caps |= NFS_CAP_ACLS;
3353 if (res.has_links != 0)
3354 server->caps |= NFS_CAP_HARDLINKS;
3355 if (res.has_symlinks != 0)
3356 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003357 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3358 server->caps |= NFS_CAP_FILEID;
3359 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3360 server->caps |= NFS_CAP_MODE;
3361 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3362 server->caps |= NFS_CAP_NLINK;
3363 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3364 server->caps |= NFS_CAP_OWNER;
3365 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3366 server->caps |= NFS_CAP_OWNER_GROUP;
3367 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3368 server->caps |= NFS_CAP_ATIME;
3369 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3370 server->caps |= NFS_CAP_CTIME;
3371 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3372 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003373#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3374 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3375 server->caps |= NFS_CAP_SECURITY_LABEL;
3376#endif
3377 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3378 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003379 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003380
Trond Myklebusta65318b2009-03-11 14:10:28 -04003381 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3382 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3383 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003384 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003385
3386 /* Avoid a regression due to buggy server */
3387 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3388 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003389 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3390 sizeof(server->exclcreat_bitmask));
Trond Myklebustfca22d52017-05-09 15:47:15 -04003391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003393 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return status;
3397}
3398
Trond Myklebust55a97592006-06-09 09:34:19 -04003399int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
3401 struct nfs4_exception exception = { };
3402 int err;
3403 do {
3404 err = nfs4_handle_exception(server,
3405 _nfs4_server_capabilities(server, fhandle),
3406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
3411static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3412 struct nfs_fsinfo *info)
3413{
David Quigleyaa9c2662013-05-22 12:50:44 -04003414 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003416 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 };
3418 struct nfs4_lookup_res res = {
3419 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003420 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 .fh = fhandle,
3422 };
3423 struct rpc_message msg = {
3424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3425 .rpc_argp = &args,
3426 .rpc_resp = &res,
3427 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003428
David Quigleyaa9c2662013-05-22 12:50:44 -04003429 bitmask[0] = nfs4_fattr_bitmap[0];
3430 bitmask[1] = nfs4_fattr_bitmap[1];
3431 /*
3432 * Process the label in the upcoming getfattr
3433 */
3434 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3435
Trond Myklebust0e574af2005-10-27 22:12:38 -04003436 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003437 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
3440static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3441 struct nfs_fsinfo *info)
3442{
3443 struct nfs4_exception exception = { };
3444 int err;
3445 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003446 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003447 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003448 switch (err) {
3449 case 0:
3450 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003451 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003452 default:
3453 err = nfs4_handle_exception(server, err, &exception);
3454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003456out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 return err;
3458}
3459
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003460static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3461 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3462{
Trond Myklebustc2190662013-08-26 19:23:04 -04003463 struct rpc_auth_create_args auth_args = {
3464 .pseudoflavor = flavor,
3465 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003466 struct rpc_auth *auth;
3467 int ret;
3468
Trond Myklebustc2190662013-08-26 19:23:04 -04003469 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003470 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003471 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003472 goto out;
3473 }
3474 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003475out:
3476 return ret;
3477}
3478
Chuck Lever9a744ba2013-03-16 15:56:02 -04003479/*
3480 * Retry pseudoroot lookup with various security flavors. We do this when:
3481 *
3482 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3483 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3484 *
3485 * Returns zero on success, or a negative NFS4ERR value, or a
3486 * negative errno value.
3487 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003488static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003489 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003491 /* Per 3530bis 15.33.5 */
3492 static const rpc_authflavor_t flav_array[] = {
3493 RPC_AUTH_GSS_KRB5P,
3494 RPC_AUTH_GSS_KRB5I,
3495 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003496 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003497 RPC_AUTH_NULL,
3498 };
3499 int status = -EPERM;
3500 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003502 if (server->auth_info.flavor_len > 0) {
3503 /* try each flavor specified by user */
3504 for (i = 0; i < server->auth_info.flavor_len; i++) {
3505 status = nfs4_lookup_root_sec(server, fhandle, info,
3506 server->auth_info.flavors[i]);
3507 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3508 continue;
3509 break;
3510 }
3511 } else {
3512 /* no flavors specified by user, try default list */
3513 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3514 status = nfs4_lookup_root_sec(server, fhandle, info,
3515 flav_array[i]);
3516 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3517 continue;
3518 break;
3519 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003520 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003521
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003522 /*
3523 * -EACCESS could mean that the user doesn't have correct permissions
3524 * to access the mount. It could also mean that we tried to mount
3525 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3526 * existing mount programs don't handle -EACCES very well so it should
3527 * be mapped to -EPERM instead.
3528 */
3529 if (status == -EACCES)
3530 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003531 return status;
3532}
3533
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003534/**
3535 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3536 * @server: initialized nfs_server handle
3537 * @fhandle: we fill in the pseudo-fs root file handle
3538 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003539 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003540 *
3541 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003542 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003543int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003544 struct nfs_fsinfo *info,
3545 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003546{
Andre Przywarac7757072015-04-23 17:17:40 +01003547 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003548
Andre Przywarac7757072015-04-23 17:17:40 +01003549 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003550 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003551
3552 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003553 status = server->nfs_client->cl_mvops->find_root_sec(server,
3554 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 if (status == 0)
3557 status = nfs4_server_capabilities(server, fhandle);
3558 if (status == 0)
3559 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003560
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003561 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Bryan Schumakerbae36242012-05-10 15:07:31 -04003564static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3565 struct nfs_fsinfo *info)
3566{
3567 int error;
3568 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003569 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003570
3571 error = nfs4_server_capabilities(server, mntfh);
3572 if (error < 0) {
3573 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3574 return error;
3575 }
3576
David Quigley14c43f72013-05-22 12:50:43 -04003577 label = nfs4_label_alloc(server, GFP_KERNEL);
3578 if (IS_ERR(label))
3579 return PTR_ERR(label);
3580
David Quigley1775fd32013-05-22 12:50:42 -04003581 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003582 if (error < 0) {
3583 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003584 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003585 }
3586
3587 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3588 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3589 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3590
David Quigley14c43f72013-05-22 12:50:43 -04003591err_free_label:
3592 nfs4_label_free(label);
3593
Bryan Schumakerbae36242012-05-10 15:07:31 -04003594 return error;
3595}
3596
Manoj Naik6b97fd32006-06-09 09:34:29 -04003597/*
3598 * Get locations and (maybe) other attributes of a referral.
3599 * Note that we'll actually follow the referral later when
3600 * we detect fsid mismatch in inode revalidation
3601 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003602static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3603 const struct qstr *name, struct nfs_fattr *fattr,
3604 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003605{
3606 int status = -ENOMEM;
3607 struct page *page = NULL;
3608 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003609
3610 page = alloc_page(GFP_KERNEL);
3611 if (page == NULL)
3612 goto out;
3613 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3614 if (locations == NULL)
3615 goto out;
3616
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003617 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003618 if (status != 0)
3619 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003620
3621 /*
3622 * If the fsid didn't change, this is a migration event, not a
3623 * referral. Cause us to drop into the exception handler, which
3624 * will kick off migration recovery.
3625 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003626 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003627 dprintk("%s: server did not return a different fsid for"
3628 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003629 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003630 goto out;
3631 }
Andy Adamson533eb462011-06-13 18:25:56 -04003632 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3633 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003634
Andy Adamson533eb462011-06-13 18:25:56 -04003635 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003636 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003637 memset(fhandle, 0, sizeof(struct nfs_fh));
3638out:
3639 if (page)
3640 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003641 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003642 return status;
3643}
3644
David Quigley1775fd32013-05-22 12:50:42 -04003645static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3646 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648 struct nfs4_getattr_arg args = {
3649 .fh = fhandle,
3650 .bitmask = server->attr_bitmask,
3651 };
3652 struct nfs4_getattr_res res = {
3653 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003654 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 .server = server,
3656 };
3657 struct rpc_message msg = {
3658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3659 .rpc_argp = &args,
3660 .rpc_resp = &res,
3661 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003662
3663 args.bitmask = nfs4_bitmask(server, label);
3664
Trond Myklebust0e574af2005-10-27 22:12:38 -04003665 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003666 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
David Quigley1775fd32013-05-22 12:50:42 -04003669static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3670 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
3672 struct nfs4_exception exception = { };
3673 int err;
3674 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003675 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3676 trace_nfs4_getattr(server, fhandle, fattr, err);
3677 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 &exception);
3679 } while (exception.retry);
3680 return err;
3681}
3682
3683/*
3684 * The file is not closed if it is opened due to the a request to change
3685 * the size of the file. The open call will not be needed once the
3686 * VFS layer lookup-intents are implemented.
3687 *
3688 * Close is called when the inode is destroyed.
3689 * If we haven't opened the file for O_WRONLY, we
3690 * need to in the size_change case to obtain a stateid.
3691 *
3692 * Got race?
3693 * Because OPEN is always done by name in nfsv4, it is
3694 * possible that we opened a different file by the same
3695 * name. We can recognize this race condition, but we
3696 * can't do anything about it besides returning an error.
3697 *
3698 * This will be fixed with VFS changes (lookup-intent).
3699 */
3700static int
3701nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3702 struct iattr *sattr)
3703{
David Howells2b0143b2015-03-17 22:25:59 +00003704 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003705 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003706 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003707 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 int status;
3709
Peng Tao88ac8152014-09-12 11:04:10 +08003710 if (pnfs_ld_layoutret_on_setattr(inode) &&
3711 sattr->ia_valid & ATTR_SIZE &&
3712 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003713 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003714
Trond Myklebust0e574af2005-10-27 22:12:38 -04003715 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Andy Adamson26699402012-05-30 16:12:24 -04003717 /* Deal with open(O_TRUNC) */
3718 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003719 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003720
3721 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003722 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003723 return 0;
3724
Trond Myklebustd5308382005-11-04 15:33:38 -05003725 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003726 if (sattr->ia_valid & ATTR_FILE) {
3727 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003728
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003729 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003730 if (ctx) {
3731 cred = ctx->cred;
3732 state = ctx->state;
3733 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003734 }
3735
David Quigley14c43f72013-05-22 12:50:43 -04003736 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3737 if (IS_ERR(label))
3738 return PTR_ERR(label);
3739
3740 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003741 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003742 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003743 nfs_setsecurity(inode, fattr, label);
3744 }
David Quigley14c43f72013-05-22 12:50:43 -04003745 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 return status;
3747}
3748
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003749static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3750 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003751 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003752{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003753 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003754 int status;
3755 struct nfs4_lookup_arg args = {
3756 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003757 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003758 .name = name,
3759 };
3760 struct nfs4_lookup_res res = {
3761 .server = server,
3762 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003763 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003764 .fh = fhandle,
3765 };
3766 struct rpc_message msg = {
3767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3768 .rpc_argp = &args,
3769 .rpc_resp = &res,
3770 };
3771
David Quigleyaa9c2662013-05-22 12:50:44 -04003772 args.bitmask = nfs4_bitmask(server, label);
3773
David Howells2b3de442006-08-22 20:06:09 -04003774 nfs_fattr_init(fattr);
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003777 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 dprintk("NFS reply lookup: %d\n", status);
3779 return status;
3780}
3781
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003782static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003783{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003784 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003785 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003786 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3787 fattr->nlink = 2;
3788}
3789
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003790static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003791 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003792 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003793{
3794 struct nfs4_exception exception = { };
3795 struct rpc_clnt *client = *clnt;
3796 int err;
3797 do {
David Quigley1775fd32013-05-22 12:50:42 -04003798 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003799 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003800 switch (err) {
3801 case -NFS4ERR_BADNAME:
3802 err = -ENOENT;
3803 goto out;
3804 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003805 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003806 if (err == -NFS4ERR_MOVED)
3807 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003808 goto out;
3809 case -NFS4ERR_WRONGSEC:
3810 err = -EPERM;
3811 if (client != *clnt)
3812 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003813 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003814 if (IS_ERR(client))
3815 return PTR_ERR(client);
3816
3817 exception.retry = 1;
3818 break;
3819 default:
3820 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3821 }
3822 } while (exception.retry);
3823
3824out:
3825 if (err == 0)
3826 *clnt = client;
3827 else if (client != *clnt)
3828 rpc_shutdown_client(client);
3829
3830 return err;
3831}
3832
Al Virobeffb8f2016-07-20 16:34:42 -04003833static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003834 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3835 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003837 int status;
3838 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003839
David Quigley1775fd32013-05-22 12:50:42 -04003840 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003841 if (client != NFS_CLIENT(dir)) {
3842 rpc_shutdown_client(client);
3843 nfs_fixup_secinfo_attributes(fattr);
3844 }
3845 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846}
3847
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003848struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003849nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003850 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3851{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003852 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003853 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003854
David Quigley1775fd32013-05-22 12:50:42 -04003855 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003856 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003857 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003858 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003859}
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3862{
Trond Myklebust76b32992007-08-10 17:45:11 -04003863 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 struct nfs4_accessargs args = {
3865 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003866 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003868 struct nfs4_accessres res = {
3869 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003870 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 struct rpc_message msg = {
3872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3873 .rpc_argp = &args,
3874 .rpc_resp = &res,
3875 .rpc_cred = entry->cred,
3876 };
3877 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003878 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
3880 /*
3881 * Determine which access bits we want to ask for...
3882 */
3883 if (mode & MAY_READ)
3884 args.access |= NFS4_ACCESS_READ;
3885 if (S_ISDIR(inode->i_mode)) {
3886 if (mode & MAY_WRITE)
3887 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3888 if (mode & MAY_EXEC)
3889 args.access |= NFS4_ACCESS_LOOKUP;
3890 } else {
3891 if (mode & MAY_WRITE)
3892 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3893 if (mode & MAY_EXEC)
3894 args.access |= NFS4_ACCESS_EXECUTE;
3895 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003896
3897 res.fattr = nfs_alloc_fattr();
3898 if (res.fattr == NULL)
3899 return -ENOMEM;
3900
Bryan Schumaker7c513052011-03-24 17:12:24 +00003901 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003903 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003904 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003906 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return status;
3908}
3909
3910static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3911{
3912 struct nfs4_exception exception = { };
3913 int err;
3914 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003915 err = _nfs4_proc_access(inode, entry);
3916 trace_nfs4_access(inode, err);
3917 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 &exception);
3919 } while (exception.retry);
3920 return err;
3921}
3922
3923/*
3924 * TODO: For the time being, we don't try to get any attributes
3925 * along with any of the zero-copy operations READ, READDIR,
3926 * READLINK, WRITE.
3927 *
3928 * In the case of the first three, we want to put the GETATTR
3929 * after the read-type operation -- this is because it is hard
3930 * to predict the length of a GETATTR response in v4, and thus
3931 * align the READ data correctly. This means that the GETATTR
3932 * may end up partially falling into the page cache, and we should
3933 * shift it into the 'tail' of the xdr_buf before processing.
3934 * To do this efficiently, we need to know the total length
3935 * of data received, which doesn't seem to be available outside
3936 * of the RPC layer.
3937 *
3938 * In the case of WRITE, we also want to put the GETATTR after
3939 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003940 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 *
3942 * Both of these changes to the XDR layer would in fact be quite
3943 * minor, but I decided to leave them for a subsequent patch.
3944 */
3945static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3946 unsigned int pgbase, unsigned int pglen)
3947{
3948 struct nfs4_readlink args = {
3949 .fh = NFS_FH(inode),
3950 .pgbase = pgbase,
3951 .pglen = pglen,
3952 .pages = &page,
3953 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003954 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 struct rpc_message msg = {
3956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3957 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003958 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 };
3960
Bryan Schumaker7c513052011-03-24 17:12:24 +00003961 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 -07003962}
3963
3964static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3965 unsigned int pgbase, unsigned int pglen)
3966{
3967 struct nfs4_exception exception = { };
3968 int err;
3969 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003970 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3971 trace_nfs4_readlink(inode, err);
3972 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 &exception);
3974 } while (exception.retry);
3975 return err;
3976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003979 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981static int
3982nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003983 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
David Quigleyaa9c2662013-05-22 12:50:44 -04003985 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003986 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 int status = 0;
3989
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003990 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3991 if (IS_ERR(ctx))
3992 return PTR_ERR(ctx);
3993
David Quigleyaa9c2662013-05-22 12:50:44 -04003994 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3995
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003996 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003997 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (IS_ERR(state)) {
3999 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004003 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004004 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 return status;
4006}
4007
Al Virobeffb8f2016-07-20 16:34:42 -04004008static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Trond Myklebust16e42952005-10-27 22:12:44 -04004010 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004011 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004013 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004015 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004016 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004017 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4020 .rpc_argp = &args,
4021 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004023 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004024
Bryan Schumaker7c513052011-03-24 17:12:24 +00004025 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004026 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004027 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 return status;
4029}
4030
Al Virobeffb8f2016-07-20 16:34:42 -04004031static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
4033 struct nfs4_exception exception = { };
4034 int err;
4035 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004036 err = _nfs4_proc_remove(dir, name);
4037 trace_nfs4_remove(dir, name, err);
4038 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 &exception);
4040 } while (exception.retry);
4041 return err;
4042}
4043
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004044static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004046 struct nfs_server *server = NFS_SERVER(dir);
4047 struct nfs_removeargs *args = msg->rpc_argp;
4048 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004050 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004052 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004053
4054 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004057static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4058{
Al Viro884be172016-04-28 23:56:31 -04004059 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004060 &data->args.seq_args,
4061 &data->res.seq_res,
4062 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004065static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004067 struct nfs_unlinkdata *data = task->tk_calldata;
4068 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004069
Trond Myklebust14516c32010-07-31 14:29:06 -04004070 if (!nfs4_sequence_done(task, &res->seq_res))
4071 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004072 if (nfs4_async_handle_error(task, res->server, NULL,
4073 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004074 return 0;
4075 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004076 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Jeff Laytond3d41522010-09-17 17:31:57 -04004079static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4080{
4081 struct nfs_server *server = NFS_SERVER(dir);
4082 struct nfs_renameargs *arg = msg->rpc_argp;
4083 struct nfs_renameres *res = msg->rpc_resp;
4084
4085 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004086 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004087 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004088}
4089
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004090static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4091{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004092 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4093 &data->args.seq_args,
4094 &data->res.seq_res,
4095 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004096}
4097
4098static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4099 struct inode *new_dir)
4100{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004101 struct nfs_renamedata *data = task->tk_calldata;
4102 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004103
4104 if (!nfs4_sequence_done(task, &res->seq_res))
4105 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004106 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004107 return 0;
4108
4109 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004110 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004111 return 1;
4112}
4113
Al Virobeffb8f2016-07-20 16:34:42 -04004114static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004116 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 struct nfs4_link_arg arg = {
4118 .fh = NFS_FH(inode),
4119 .dir_fh = NFS_FH(dir),
4120 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004121 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004123 struct nfs4_link_res res = {
4124 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004125 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004126 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 struct rpc_message msg = {
4128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4129 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004130 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004132 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Trond Myklebust136f2622010-04-16 16:22:49 -04004134 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004135 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004136 goto out;
4137
David Quigley14c43f72013-05-22 12:50:43 -04004138 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4139 if (IS_ERR(res.label)) {
4140 status = PTR_ERR(res.label);
4141 goto out;
4142 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004143 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004144
Bryan Schumaker7c513052011-03-24 17:12:24 +00004145 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004146 if (!status) {
4147 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004148 status = nfs_post_op_update_inode(inode, res.fattr);
4149 if (!status)
4150 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004151 }
David Quigley14c43f72013-05-22 12:50:43 -04004152
4153
4154 nfs4_label_free(res.label);
4155
Trond Myklebust136f2622010-04-16 16:22:49 -04004156out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004157 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return status;
4159}
4160
Al Virobeffb8f2016-07-20 16:34:42 -04004161static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
4163 struct nfs4_exception exception = { };
4164 int err;
4165 do {
4166 err = nfs4_handle_exception(NFS_SERVER(inode),
4167 _nfs4_proc_link(inode, dir, name),
4168 &exception);
4169 } while (exception.retry);
4170 return err;
4171}
4172
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004173struct nfs4_createdata {
4174 struct rpc_message msg;
4175 struct nfs4_create_arg arg;
4176 struct nfs4_create_res res;
4177 struct nfs_fh fh;
4178 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004179 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004180};
4181
4182static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004183 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004184{
4185 struct nfs4_createdata *data;
4186
4187 data = kzalloc(sizeof(*data), GFP_KERNEL);
4188 if (data != NULL) {
4189 struct nfs_server *server = NFS_SERVER(dir);
4190
David Quigley14c43f72013-05-22 12:50:43 -04004191 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4192 if (IS_ERR(data->label))
4193 goto out_free;
4194
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004195 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4196 data->msg.rpc_argp = &data->arg;
4197 data->msg.rpc_resp = &data->res;
4198 data->arg.dir_fh = NFS_FH(dir);
4199 data->arg.server = server;
4200 data->arg.name = name;
4201 data->arg.attrs = sattr;
4202 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004203 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204 data->res.server = server;
4205 data->res.fh = &data->fh;
4206 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004207 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004209 }
4210 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004211out_free:
4212 kfree(data);
4213 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004214}
4215
4216static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4217{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004218 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004219 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004220 if (status == 0) {
4221 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004222 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004223 }
4224 return status;
4225}
4226
4227static void nfs4_free_createdata(struct nfs4_createdata *data)
4228{
David Quigley14c43f72013-05-22 12:50:43 -04004229 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004230 kfree(data);
4231}
4232
Chuck Lever4f390c12006-08-22 20:06:22 -04004233static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004234 struct page *page, unsigned int len, struct iattr *sattr,
4235 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 struct nfs4_createdata *data;
4238 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Chuck Lever94a6d752006-08-22 20:06:23 -04004240 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004242
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004243 status = -ENOMEM;
4244 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4245 if (data == NULL)
4246 goto out;
4247
4248 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4249 data->arg.u.symlink.pages = &page;
4250 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004251 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004253 status = nfs4_do_create(dir, dentry, data);
4254
4255 nfs4_free_createdata(data);
4256out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 return status;
4258}
4259
Chuck Lever4f390c12006-08-22 20:06:22 -04004260static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004261 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
4263 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004264 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004266
4267 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004270 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4271 trace_nfs4_symlink(dir, &dentry->d_name, err);
4272 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 &exception);
4274 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004275
4276 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return err;
4278}
4279
4280static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004281 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004283 struct nfs4_createdata *data;
4284 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004286 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4287 if (data == NULL)
4288 goto out;
4289
David Quigley1775fd32013-05-22 12:50:42 -04004290 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004291 status = nfs4_do_create(dir, dentry, data);
4292
4293 nfs4_free_createdata(data);
4294out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return status;
4296}
4297
4298static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4299 struct iattr *sattr)
4300{
4301 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004302 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004304
David Quigleyaa9c2662013-05-22 12:50:44 -04004305 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4306
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004307 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004309 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4310 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4311 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 &exception);
4313 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004314 nfs4_label_release_security(label);
4315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 return err;
4317}
4318
4319static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004320 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
David Howells2b0143b2015-03-17 22:25:59 +00004322 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 struct nfs4_readdir_arg args = {
4324 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004325 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 .pgbase = 0,
4327 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004328 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004329 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 };
4331 struct nfs4_readdir_res res;
4332 struct rpc_message msg = {
4333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4334 .rpc_argp = &args,
4335 .rpc_resp = &res,
4336 .rpc_cred = cred,
4337 };
4338 int status;
4339
Al Viro6de14722013-09-16 10:53:17 -04004340 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4341 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004342 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004343 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004345 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 -05004346 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004347 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004348 status += args.pgbase;
4349 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004350
4351 nfs_invalidate_atime(dir);
4352
Harvey Harrison3110ff82008-05-02 13:42:44 -07004353 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 return status;
4355}
4356
4357static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004358 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
4360 struct nfs4_exception exception = { };
4361 int err;
4362 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004363 err = _nfs4_proc_readdir(dentry, cred, cookie,
4364 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004365 trace_nfs4_readdir(d_inode(dentry), err);
4366 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 &exception);
4368 } while (exception.retry);
4369 return err;
4370}
4371
4372static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004373 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004375 struct nfs4_createdata *data;
4376 int mode = sattr->ia_mode;
4377 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004379 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4380 if (data == NULL)
4381 goto out;
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004384 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 data->arg.ftype = NF4BLK;
4387 data->arg.u.device.specdata1 = MAJOR(rdev);
4388 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
4390 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004391 data->arg.ftype = NF4CHR;
4392 data->arg.u.device.specdata1 = MAJOR(rdev);
4393 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004394 } else if (!S_ISSOCK(mode)) {
4395 status = -EINVAL;
4396 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 }
David Quigley1775fd32013-05-22 12:50:42 -04004398
David Quigleyaa9c2662013-05-22 12:50:44 -04004399 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004400 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004401out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004402 nfs4_free_createdata(data);
4403out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 return status;
4405}
4406
4407static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4408 struct iattr *sattr, dev_t rdev)
4409{
4410 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004411 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004413
David Quigleyaa9c2662013-05-22 12:50:44 -04004414 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4415
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004416 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004418 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4419 trace_nfs4_mknod(dir, &dentry->d_name, err);
4420 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 &exception);
4422 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004423
4424 nfs4_label_release_security(label);
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 return err;
4427}
4428
4429static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4430 struct nfs_fsstat *fsstat)
4431{
4432 struct nfs4_statfs_arg args = {
4433 .fh = fhandle,
4434 .bitmask = server->attr_bitmask,
4435 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004436 struct nfs4_statfs_res res = {
4437 .fsstat = fsstat,
4438 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 struct rpc_message msg = {
4440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4441 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004442 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 };
4444
Trond Myklebust0e574af2005-10-27 22:12:38 -04004445 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004446 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
4449static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4450{
4451 struct nfs4_exception exception = { };
4452 int err;
4453 do {
4454 err = nfs4_handle_exception(server,
4455 _nfs4_proc_statfs(server, fhandle, fsstat),
4456 &exception);
4457 } while (exception.retry);
4458 return err;
4459}
4460
4461static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4462 struct nfs_fsinfo *fsinfo)
4463{
4464 struct nfs4_fsinfo_arg args = {
4465 .fh = fhandle,
4466 .bitmask = server->attr_bitmask,
4467 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004468 struct nfs4_fsinfo_res res = {
4469 .fsinfo = fsinfo,
4470 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 struct rpc_message msg = {
4472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4473 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004474 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 };
4476
Bryan Schumaker7c513052011-03-24 17:12:24 +00004477 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478}
4479
4480static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4481{
4482 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004483 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 int err;
4485
4486 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004487 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004488 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004489 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004490 nfs4_set_lease_period(server->nfs_client,
4491 fsinfo->lease_time * HZ,
4492 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004493 break;
4494 }
4495 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 } while (exception.retry);
4497 return err;
4498}
4499
4500static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4501{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004502 int error;
4503
Trond Myklebust0e574af2005-10-27 22:12:38 -04004504 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004505 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004506 if (error == 0) {
4507 /* block layout checks this! */
4508 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004509 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004510 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004511
4512 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
4514
4515static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4516 struct nfs_pathconf *pathconf)
4517{
4518 struct nfs4_pathconf_arg args = {
4519 .fh = fhandle,
4520 .bitmask = server->attr_bitmask,
4521 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004522 struct nfs4_pathconf_res res = {
4523 .pathconf = pathconf,
4524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 struct rpc_message msg = {
4526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4527 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004528 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 };
4530
4531 /* None of the pathconf attributes are mandatory to implement */
4532 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4533 memset(pathconf, 0, sizeof(*pathconf));
4534 return 0;
4535 }
4536
Trond Myklebust0e574af2005-10-27 22:12:38 -04004537 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004538 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539}
4540
4541static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4542 struct nfs_pathconf *pathconf)
4543{
4544 struct nfs4_exception exception = { };
4545 int err;
4546
4547 do {
4548 err = nfs4_handle_exception(server,
4549 _nfs4_proc_pathconf(server, fhandle, pathconf),
4550 &exception);
4551 } while (exception.retry);
4552 return err;
4553}
4554
Trond Myklebust5521abf2013-03-16 20:54:34 -04004555int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004556 const struct nfs_open_context *ctx,
4557 const struct nfs_lock_context *l_ctx,
4558 fmode_t fmode)
4559{
4560 const struct nfs_lockowner *lockowner = NULL;
4561
4562 if (l_ctx != NULL)
4563 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004564 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004565}
4566EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4567
Trond Myklebust5521abf2013-03-16 20:54:34 -04004568static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4569 const struct nfs_open_context *ctx,
4570 const struct nfs_lock_context *l_ctx,
4571 fmode_t fmode)
4572{
4573 nfs4_stateid current_stateid;
4574
Trond Myklebuste1253be2014-03-05 08:44:23 -05004575 /* If the current stateid represents a lost lock, then exit */
4576 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4577 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004578 return nfs4_stateid_match(stateid, &current_stateid);
4579}
4580
4581static bool nfs4_error_stateid_expired(int err)
4582{
4583 switch (err) {
4584 case -NFS4ERR_DELEG_REVOKED:
4585 case -NFS4ERR_ADMIN_REVOKED:
4586 case -NFS4ERR_BAD_STATEID:
4587 case -NFS4ERR_STALE_STATEID:
4588 case -NFS4ERR_OLD_STATEID:
4589 case -NFS4ERR_OPENMODE:
4590 case -NFS4ERR_EXPIRED:
4591 return true;
4592 }
4593 return false;
4594}
4595
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004596static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004598 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004600 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004601 if (task->tk_status < 0) {
4602 struct nfs4_exception exception = {
4603 .inode = hdr->inode,
4604 .state = hdr->args.context->state,
4605 .stateid = &hdr->args.stateid,
4606 };
4607 task->tk_status = nfs4_async_handle_exception(task,
4608 server, task->tk_status, &exception);
4609 if (exception.retry) {
4610 rpc_restart_call_prepare(task);
4611 return -EAGAIN;
4612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004616 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618}
4619
Trond Myklebust5521abf2013-03-16 20:54:34 -04004620static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004621 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004622{
4623
4624 if (!nfs4_error_stateid_expired(task->tk_status) ||
4625 nfs4_stateid_is_current(&args->stateid,
4626 args->context,
4627 args->lock_context,
4628 FMODE_READ))
4629 return false;
4630 rpc_restart_call_prepare(task);
4631 return true;
4632}
4633
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004634static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004635{
4636
4637 dprintk("--> %s\n", __func__);
4638
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004639 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004640 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004641 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004642 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004643 if (task->tk_status > 0)
4644 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004645 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4646 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004647}
4648
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004649static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4650 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004652 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004653 if (!hdr->pgio_done_cb)
4654 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004655 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004656 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
4658
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004659static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4660 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004661{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004662 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4663 &hdr->args.seq_args,
4664 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004665 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004666 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4668 hdr->args.lock_context,
4669 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004670 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004672 return -EIO;
4673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674}
4675
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004676static int nfs4_write_done_cb(struct rpc_task *task,
4677 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004680
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004682 if (task->tk_status < 0) {
4683 struct nfs4_exception exception = {
4684 .inode = hdr->inode,
4685 .state = hdr->args.context->state,
4686 .stateid = &hdr->args.stateid,
4687 };
4688 task->tk_status = nfs4_async_handle_exception(task,
4689 NFS_SERVER(inode), task->tk_status,
4690 &exception);
4691 if (exception.retry) {
4692 rpc_restart_call_prepare(task);
4693 return -EAGAIN;
4694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004696 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004698 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004699 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
Trond Myklebust5521abf2013-03-16 20:54:34 -04004703static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004704 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004705{
4706
4707 if (!nfs4_error_stateid_expired(task->tk_status) ||
4708 nfs4_stateid_is_current(&args->stateid,
4709 args->context,
4710 args->lock_context,
4711 FMODE_WRITE))
4712 return false;
4713 rpc_restart_call_prepare(task);
4714 return true;
4715}
4716
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004717static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004718{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004720 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004721 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004722 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004723 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4724 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004725}
4726
Trond Myklebust5a37f852012-04-28 14:55:16 -04004727static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004729{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004730 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004731 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004732 return false;
4733 /* Otherwise, request attributes if and only if we don't hold
4734 * a delegation
4735 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004736 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004737}
Fred Isamana69aef12011-03-03 15:13:47 +00004738
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004739static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4740 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004742 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004743
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004744 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4745 hdr->args.bitmask = NULL;
4746 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004747 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004748 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004749
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004750 if (!hdr->pgio_done_cb)
4751 hdr->pgio_done_cb = nfs4_write_done_cb;
4752 hdr->res.server = server;
4753 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004755 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004756 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757}
4758
Fred Isaman0b7c0152012-04-20 14:47:39 -04004759static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4760{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004761 nfs4_setup_sequence(NFS_SERVER(data->inode),
4762 &data->args.seq_args,
4763 &data->res.seq_res,
4764 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
Fred Isaman0b7c0152012-04-20 14:47:39 -04004767static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004770
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004771 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004772 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4773 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004774 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004775 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
Fred Isaman0b7c0152012-04-20 14:47:39 -04004780static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004781{
4782 if (!nfs4_sequence_done(task, &data->res.seq_res))
4783 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004784 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004785}
4786
Fred Isaman0b7c0152012-04-20 14:47:39 -04004787static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004789 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004790
Fred Isaman0b7c0152012-04-20 14:47:39 -04004791 if (data->commit_done_cb == NULL)
4792 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004793 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004794 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004795 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004798struct nfs4_renewdata {
4799 struct nfs_client *client;
4800 unsigned long timestamp;
4801};
4802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803/*
4804 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4805 * standalone procedure for queueing an asynchronous RENEW.
4806 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004807static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004808{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004809 struct nfs4_renewdata *data = calldata;
4810 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004811
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004812 if (atomic_read(&clp->cl_count) > 1)
4813 nfs4_schedule_state_renewal(clp);
4814 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004815 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004816}
4817
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004818static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004820 struct nfs4_renewdata *data = calldata;
4821 struct nfs_client *clp = data->client;
4822 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004824 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004825 switch (task->tk_status) {
4826 case 0:
4827 break;
4828 case -NFS4ERR_LEASE_MOVED:
4829 nfs4_schedule_lease_moved_recovery(clp);
4830 break;
4831 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004832 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004833 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4834 return;
4835 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004836 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004837 return;
4838 }
4839 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004841 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842}
4843
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004844static const struct rpc_call_ops nfs4_renew_ops = {
4845 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004846 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004847};
4848
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004849static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
4851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4853 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004854 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004856 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004858 if (renew_flags == 0)
4859 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004860 if (!atomic_inc_not_zero(&clp->cl_count))
4861 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004862 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004863 if (data == NULL)
4864 return -ENOMEM;
4865 data->client = clp;
4866 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004867 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004868 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004871static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
4873 struct rpc_message msg = {
4874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4875 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004876 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 };
4878 unsigned long now = jiffies;
4879 int status;
4880
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004881 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 if (status < 0)
4883 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004884 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 return 0;
4886}
4887
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004888static inline int nfs4_server_supports_acls(struct nfs_server *server)
4889{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004890 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004891}
4892
Trond Myklebust21f498c2012-08-24 10:59:25 -04004893/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4894 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004895 * the stack.
4896 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004897#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004898
Neil Hormane9e3d722011-03-04 19:26:03 -05004899static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004900 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004901{
4902 struct page *newpage, **spages;
4903 int rc = 0;
4904 size_t len;
4905 spages = pages;
4906
4907 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004908 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004909 newpage = alloc_page(GFP_KERNEL);
4910
4911 if (newpage == NULL)
4912 goto unwind;
4913 memcpy(page_address(newpage), buf, len);
4914 buf += len;
4915 buflen -= len;
4916 *pages++ = newpage;
4917 rc++;
4918 } while (buflen != 0);
4919
4920 return rc;
4921
4922unwind:
4923 for(; rc > 0; rc--)
4924 __free_page(spages[rc-1]);
4925 return -ENOMEM;
4926}
4927
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004928struct nfs4_cached_acl {
4929 int cached;
4930 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004931 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004932};
4933
4934static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004935{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004936 struct nfs_inode *nfsi = NFS_I(inode);
4937
4938 spin_lock(&inode->i_lock);
4939 kfree(nfsi->nfs4_acl);
4940 nfsi->nfs4_acl = acl;
4941 spin_unlock(&inode->i_lock);
4942}
4943
4944static void nfs4_zap_acl_attr(struct inode *inode)
4945{
4946 nfs4_set_cached_acl(inode, NULL);
4947}
4948
4949static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4950{
4951 struct nfs_inode *nfsi = NFS_I(inode);
4952 struct nfs4_cached_acl *acl;
4953 int ret = -ENOENT;
4954
4955 spin_lock(&inode->i_lock);
4956 acl = nfsi->nfs4_acl;
4957 if (acl == NULL)
4958 goto out;
4959 if (buf == NULL) /* user is just asking for length */
4960 goto out_len;
4961 if (acl->cached == 0)
4962 goto out;
4963 ret = -ERANGE; /* see getxattr(2) man page */
4964 if (acl->len > buflen)
4965 goto out;
4966 memcpy(buf, acl->data, acl->len);
4967out_len:
4968 ret = acl->len;
4969out:
4970 spin_unlock(&inode->i_lock);
4971 return ret;
4972}
4973
Sachin Prabhu5794d212012-04-17 14:36:40 +01004974static 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 +00004975{
4976 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004977 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004978
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004979 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004980 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004981 if (acl == NULL)
4982 goto out;
4983 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004984 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004985 } else {
4986 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4987 if (acl == NULL)
4988 goto out;
4989 acl->cached = 0;
4990 }
4991 acl->len = acl_len;
4992out:
4993 nfs4_set_cached_acl(inode, acl);
4994}
4995
Andy Adamsonbf118a32011-12-07 11:55:27 -05004996/*
4997 * The getxattr API returns the required buffer length when called with a
4998 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4999 * the required buf. On a NULL buf, we send a page of data to the server
5000 * guessing that the ACL request can be serviced by a page. If so, we cache
5001 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5002 * the cache. If not so, we throw away the page, and cache the required
5003 * length. The next getxattr call will then produce another round trip to
5004 * the server, this time with the input buf of the required size.
5005 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005006static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005007{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005008 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005009 struct nfs_getaclargs args = {
5010 .fh = NFS_FH(inode),
5011 .acl_pages = pages,
5012 .acl_len = buflen,
5013 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005014 struct nfs_getaclres res = {
5015 .acl_len = buflen,
5016 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5019 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005020 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005021 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005022 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005023 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005024
Trond Myklebust21f498c2012-08-24 10:59:25 -04005025 if (npages > ARRAY_SIZE(pages))
5026 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005027
Andy Adamsonbf118a32011-12-07 11:55:27 -05005028 for (i = 0; i < npages; i++) {
5029 pages[i] = alloc_page(GFP_KERNEL);
5030 if (!pages[i])
5031 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005032 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005033
5034 /* for decoding across pages */
5035 res.acl_scratch = alloc_page(GFP_KERNEL);
5036 if (!res.acl_scratch)
5037 goto out_free;
5038
Andy Adamsonbf118a32011-12-07 11:55:27 -05005039 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005040
Peng Taode040be2012-01-10 22:42:47 +08005041 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005042 __func__, buf, buflen, npages, args.acl_len);
5043 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5044 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045 if (ret)
5046 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005047
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005048 /* Handle the case where the passed-in buffer is too short */
5049 if (res.acl_flags & NFS4_ACL_TRUNC) {
5050 /* Did the user only issue a request for the acl length? */
5051 if (buf == NULL)
5052 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005053 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005054 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005055 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005056 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005057 if (buf) {
5058 if (res.acl_len > buflen) {
5059 ret = -ERANGE;
5060 goto out_free;
5061 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005062 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005063 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005064out_ok:
5065 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005066out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005067 for (i = 0; i < npages; i++)
5068 if (pages[i])
5069 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005070 if (res.acl_scratch)
5071 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005072 return ret;
5073}
5074
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005075static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5076{
5077 struct nfs4_exception exception = { };
5078 ssize_t ret;
5079 do {
5080 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005081 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005082 if (ret >= 0)
5083 break;
5084 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5085 } while (exception.retry);
5086 return ret;
5087}
5088
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5090{
5091 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005092 int ret;
5093
5094 if (!nfs4_server_supports_acls(server))
5095 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005096 ret = nfs_revalidate_inode(server, inode);
5097 if (ret < 0)
5098 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005099 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5100 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005101 ret = nfs4_read_cached_acl(inode, buf, buflen);
5102 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005103 /* -ENOENT is returned if there is no ACL or if there is an ACL
5104 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005105 return ret;
5106 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005107}
5108
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005109static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005110{
5111 struct nfs_server *server = NFS_SERVER(inode);
5112 struct page *pages[NFS4ACL_MAXPAGES];
5113 struct nfs_setaclargs arg = {
5114 .fh = NFS_FH(inode),
5115 .acl_pages = pages,
5116 .acl_len = buflen,
5117 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005118 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005119 struct rpc_message msg = {
5120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5121 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005122 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005123 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005124 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005125 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005126
5127 if (!nfs4_server_supports_acls(server))
5128 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005129 if (npages > ARRAY_SIZE(pages))
5130 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005131 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005132 if (i < 0)
5133 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005134 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005135 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005136
5137 /*
5138 * Free each page after tx, so the only ref left is
5139 * held by the network stack
5140 */
5141 for (; i > 0; i--)
5142 put_page(pages[i-1]);
5143
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005144 /*
5145 * Acl update can result in inode attribute update.
5146 * so mark the attribute cache invalid.
5147 */
5148 spin_lock(&inode->i_lock);
5149 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5150 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005151 nfs_access_zap_cache(inode);
5152 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005153 return ret;
5154}
5155
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005156static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5157{
5158 struct nfs4_exception exception = { };
5159 int err;
5160 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005161 err = __nfs4_proc_set_acl(inode, buf, buflen);
5162 trace_nfs4_set_acl(inode, err);
5163 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005164 &exception);
5165 } while (exception.retry);
5166 return err;
5167}
5168
David Quigleyaa9c2662013-05-22 12:50:44 -04005169#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5170static int _nfs4_get_security_label(struct inode *inode, void *buf,
5171 size_t buflen)
5172{
5173 struct nfs_server *server = NFS_SERVER(inode);
5174 struct nfs_fattr fattr;
5175 struct nfs4_label label = {0, 0, buflen, buf};
5176
5177 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005178 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005179 .fh = NFS_FH(inode),
5180 .bitmask = bitmask,
5181 };
5182 struct nfs4_getattr_res res = {
5183 .fattr = &fattr,
5184 .label = &label,
5185 .server = server,
5186 };
5187 struct rpc_message msg = {
5188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005189 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005190 .rpc_resp = &res,
5191 };
5192 int ret;
5193
5194 nfs_fattr_init(&fattr);
5195
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005196 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005197 if (ret)
5198 return ret;
5199 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5200 return -ENOENT;
5201 if (buflen < label.len)
5202 return -ERANGE;
5203 return 0;
5204}
5205
5206static int nfs4_get_security_label(struct inode *inode, void *buf,
5207 size_t buflen)
5208{
5209 struct nfs4_exception exception = { };
5210 int err;
5211
5212 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5213 return -EOPNOTSUPP;
5214
5215 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005216 err = _nfs4_get_security_label(inode, buf, buflen);
5217 trace_nfs4_get_security_label(inode, err);
5218 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005219 &exception);
5220 } while (exception.retry);
5221 return err;
5222}
5223
5224static int _nfs4_do_set_security_label(struct inode *inode,
5225 struct nfs4_label *ilabel,
5226 struct nfs_fattr *fattr,
5227 struct nfs4_label *olabel)
5228{
5229
5230 struct iattr sattr = {0};
5231 struct nfs_server *server = NFS_SERVER(inode);
5232 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005233 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005234 .fh = NFS_FH(inode),
5235 .iap = &sattr,
5236 .server = server,
5237 .bitmask = bitmask,
5238 .label = ilabel,
5239 };
5240 struct nfs_setattrres res = {
5241 .fattr = fattr,
5242 .label = olabel,
5243 .server = server,
5244 };
5245 struct rpc_message msg = {
5246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005247 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005248 .rpc_resp = &res,
5249 };
5250 int status;
5251
Jeff Layton12207f62013-11-01 10:49:32 -04005252 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005253
Jeff Layton12207f62013-11-01 10:49:32 -04005254 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005255 if (status)
5256 dprintk("%s failed: %d\n", __func__, status);
5257
5258 return status;
5259}
5260
5261static int nfs4_do_set_security_label(struct inode *inode,
5262 struct nfs4_label *ilabel,
5263 struct nfs_fattr *fattr,
5264 struct nfs4_label *olabel)
5265{
5266 struct nfs4_exception exception = { };
5267 int err;
5268
5269 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005270 err = _nfs4_do_set_security_label(inode, ilabel,
5271 fattr, olabel);
5272 trace_nfs4_set_security_label(inode, err);
5273 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005274 &exception);
5275 } while (exception.retry);
5276 return err;
5277}
5278
5279static int
Al Viro59301222016-05-27 10:19:30 -04005280nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005281{
5282 struct nfs4_label ilabel, *olabel = NULL;
5283 struct nfs_fattr fattr;
5284 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005285 int status;
5286
5287 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5288 return -EOPNOTSUPP;
5289
5290 nfs_fattr_init(&fattr);
5291
5292 ilabel.pi = 0;
5293 ilabel.lfs = 0;
5294 ilabel.label = (char *)buf;
5295 ilabel.len = buflen;
5296
5297 cred = rpc_lookup_cred();
5298 if (IS_ERR(cred))
5299 return PTR_ERR(cred);
5300
5301 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5302 if (IS_ERR(olabel)) {
5303 status = -PTR_ERR(olabel);
5304 goto out;
5305 }
5306
5307 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5308 if (status == 0)
5309 nfs_setsecurity(inode, &fattr, olabel);
5310
5311 nfs4_label_free(olabel);
5312out:
5313 put_rpccred(cred);
5314 return status;
5315}
5316#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5317
5318
Chuck Leverf0920752012-05-21 22:45:41 -04005319static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5320 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005321{
5322 __be32 verf[2];
5323
Chuck Lever2c820d92012-05-21 22:45:33 -04005324 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5325 /* An impossible timestamp guarantees this value
5326 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005327 verf[0] = cpu_to_be32(U32_MAX);
5328 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005329 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005330 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005331 u64 ns = ktime_to_ns(nn->boot_time);
5332
5333 verf[0] = cpu_to_be32(ns >> 32);
5334 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005335 }
Chuck Levercd937102012-03-02 17:14:31 -05005336 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5337}
5338
Jeff Laytona3192682015-06-09 19:43:59 -04005339static int
5340nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005341{
Jeff Laytona3192682015-06-09 19:43:59 -04005342 size_t len;
5343 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005344
Trond Myklebustceb3a162015-01-03 15:16:04 -05005345 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005346 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005347
Jeff Laytona3192682015-06-09 19:43:59 -04005348 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005349 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005350 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5351 1 +
5352 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5353 1;
5354 rcu_read_unlock();
5355
5356 if (len > NFS4_OPAQUE_LIMIT + 1)
5357 return -EINVAL;
5358
5359 /*
5360 * Since this string is allocated at mount time, and held until the
5361 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5362 * about a memory-reclaim deadlock.
5363 */
5364 str = kmalloc(len, GFP_KERNEL);
5365 if (!str)
5366 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005367
Chuck Levere984a552012-09-14 17:24:21 -04005368 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005369 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005370 clp->cl_ipaddr,
5371 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5372 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005373 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005374
Jeff Laytona3192682015-06-09 19:43:59 -04005375 clp->cl_owner_id = str;
5376 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005377}
5378
Jeff Layton873e3852015-06-09 19:44:00 -04005379static int
5380nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005381{
Jeff Layton873e3852015-06-09 19:44:00 -04005382 size_t len;
5383 char *str;
5384
5385 len = 10 + 10 + 1 + 10 + 1 +
5386 strlen(nfs4_client_id_uniquifier) + 1 +
5387 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5388
5389 if (len > NFS4_OPAQUE_LIMIT + 1)
5390 return -EINVAL;
5391
5392 /*
5393 * Since this string is allocated at mount time, and held until the
5394 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5395 * about a memory-reclaim deadlock.
5396 */
5397 str = kmalloc(len, GFP_KERNEL);
5398 if (!str)
5399 return -ENOMEM;
5400
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005401 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005402 clp->rpc_ops->version, clp->cl_minorversion,
5403 nfs4_client_id_uniquifier,
5404 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005405 clp->cl_owner_id = str;
5406 return 0;
5407}
5408
5409static int
5410nfs4_init_uniform_client_string(struct nfs_client *clp)
5411{
Jeff Layton873e3852015-06-09 19:44:00 -04005412 size_t len;
5413 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005414
5415 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005416 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005417
5418 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005419 return nfs4_init_uniquifier_client_string(clp);
5420
5421 len = 10 + 10 + 1 + 10 + 1 +
5422 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5423
5424 if (len > NFS4_OPAQUE_LIMIT + 1)
5425 return -EINVAL;
5426
5427 /*
5428 * Since this string is allocated at mount time, and held until the
5429 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5430 * about a memory-reclaim deadlock.
5431 */
5432 str = kmalloc(len, GFP_KERNEL);
5433 if (!str)
5434 return -ENOMEM;
5435
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005436 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005437 clp->rpc_ops->version, clp->cl_minorversion,
5438 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005439 clp->cl_owner_id = str;
5440 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005441}
5442
Chuck Lever706cb8d2014-03-12 12:51:47 -04005443/*
5444 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5445 * services. Advertise one based on the address family of the
5446 * clientaddr.
5447 */
5448static unsigned int
5449nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5450{
5451 if (strchr(clp->cl_ipaddr, ':') != NULL)
5452 return scnprintf(buf, len, "tcp6");
5453 else
5454 return scnprintf(buf, len, "tcp");
5455}
5456
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005457static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5458{
5459 struct nfs4_setclientid *sc = calldata;
5460
5461 if (task->tk_status == 0)
5462 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5463}
5464
5465static const struct rpc_call_ops nfs4_setclientid_ops = {
5466 .rpc_call_done = nfs4_setclientid_done,
5467};
5468
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005469/**
5470 * nfs4_proc_setclientid - Negotiate client ID
5471 * @clp: state data structure
5472 * @program: RPC program for NFSv4 callback service
5473 * @port: IP port number for NFS4 callback service
5474 * @cred: RPC credential to use for this call
5475 * @res: where to place the result
5476 *
5477 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5478 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005479int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5480 unsigned short port, struct rpc_cred *cred,
5481 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
5483 nfs4_verifier sc_verifier;
5484 struct nfs4_setclientid setclientid = {
5485 .sc_verifier = &sc_verifier,
5486 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005487 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 };
5489 struct rpc_message msg = {
5490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5491 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005492 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005493 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005495 struct rpc_task *task;
5496 struct rpc_task_setup task_setup_data = {
5497 .rpc_client = clp->cl_rpcclient,
5498 .rpc_message = &msg,
5499 .callback_ops = &nfs4_setclientid_ops,
5500 .callback_data = &setclientid,
5501 .flags = RPC_TASK_TIMEOUT,
5502 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005503 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Chuck Leverde734832012-07-11 16:30:50 -04005505 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005506 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005507
5508 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5509 status = nfs4_init_uniform_client_string(clp);
5510 else
Jeff Laytona3192682015-06-09 19:43:59 -04005511 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005512
5513 if (status)
5514 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005515
Chuck Leverde734832012-07-11 16:30:50 -04005516 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005517 setclientid.sc_netid_len =
5518 nfs4_init_callback_netid(clp,
5519 setclientid.sc_netid,
5520 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005521 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005522 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 clp->cl_ipaddr, port >> 8, port & 255);
5524
Jeff Layton3a6bb732015-06-09 19:43:57 -04005525 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005526 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005527 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005528 task = rpc_run_task(&task_setup_data);
5529 if (IS_ERR(task)) {
5530 status = PTR_ERR(task);
5531 goto out;
5532 }
5533 status = task->tk_status;
5534 if (setclientid.sc_cred) {
5535 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5536 put_rpccred(setclientid.sc_cred);
5537 }
5538 rpc_put_task(task);
5539out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005540 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005541 dprintk("NFS reply setclientid: %d\n", status);
5542 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543}
5544
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005545/**
5546 * nfs4_proc_setclientid_confirm - Confirm client ID
5547 * @clp: state data structure
5548 * @res: result of a previous SETCLIENTID
5549 * @cred: RPC credential to use for this call
5550 *
5551 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5552 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005553int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005554 struct nfs4_setclientid_res *arg,
5555 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 struct rpc_message msg = {
5558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005559 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005560 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 int status;
5563
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005564 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5565 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5566 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005567 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005568 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005569 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return status;
5571}
5572
Trond Myklebustfe650402006-01-03 09:55:18 +01005573struct nfs4_delegreturndata {
5574 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005575 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005576 struct nfs_fh fh;
5577 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005578 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005579 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005580 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005581 struct inode *inode;
5582 bool roc;
5583 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005584};
5585
Trond Myklebustfe650402006-01-03 09:55:18 +01005586static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5587{
5588 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005589
Trond Myklebust14516c32010-07-31 14:29:06 -04005590 if (!nfs4_sequence_done(task, &data->res.seq_res))
5591 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005592
Trond Myklebustca8acf82013-08-13 10:36:56 -04005593 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005594 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005595 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005596 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005597 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005598 case -NFS4ERR_ADMIN_REVOKED:
5599 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005600 case -NFS4ERR_EXPIRED:
5601 nfs4_free_revoked_stateid(data->res.server,
5602 data->args.stateid,
5603 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005604 case -NFS4ERR_BAD_STATEID:
5605 case -NFS4ERR_OLD_STATEID:
5606 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005607 task->tk_status = 0;
5608 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005609 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005610 if (nfs4_async_handle_error(task, data->res.server,
5611 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005612 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005613 return;
5614 }
5615 }
5616 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005617 if (data->roc && data->rpc_status == 0)
5618 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005619}
5620
5621static void nfs4_delegreturn_release(void *calldata)
5622{
Peng Tao039b7562014-07-03 13:05:02 +08005623 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005624 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005625
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005626 if (inode) {
5627 if (data->roc)
5628 pnfs_roc_release(inode);
5629 nfs_iput_and_deactive(inode);
5630 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005631 kfree(calldata);
5632}
5633
Andy Adamson938e1012009-04-01 09:22:28 -04005634static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5635{
5636 struct nfs4_delegreturndata *d_data;
5637
5638 d_data = (struct nfs4_delegreturndata *)data;
5639
Peng Tao500d7012015-09-22 11:35:22 +08005640 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5641 return;
5642
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005643 if (d_data->roc)
5644 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005645
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005646 nfs4_setup_sequence(d_data->res.server,
5647 &d_data->args.seq_args,
5648 &d_data->res.seq_res,
5649 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005650}
Andy Adamson938e1012009-04-01 09:22:28 -04005651
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005652static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005653 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005654 .rpc_call_done = nfs4_delegreturn_done,
5655 .rpc_release = nfs4_delegreturn_release,
5656};
5657
Trond Myklebuste6f81072008-01-24 18:14:34 -05005658static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005659{
5660 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005661 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005662 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005663 struct rpc_message msg = {
5664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5665 .rpc_cred = cred,
5666 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005667 struct rpc_task_setup task_setup_data = {
5668 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005669 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005670 .callback_ops = &nfs4_delegreturn_ops,
5671 .flags = RPC_TASK_ASYNC,
5672 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005673 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005674
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005675 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005676 if (data == NULL)
5677 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005678 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005679
5680 nfs4_state_protect(server->nfs_client,
5681 NFS_SP4_MACH_CRED_CLEANUP,
5682 &task_setup_data.rpc_client, &msg);
5683
Trond Myklebustfe650402006-01-03 09:55:18 +01005684 data->args.fhandle = &data->fh;
5685 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005686 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005687 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005688 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005689 data->res.fattr = &data->fattr;
5690 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005691 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005692 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005693 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005694 data->inode = nfs_igrab_and_active(inode);
5695 if (data->inode)
5696 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005697
Trond Myklebustc970aa82007-07-14 15:39:59 -04005698 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005699 msg.rpc_argp = &data->args;
5700 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005701 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005702 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005703 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005704 if (!issync)
5705 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005706 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005707 if (status != 0)
5708 goto out;
5709 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005710 if (status == 0)
5711 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5712 else
5713 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005714out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005715 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005716 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717}
5718
Trond Myklebuste6f81072008-01-24 18:14:34 -05005719int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720{
5721 struct nfs_server *server = NFS_SERVER(inode);
5722 struct nfs4_exception exception = { };
5723 int err;
5724 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005725 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005726 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 switch (err) {
5728 case -NFS4ERR_STALE_STATEID:
5729 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 case 0:
5731 return 0;
5732 }
5733 err = nfs4_handle_exception(server, err, &exception);
5734 } while (exception.retry);
5735 return err;
5736}
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5739{
5740 struct inode *inode = state->inode;
5741 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005742 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005743 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005745 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005747 struct nfs_lockt_res res = {
5748 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 };
5750 struct rpc_message msg = {
5751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5752 .rpc_argp = &arg,
5753 .rpc_resp = &res,
5754 .rpc_cred = state->owner->so_cred,
5755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 struct nfs4_lock_state *lsp;
5757 int status;
5758
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005759 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005760 status = nfs4_set_lock_state(state, request);
5761 if (status != 0)
5762 goto out;
5763 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005764 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005765 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005766 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005767 switch (status) {
5768 case 0:
5769 request->fl_type = F_UNLCK;
5770 break;
5771 case -NFS4ERR_DENIED:
5772 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005774 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005775 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005776out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 return status;
5778}
5779
5780static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5781{
5782 struct nfs4_exception exception = { };
5783 int err;
5784
5785 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005786 err = _nfs4_proc_getlk(state, cmd, request);
5787 trace_nfs4_get_lock(request, state, cmd, err);
5788 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 &exception);
5790 } while (exception.retry);
5791 return err;
5792}
5793
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005794struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005795 struct nfs_locku_args arg;
5796 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005797 struct nfs4_lock_state *lsp;
5798 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005799 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005800 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005801 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005802};
5803
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005804static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5805 struct nfs_open_context *ctx,
5806 struct nfs4_lock_state *lsp,
5807 struct nfs_seqid *seqid)
5808{
5809 struct nfs4_unlockdata *p;
5810 struct inode *inode = lsp->ls_state->inode;
5811
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005812 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005813 if (p == NULL)
5814 return NULL;
5815 p->arg.fh = NFS_FH(inode);
5816 p->arg.fl = &p->fl;
5817 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005818 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005819 p->lsp = lsp;
5820 atomic_inc(&lsp->ls_count);
5821 /* Ensure we don't close file until we're done freeing locks! */
5822 p->ctx = get_nfs_open_context(ctx);
5823 memcpy(&p->fl, fl, sizeof(p->fl));
5824 p->server = NFS_SERVER(inode);
5825 return p;
5826}
5827
Trond Myklebust06f814a2006-01-03 09:55:07 +01005828static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005829{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005830 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005831 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005832 nfs4_put_lock_state(calldata->lsp);
5833 put_nfs_open_context(calldata->ctx);
5834 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005835}
5836
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005837static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005838{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005839 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005840
Trond Myklebust14516c32010-07-31 14:29:06 -04005841 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5842 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005843 switch (task->tk_status) {
5844 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005845 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005846 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005847 if (nfs4_update_lock_stateid(calldata->lsp,
5848 &calldata->res.stateid))
5849 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005850 case -NFS4ERR_ADMIN_REVOKED:
5851 case -NFS4ERR_EXPIRED:
5852 nfs4_free_revoked_stateid(calldata->server,
5853 &calldata->arg.stateid,
5854 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005855 case -NFS4ERR_BAD_STATEID:
5856 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005857 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005858 if (!nfs4_stateid_match(&calldata->arg.stateid,
5859 &calldata->lsp->ls_stateid))
5860 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005861 break;
5862 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005863 if (nfs4_async_handle_error(task, calldata->server,
5864 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005865 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005866 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005867 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005868}
5869
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005870static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005871{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005872 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005874 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005875 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005876 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005877 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005878 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005879 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005880 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005881 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005882 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005883 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005884 &calldata->res.seq_res,
5885 task) != 0)
5886 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005887 return;
5888out_no_action:
5889 task->tk_action = NULL;
5890out_wait:
5891 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892}
5893
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005894static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005895 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005896 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005897 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005898};
5899
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005900static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5901 struct nfs_open_context *ctx,
5902 struct nfs4_lock_state *lsp,
5903 struct nfs_seqid *seqid)
5904{
5905 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005906 struct rpc_message msg = {
5907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5908 .rpc_cred = ctx->cred,
5909 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005910 struct rpc_task_setup task_setup_data = {
5911 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005912 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005913 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005914 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005915 .flags = RPC_TASK_ASYNC,
5916 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005917
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005918 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5919 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5920
Frank Filz137d6ac2007-07-09 15:32:29 -07005921 /* Ensure this is an unlock - when canceling a lock, the
5922 * canceled lock is passed in, and it won't be an unlock.
5923 */
5924 fl->fl_type = F_UNLCK;
5925
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005926 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5927 if (data == NULL) {
5928 nfs_free_seqid(seqid);
5929 return ERR_PTR(-ENOMEM);
5930 }
5931
Chuck Levera9c92d62013-08-09 12:48:18 -04005932 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005933 msg.rpc_argp = &data->arg;
5934 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005935 task_setup_data.callback_data = data;
5936 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005937}
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5940{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005941 struct inode *inode = state->inode;
5942 struct nfs4_state_owner *sp = state->owner;
5943 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005944 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005945 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005946 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005947 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005948 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005949 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
Trond Myklebust9b073572006-06-29 16:38:34 -04005951 status = nfs4_set_lock_state(state, request);
5952 /* Unlock _before_ we do the RPC call */
5953 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005954 /* Exclude nfs_delegation_claim_locks() */
5955 mutex_lock(&sp->so_delegreturn_mutex);
5956 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005957 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005958 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005959 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005960 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005961 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005962 }
5963 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005964 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005965 if (status != 0)
5966 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005967 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005968 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005969 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5970 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005971 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5972 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005973 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005974 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005975 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005976 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977 status = PTR_ERR(task);
5978 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005979 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005980 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005981 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005982out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005983 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005984 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005985 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986}
5987
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005988struct nfs4_lockdata {
5989 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005990 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005991 struct nfs4_lock_state *lsp;
5992 struct nfs_open_context *ctx;
5993 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005994 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995 int rpc_status;
5996 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005997 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005998};
5999
6000static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006001 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6002 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003{
6004 struct nfs4_lockdata *p;
6005 struct inode *inode = lsp->ls_state->inode;
6006 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006007 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006009 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010 if (p == NULL)
6011 return NULL;
6012
6013 p->arg.fh = NFS_FH(inode);
6014 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006015 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006016 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006017 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006018 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6019 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006020 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006021 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006022 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006023 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006024 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006025 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006026 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006027 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006028 atomic_inc(&lsp->ls_count);
6029 p->ctx = get_nfs_open_context(ctx);
6030 memcpy(&p->fl, fl, sizeof(p->fl));
6031 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006032out_free_seqid:
6033 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006034out_free:
6035 kfree(p);
6036 return NULL;
6037}
6038
6039static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6040{
6041 struct nfs4_lockdata *data = calldata;
6042 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
Harvey Harrison3110ff82008-05-02 13:42:44 -07006044 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006045 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006046 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006047 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006048 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006049 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006050 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006051 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006052 nfs4_stateid_copy(&data->arg.open_stateid,
6053 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006054 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006055 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006056 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006057 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006058 nfs4_stateid_copy(&data->arg.lock_stateid,
6059 &data->lsp->ls_stateid);
6060 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006061 if (!nfs4_valid_open_stateid(state)) {
6062 data->rpc_status = -EBADF;
6063 task->tk_action = NULL;
6064 goto out_release_open_seqid;
6065 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006066 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006067 if (nfs4_setup_sequence(data->server,
6068 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006069 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006070 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006071 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006072out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006073 nfs_release_seqid(data->arg.open_seqid);
6074out_release_lock_seqid:
6075 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006076out_wait:
6077 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006078 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006079}
6080
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006081static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6082{
6083 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006084 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006085
Harvey Harrison3110ff82008-05-02 13:42:44 -07006086 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087
Trond Myklebust14516c32010-07-31 14:29:06 -04006088 if (!nfs4_sequence_done(task, &data->res.seq_res))
6089 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006090
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006092 switch (task->tk_status) {
6093 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006094 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006095 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006096 if (data->arg.new_lock) {
6097 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006098 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006099 rpc_restart_call_prepare(task);
6100 break;
6101 }
6102 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006103 if (data->arg.new_lock_owner != 0) {
6104 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6105 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6106 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6107 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6108 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006109 break;
6110 case -NFS4ERR_BAD_STATEID:
6111 case -NFS4ERR_OLD_STATEID:
6112 case -NFS4ERR_STALE_STATEID:
6113 case -NFS4ERR_EXPIRED:
6114 if (data->arg.new_lock_owner != 0) {
6115 if (!nfs4_stateid_match(&data->arg.open_stateid,
6116 &lsp->ls_state->open_stateid))
6117 rpc_restart_call_prepare(task);
6118 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6119 &lsp->ls_stateid))
6120 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006121 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006122 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006123}
6124
6125static void nfs4_lock_release(void *calldata)
6126{
6127 struct nfs4_lockdata *data = calldata;
6128
Harvey Harrison3110ff82008-05-02 13:42:44 -07006129 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006130 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006131 if (data->cancelled != 0) {
6132 struct rpc_task *task;
6133 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6134 data->arg.lock_seqid);
6135 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006136 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006137 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138 } else
6139 nfs_free_seqid(data->arg.lock_seqid);
6140 nfs4_put_lock_state(data->lsp);
6141 put_nfs_open_context(data->ctx);
6142 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006143 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006144}
6145
6146static const struct rpc_call_ops nfs4_lock_ops = {
6147 .rpc_call_prepare = nfs4_lock_prepare,
6148 .rpc_call_done = nfs4_lock_done,
6149 .rpc_release = nfs4_lock_release,
6150};
6151
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006152static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6153{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006154 switch (error) {
6155 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006156 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006157 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006158 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006159 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006160 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006161 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006162 break;
6163 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006164 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006165 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006166 };
6167}
6168
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006169static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170{
6171 struct nfs4_lockdata *data;
6172 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006173 struct rpc_message msg = {
6174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6175 .rpc_cred = state->owner->so_cred,
6176 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006177 struct rpc_task_setup task_setup_data = {
6178 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006179 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006180 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006181 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006182 .flags = RPC_TASK_ASYNC,
6183 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006184 int ret;
6185
Harvey Harrison3110ff82008-05-02 13:42:44 -07006186 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006187 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006188 fl->fl_u.nfs4_fl.owner,
6189 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006190 if (data == NULL)
6191 return -ENOMEM;
6192 if (IS_SETLKW(cmd))
6193 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006194 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006195 msg.rpc_argp = &data->arg;
6196 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006197 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006198 if (recovery_type > NFS_LOCK_NEW) {
6199 if (recovery_type == NFS_LOCK_RECLAIM)
6200 data->arg.reclaim = NFS_LOCK_RECLAIM;
6201 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006202 } else
6203 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006204 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006205 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006206 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006207 ret = nfs4_wait_for_completion_rpc_task(task);
6208 if (ret == 0) {
6209 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006210 if (ret)
6211 nfs4_handle_setlk_error(data->server, data->lsp,
6212 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006213 } else
6214 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006215 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006216 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006217 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219}
6220
6221static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6222{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006223 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006224 struct nfs4_exception exception = {
6225 .inode = state->inode,
6226 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006227 int err;
6228
6229 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006230 /* Cache the lock if possible... */
6231 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6232 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006233 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006234 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006235 break;
6236 nfs4_handle_exception(server, err, &exception);
6237 } while (exception.retry);
6238 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
6240
6241static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6242{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006243 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006244 struct nfs4_exception exception = {
6245 .inode = state->inode,
6246 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006247 int err;
6248
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006249 err = nfs4_set_lock_state(state, request);
6250 if (err != 0)
6251 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006252 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006253 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6254 return 0;
6255 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006256 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006257 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6258 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006259 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006260 switch (err) {
6261 default:
6262 goto out;
6263 case -NFS4ERR_GRACE:
6264 case -NFS4ERR_DELAY:
6265 nfs4_handle_exception(server, err, &exception);
6266 err = 0;
6267 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006268 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006269out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271}
6272
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006273#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006274static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6275{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006276 struct nfs4_lock_state *lsp;
6277 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006278
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006279 status = nfs4_set_lock_state(state, request);
6280 if (status != 0)
6281 return status;
6282 lsp = request->fl_u.nfs4_fl.owner;
6283 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6284 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6285 return 0;
6286 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006287 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006288}
6289#endif
6290
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6292{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006293 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006294 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006295 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006296 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Trond Myklebust01c3b862006-06-29 16:38:39 -04006298 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006299 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006300 if (status < 0)
6301 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006302 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006303 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006304 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006305 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006306 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006307 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006308 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006309 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006310 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006311 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006312 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006313 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006314 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006315 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006316out:
6317 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 return status;
6319}
6320
6321static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6322{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006323 struct nfs4_exception exception = {
6324 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006325 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006326 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 int err;
6328
6329 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006330 err = _nfs4_proc_setlk(state, cmd, request);
6331 if (err == -NFS4ERR_DENIED)
6332 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006334 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 } while (exception.retry);
6336 return err;
6337}
6338
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006339#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6340#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6341
6342static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006343nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6344 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006345{
6346 int status = -ERESTARTSYS;
6347 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6348
6349 while(!signalled()) {
6350 status = nfs4_proc_setlk(state, cmd, request);
6351 if ((status != -EAGAIN) || IS_SETLK(cmd))
6352 break;
6353 freezable_schedule_timeout_interruptible(timeout);
6354 timeout *= 2;
6355 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6356 status = -ERESTARTSYS;
6357 }
6358 return status;
6359}
6360
Jeff Laytona1d617d82016-09-17 18:17:39 -04006361#ifdef CONFIG_NFS_V4_1
6362struct nfs4_lock_waiter {
6363 struct task_struct *task;
6364 struct inode *inode;
6365 struct nfs_lowner *owner;
6366 bool notified;
6367};
6368
6369static int
6370nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6371{
6372 int ret;
6373 struct cb_notify_lock_args *cbnl = key;
6374 struct nfs4_lock_waiter *waiter = wait->private;
6375 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6376 *wowner = waiter->owner;
6377
6378 /* Only wake if the callback was for the same owner */
6379 if (lowner->clientid != wowner->clientid ||
6380 lowner->id != wowner->id ||
6381 lowner->s_dev != wowner->s_dev)
6382 return 0;
6383
6384 /* Make sure it's for the right inode */
6385 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6386 return 0;
6387
6388 waiter->notified = true;
6389
6390 /* override "private" so we can use default_wake_function */
6391 wait->private = waiter->task;
6392 ret = autoremove_wake_function(wait, mode, flags, key);
6393 wait->private = waiter;
6394 return ret;
6395}
6396
6397static int
6398nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6399{
6400 int status = -ERESTARTSYS;
6401 unsigned long flags;
6402 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6403 struct nfs_server *server = NFS_SERVER(state->inode);
6404 struct nfs_client *clp = server->nfs_client;
6405 wait_queue_head_t *q = &clp->cl_lock_waitq;
6406 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6407 .id = lsp->ls_seqid.owner_id,
6408 .s_dev = server->s_dev };
6409 struct nfs4_lock_waiter waiter = { .task = current,
6410 .inode = state->inode,
6411 .owner = &owner,
6412 .notified = false };
6413 wait_queue_t wait;
6414
6415 /* Don't bother with waitqueue if we don't expect a callback */
6416 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6417 return nfs4_retry_setlk_simple(state, cmd, request);
6418
6419 init_wait(&wait);
6420 wait.private = &waiter;
6421 wait.func = nfs4_wake_lock_waiter;
6422 add_wait_queue(q, &wait);
6423
6424 while(!signalled()) {
6425 status = nfs4_proc_setlk(state, cmd, request);
6426 if ((status != -EAGAIN) || IS_SETLK(cmd))
6427 break;
6428
6429 status = -ERESTARTSYS;
6430 spin_lock_irqsave(&q->lock, flags);
6431 if (waiter.notified) {
6432 spin_unlock_irqrestore(&q->lock, flags);
6433 continue;
6434 }
6435 set_current_state(TASK_INTERRUPTIBLE);
6436 spin_unlock_irqrestore(&q->lock, flags);
6437
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006438 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006439 }
6440
6441 finish_wait(q, &wait);
6442 return status;
6443}
6444#else /* !CONFIG_NFS_V4_1 */
6445static inline int
6446nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6447{
6448 return nfs4_retry_setlk_simple(state, cmd, request);
6449}
6450#endif
6451
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452static int
6453nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6454{
6455 struct nfs_open_context *ctx;
6456 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 int status;
6458
6459 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006460 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 state = ctx->state;
6462
6463 if (request->fl_start < 0 || request->fl_end < 0)
6464 return -EINVAL;
6465
Trond Myklebustd9531262009-07-21 19:22:38 -04006466 if (IS_GETLK(cmd)) {
6467 if (state != NULL)
6468 return nfs4_proc_getlk(state, F_GETLK, request);
6469 return 0;
6470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471
6472 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6473 return -EINVAL;
6474
Trond Myklebustd9531262009-07-21 19:22:38 -04006475 if (request->fl_type == F_UNLCK) {
6476 if (state != NULL)
6477 return nfs4_proc_unlck(state, cmd, request);
6478 return 0;
6479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
Trond Myklebustd9531262009-07-21 19:22:38 -04006481 if (state == NULL)
6482 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006483
6484 if ((request->fl_flags & FL_POSIX) &&
6485 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6486 return -ENOLCK;
6487
Trond Myklebust55725512012-04-18 12:48:35 -04006488 /*
6489 * Don't rely on the VFS having checked the file open mode,
6490 * since it won't do this for flock() locks.
6491 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006492 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006493 case F_RDLCK:
6494 if (!(filp->f_mode & FMODE_READ))
6495 return -EBADF;
6496 break;
6497 case F_WRLCK:
6498 if (!(filp->f_mode & FMODE_WRITE))
6499 return -EBADF;
6500 }
6501
Jeff Layton1ea67db2016-09-17 18:17:37 -04006502 status = nfs4_set_lock_state(state, request);
6503 if (status != 0)
6504 return status;
6505
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006506 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507}
6508
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006509int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006510{
6511 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006512 int err;
6513
6514 err = nfs4_set_lock_state(state, fl);
6515 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006516 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006517 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrown70f254a2017-12-13 09:57:09 +11006518 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006519}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006520
Trond Myklebustcf470c32012-03-07 13:49:12 -05006521struct nfs_release_lockowner_data {
6522 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006523 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006524 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006525 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006526 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006527};
6528
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006529static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6530{
6531 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006532 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006533 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6534 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006535 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006536 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006537}
6538
6539static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6540{
6541 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006542 struct nfs_server *server = data->server;
6543
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006544 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006545
6546 switch (task->tk_status) {
6547 case 0:
6548 renew_lease(server, data->timestamp);
6549 break;
6550 case -NFS4ERR_STALE_CLIENTID:
6551 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006552 nfs4_schedule_lease_recovery(server->nfs_client);
6553 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006554 case -NFS4ERR_LEASE_MOVED:
6555 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006556 if (nfs4_async_handle_error(task, server,
6557 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006558 rpc_restart_call_prepare(task);
6559 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006560}
6561
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006562static void nfs4_release_lockowner_release(void *calldata)
6563{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006564 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006565 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006566 kfree(calldata);
6567}
6568
Trond Myklebust17280172012-03-11 13:11:00 -04006569static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006570 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6571 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006572 .rpc_release = nfs4_release_lockowner_release,
6573};
6574
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006575static void
6576nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006577{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006578 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006579 struct rpc_message msg = {
6580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6581 };
6582
6583 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006584 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006585
Trond Myklebustcf470c32012-03-07 13:49:12 -05006586 data = kmalloc(sizeof(*data), GFP_NOFS);
6587 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006588 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006589 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006590 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006591 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6592 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6593 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006594
Trond Myklebustcf470c32012-03-07 13:49:12 -05006595 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006596 msg.rpc_resp = &data->res;
6597 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006598 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006599}
6600
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006601#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6602
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006603static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006604 struct dentry *unused, struct inode *inode,
6605 const char *key, const void *buf,
6606 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006607{
Al Viro59301222016-05-27 10:19:30 -04006608 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006609}
6610
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006611static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006612 struct dentry *unused, struct inode *inode,
6613 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006614{
Al Virob2968212016-04-10 20:48:24 -04006615 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006616}
6617
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006618static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006619{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006620 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006621}
6622
David Quigleyc9bccef2013-05-22 12:50:45 -04006623#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006624
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006625static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006626 struct dentry *unused, struct inode *inode,
6627 const char *key, const void *buf,
6628 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006629{
6630 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006631 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006632
6633 return -EOPNOTSUPP;
6634}
6635
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006636static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006637 struct dentry *unused, struct inode *inode,
6638 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006639{
6640 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006641 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006642 return -EOPNOTSUPP;
6643}
6644
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006645static ssize_t
6646nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006647{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006648 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006649
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006650 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6651 len = security_inode_listsecurity(inode, list, list_len);
6652 if (list_len && len > list_len)
6653 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006654 }
6655 return len;
6656}
6657
6658static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6659 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006660 .get = nfs4_xattr_get_nfs4_label,
6661 .set = nfs4_xattr_set_nfs4_label,
6662};
David Quigleyc9bccef2013-05-22 12:50:45 -04006663
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006664#else
6665
6666static ssize_t
6667nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6668{
6669 return 0;
6670}
6671
6672#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006673
Andy Adamson533eb462011-06-13 18:25:56 -04006674/*
6675 * nfs_fhget will use either the mounted_on_fileid or the fileid
6676 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006677static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6678{
Andy Adamson533eb462011-06-13 18:25:56 -04006679 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6680 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6681 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006682 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006683 return;
6684
6685 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006686 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006687 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6688 fattr->nlink = 2;
6689}
6690
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006691static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6692 const struct qstr *name,
6693 struct nfs4_fs_locations *fs_locations,
6694 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006695{
6696 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006697 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006698 struct nfs4_fs_locations_arg args = {
6699 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006700 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006701 .page = page,
6702 .bitmask = bitmask,
6703 };
Benny Halevy22958462009-04-01 09:22:02 -04006704 struct nfs4_fs_locations_res res = {
6705 .fs_locations = fs_locations,
6706 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006707 struct rpc_message msg = {
6708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6709 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006710 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006711 };
6712 int status;
6713
Harvey Harrison3110ff82008-05-02 13:42:44 -07006714 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006715
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006716 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6717 bitmask[1] = nfs4_fattr_bitmap[1];
6718
Andy Adamson533eb462011-06-13 18:25:56 -04006719 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6720 * is not supported */
6721 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006722 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006723 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006724 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006725
Trond Myklebustc228fd32007-01-13 02:28:11 -05006726 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006727 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006728 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006729 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006730 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006731 return status;
6732}
6733
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006734int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6735 const struct qstr *name,
6736 struct nfs4_fs_locations *fs_locations,
6737 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006738{
6739 struct nfs4_exception exception = { };
6740 int err;
6741 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006742 err = _nfs4_proc_fs_locations(client, dir, name,
6743 fs_locations, page);
6744 trace_nfs4_get_fs_locations(dir, name, err);
6745 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006746 &exception);
6747 } while (exception.retry);
6748 return err;
6749}
6750
Chuck Leverb03d7352013-10-17 14:12:50 -04006751/*
6752 * This operation also signals the server that this client is
6753 * performing migration recovery. The server can stop returning
6754 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6755 * appended to this compound to identify the client ID which is
6756 * performing recovery.
6757 */
6758static int _nfs40_proc_get_locations(struct inode *inode,
6759 struct nfs4_fs_locations *locations,
6760 struct page *page, struct rpc_cred *cred)
6761{
6762 struct nfs_server *server = NFS_SERVER(inode);
6763 struct rpc_clnt *clnt = server->client;
6764 u32 bitmask[2] = {
6765 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6766 };
6767 struct nfs4_fs_locations_arg args = {
6768 .clientid = server->nfs_client->cl_clientid,
6769 .fh = NFS_FH(inode),
6770 .page = page,
6771 .bitmask = bitmask,
6772 .migration = 1, /* skip LOOKUP */
6773 .renew = 1, /* append RENEW */
6774 };
6775 struct nfs4_fs_locations_res res = {
6776 .fs_locations = locations,
6777 .migration = 1,
6778 .renew = 1,
6779 };
6780 struct rpc_message msg = {
6781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6782 .rpc_argp = &args,
6783 .rpc_resp = &res,
6784 .rpc_cred = cred,
6785 };
6786 unsigned long now = jiffies;
6787 int status;
6788
6789 nfs_fattr_init(&locations->fattr);
6790 locations->server = server;
6791 locations->nlocations = 0;
6792
6793 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6794 nfs4_set_sequence_privileged(&args.seq_args);
6795 status = nfs4_call_sync_sequence(clnt, server, &msg,
6796 &args.seq_args, &res.seq_res);
6797 if (status)
6798 return status;
6799
6800 renew_lease(server, now);
6801 return 0;
6802}
6803
6804#ifdef CONFIG_NFS_V4_1
6805
6806/*
6807 * This operation also signals the server that this client is
6808 * performing migration recovery. The server can stop asserting
6809 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6810 * performing this operation is identified in the SEQUENCE
6811 * operation in this compound.
6812 *
6813 * When the client supports GETATTR(fs_locations_info), it can
6814 * be plumbed in here.
6815 */
6816static int _nfs41_proc_get_locations(struct inode *inode,
6817 struct nfs4_fs_locations *locations,
6818 struct page *page, struct rpc_cred *cred)
6819{
6820 struct nfs_server *server = NFS_SERVER(inode);
6821 struct rpc_clnt *clnt = server->client;
6822 u32 bitmask[2] = {
6823 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6824 };
6825 struct nfs4_fs_locations_arg args = {
6826 .fh = NFS_FH(inode),
6827 .page = page,
6828 .bitmask = bitmask,
6829 .migration = 1, /* skip LOOKUP */
6830 };
6831 struct nfs4_fs_locations_res res = {
6832 .fs_locations = locations,
6833 .migration = 1,
6834 };
6835 struct rpc_message msg = {
6836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6837 .rpc_argp = &args,
6838 .rpc_resp = &res,
6839 .rpc_cred = cred,
6840 };
6841 int status;
6842
6843 nfs_fattr_init(&locations->fattr);
6844 locations->server = server;
6845 locations->nlocations = 0;
6846
6847 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6848 nfs4_set_sequence_privileged(&args.seq_args);
6849 status = nfs4_call_sync_sequence(clnt, server, &msg,
6850 &args.seq_args, &res.seq_res);
6851 if (status == NFS4_OK &&
6852 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6853 status = -NFS4ERR_LEASE_MOVED;
6854 return status;
6855}
6856
6857#endif /* CONFIG_NFS_V4_1 */
6858
6859/**
6860 * nfs4_proc_get_locations - discover locations for a migrated FSID
6861 * @inode: inode on FSID that is migrating
6862 * @locations: result of query
6863 * @page: buffer
6864 * @cred: credential to use for this operation
6865 *
6866 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6867 * operation failed, or a negative errno if a local error occurred.
6868 *
6869 * On success, "locations" is filled in, but if the server has
6870 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6871 * asserted.
6872 *
6873 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6874 * from this client that require migration recovery.
6875 */
6876int nfs4_proc_get_locations(struct inode *inode,
6877 struct nfs4_fs_locations *locations,
6878 struct page *page, struct rpc_cred *cred)
6879{
6880 struct nfs_server *server = NFS_SERVER(inode);
6881 struct nfs_client *clp = server->nfs_client;
6882 const struct nfs4_mig_recovery_ops *ops =
6883 clp->cl_mvops->mig_recovery_ops;
6884 struct nfs4_exception exception = { };
6885 int status;
6886
6887 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6888 (unsigned long long)server->fsid.major,
6889 (unsigned long long)server->fsid.minor,
6890 clp->cl_hostname);
6891 nfs_display_fhandle(NFS_FH(inode), __func__);
6892
6893 do {
6894 status = ops->get_locations(inode, locations, page, cred);
6895 if (status != -NFS4ERR_DELAY)
6896 break;
6897 nfs4_handle_exception(server, status, &exception);
6898 } while (exception.retry);
6899 return status;
6900}
6901
Chuck Lever44c99932013-10-17 14:13:30 -04006902/*
6903 * This operation also signals the server that this client is
6904 * performing "lease moved" recovery. The server can stop
6905 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6906 * is appended to this compound to identify the client ID which is
6907 * performing recovery.
6908 */
6909static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6910{
6911 struct nfs_server *server = NFS_SERVER(inode);
6912 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6913 struct rpc_clnt *clnt = server->client;
6914 struct nfs4_fsid_present_arg args = {
6915 .fh = NFS_FH(inode),
6916 .clientid = clp->cl_clientid,
6917 .renew = 1, /* append RENEW */
6918 };
6919 struct nfs4_fsid_present_res res = {
6920 .renew = 1,
6921 };
6922 struct rpc_message msg = {
6923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6924 .rpc_argp = &args,
6925 .rpc_resp = &res,
6926 .rpc_cred = cred,
6927 };
6928 unsigned long now = jiffies;
6929 int status;
6930
6931 res.fh = nfs_alloc_fhandle();
6932 if (res.fh == NULL)
6933 return -ENOMEM;
6934
6935 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6936 nfs4_set_sequence_privileged(&args.seq_args);
6937 status = nfs4_call_sync_sequence(clnt, server, &msg,
6938 &args.seq_args, &res.seq_res);
6939 nfs_free_fhandle(res.fh);
6940 if (status)
6941 return status;
6942
6943 do_renew_lease(clp, now);
6944 return 0;
6945}
6946
6947#ifdef CONFIG_NFS_V4_1
6948
6949/*
6950 * This operation also signals the server that this client is
6951 * performing "lease moved" recovery. The server can stop asserting
6952 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6953 * this operation is identified in the SEQUENCE operation in this
6954 * compound.
6955 */
6956static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6957{
6958 struct nfs_server *server = NFS_SERVER(inode);
6959 struct rpc_clnt *clnt = server->client;
6960 struct nfs4_fsid_present_arg args = {
6961 .fh = NFS_FH(inode),
6962 };
6963 struct nfs4_fsid_present_res res = {
6964 };
6965 struct rpc_message msg = {
6966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6967 .rpc_argp = &args,
6968 .rpc_resp = &res,
6969 .rpc_cred = cred,
6970 };
6971 int status;
6972
6973 res.fh = nfs_alloc_fhandle();
6974 if (res.fh == NULL)
6975 return -ENOMEM;
6976
6977 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6978 nfs4_set_sequence_privileged(&args.seq_args);
6979 status = nfs4_call_sync_sequence(clnt, server, &msg,
6980 &args.seq_args, &res.seq_res);
6981 nfs_free_fhandle(res.fh);
6982 if (status == NFS4_OK &&
6983 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6984 status = -NFS4ERR_LEASE_MOVED;
6985 return status;
6986}
6987
6988#endif /* CONFIG_NFS_V4_1 */
6989
6990/**
6991 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6992 * @inode: inode on FSID to check
6993 * @cred: credential to use for this operation
6994 *
6995 * Server indicates whether the FSID is present, moved, or not
6996 * recognized. This operation is necessary to clear a LEASE_MOVED
6997 * condition for this client ID.
6998 *
6999 * Returns NFS4_OK if the FSID is present on this server,
7000 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7001 * NFS4ERR code if some error occurred on the server, or a
7002 * negative errno if a local failure occurred.
7003 */
7004int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7005{
7006 struct nfs_server *server = NFS_SERVER(inode);
7007 struct nfs_client *clp = server->nfs_client;
7008 const struct nfs4_mig_recovery_ops *ops =
7009 clp->cl_mvops->mig_recovery_ops;
7010 struct nfs4_exception exception = { };
7011 int status;
7012
7013 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7014 (unsigned long long)server->fsid.major,
7015 (unsigned long long)server->fsid.minor,
7016 clp->cl_hostname);
7017 nfs_display_fhandle(NFS_FH(inode), __func__);
7018
7019 do {
7020 status = ops->fsid_present(inode, cred);
7021 if (status != -NFS4ERR_DELAY)
7022 break;
7023 nfs4_handle_exception(server, status, &exception);
7024 } while (exception.retry);
7025 return status;
7026}
7027
Andy Adamson5ec16a82013-08-08 10:57:55 -04007028/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007029 * If 'use_integrity' is true and the state managment nfs_client
7030 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7031 * and the machine credential as per RFC3530bis and RFC5661 Security
7032 * Considerations sections. Otherwise, just use the user cred with the
7033 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007034 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007035static 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 +00007036{
7037 int status;
7038 struct nfs4_secinfo_arg args = {
7039 .dir_fh = NFS_FH(dir),
7040 .name = name,
7041 };
7042 struct nfs4_secinfo_res res = {
7043 .flavors = flavors,
7044 };
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7047 .rpc_argp = &args,
7048 .rpc_resp = &res,
7049 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007050 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007051 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007052
7053 if (use_integrity) {
7054 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007055 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7056 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007057 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007058
7059 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007060
7061 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7062 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7063
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007064 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7065 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007066 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007067
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007068 if (cred)
7069 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007070
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007071 return status;
7072}
7073
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007074int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7075 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007076{
7077 struct nfs4_exception exception = { };
7078 int err;
7079 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007080 err = -NFS4ERR_WRONGSEC;
7081
7082 /* try to use integrity protection with machine cred */
7083 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7084 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7085
7086 /*
7087 * if unable to use integrity protection, or SECINFO with
7088 * integrity protection returns NFS4ERR_WRONGSEC (which is
7089 * disallowed by spec, but exists in deployed servers) use
7090 * the current filesystem's rpc_client and the user cred.
7091 */
7092 if (err == -NFS4ERR_WRONGSEC)
7093 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7094
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007095 trace_nfs4_secinfo(dir, name, err);
7096 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007097 &exception);
7098 } while (exception.retry);
7099 return err;
7100}
7101
Andy Adamson557134a2009-04-01 09:21:53 -04007102#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007103/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007104 * Check the exchange flags returned by the server for invalid flags, having
7105 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7106 * DS flags set.
7107 */
7108static int nfs4_check_cl_exchange_flags(u32 flags)
7109{
7110 if (flags & ~EXCHGID4_FLAG_MASK_R)
7111 goto out_inval;
7112 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7113 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7114 goto out_inval;
7115 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7116 goto out_inval;
7117 return NFS_OK;
7118out_inval:
7119 return -NFS4ERR_INVAL;
7120}
7121
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007122static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007123nfs41_same_server_scope(struct nfs41_server_scope *a,
7124 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007125{
7126 if (a->server_scope_sz == b->server_scope_sz &&
7127 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7128 return true;
7129
7130 return false;
7131}
7132
Andy Adamson02a95de2016-02-05 16:08:37 -05007133static void
7134nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7135{
7136}
7137
7138static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7139 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7140};
7141
Andy Adamson357f54d2010-12-14 10:11:57 -05007142/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007143 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007144 *
7145 * The 4.1 client currently uses the same TCP connection for the
7146 * fore and backchannel.
7147 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007148static
7149int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7150 struct rpc_xprt *xprt,
7151 struct nfs_client *clp,
7152 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007153{
7154 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007155 struct nfs41_bind_conn_to_session_args args = {
7156 .client = clp,
7157 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7158 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007159 struct nfs41_bind_conn_to_session_res res;
7160 struct rpc_message msg = {
7161 .rpc_proc =
7162 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007163 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007164 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007165 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007166 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007167 struct rpc_task_setup task_setup_data = {
7168 .rpc_client = clnt,
7169 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007170 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007171 .rpc_message = &msg,
7172 .flags = RPC_TASK_TIMEOUT,
7173 };
7174 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007175
7176 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177
Trond Myklebust71a097c2015-02-18 09:27:18 -08007178 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7179 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7180 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007181
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007182 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7183 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7184 args.dir = NFS4_CDFC4_FORE;
7185
7186 task = rpc_run_task(&task_setup_data);
7187 if (!IS_ERR(task)) {
7188 status = task->tk_status;
7189 rpc_put_task(task);
7190 } else
7191 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007192 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007193 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007194 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007195 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7196 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7197 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007198 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007199 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007200 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007201 dprintk("NFS: %s: Unexpected direction from server\n",
7202 __func__);
7203 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007204 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007205 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007206 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007207 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7208 __func__);
7209 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007210 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007211 }
7212 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007213out:
7214 dprintk("<-- %s status= %d\n", __func__, status);
7215 return status;
7216}
7217
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007218struct rpc_bind_conn_calldata {
7219 struct nfs_client *clp;
7220 struct rpc_cred *cred;
7221};
7222
7223static int
7224nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7225 struct rpc_xprt *xprt,
7226 void *calldata)
7227{
7228 struct rpc_bind_conn_calldata *p = calldata;
7229
7230 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7231}
7232
7233int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7234{
7235 struct rpc_bind_conn_calldata data = {
7236 .clp = clp,
7237 .cred = cred,
7238 };
7239 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7240 nfs4_proc_bind_conn_to_session_callback, &data);
7241}
7242
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007243/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007244 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7245 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007246 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007247static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7248 .how = SP4_MACH_CRED,
7249 .enforce.u.words = {
7250 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7251 1 << (OP_EXCHANGE_ID - 32) |
7252 1 << (OP_CREATE_SESSION - 32) |
7253 1 << (OP_DESTROY_SESSION - 32) |
7254 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007255 },
7256 .allow.u.words = {
7257 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007258 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007259 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007260 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007261 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007262 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007263 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007264 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007265 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007266 1 << (OP_FREE_STATEID - 32) |
7267 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007268 }
7269};
7270
7271/*
7272 * Select the state protection mode for client `clp' given the server results
7273 * from exchange_id in `sp'.
7274 *
7275 * Returns 0 on success, negative errno otherwise.
7276 */
7277static int nfs4_sp4_select_mode(struct nfs_client *clp,
7278 struct nfs41_state_protection *sp)
7279{
7280 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7281 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7282 1 << (OP_EXCHANGE_ID - 32) |
7283 1 << (OP_CREATE_SESSION - 32) |
7284 1 << (OP_DESTROY_SESSION - 32) |
7285 1 << (OP_DESTROY_CLIENTID - 32)
7286 };
7287 unsigned int i;
7288
7289 if (sp->how == SP4_MACH_CRED) {
7290 /* Print state protect result */
7291 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7292 for (i = 0; i <= LAST_NFS4_OP; i++) {
7293 if (test_bit(i, sp->enforce.u.longs))
7294 dfprintk(MOUNT, " enforce op %d\n", i);
7295 if (test_bit(i, sp->allow.u.longs))
7296 dfprintk(MOUNT, " allow op %d\n", i);
7297 }
7298
7299 /* make sure nothing is on enforce list that isn't supported */
7300 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7301 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7302 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7303 return -EINVAL;
7304 }
7305 }
7306
7307 /*
7308 * Minimal mode - state operations are allowed to use machine
7309 * credential. Note this already happens by default, so the
7310 * client doesn't have to do anything more than the negotiation.
7311 *
7312 * NOTE: we don't care if EXCHANGE_ID is in the list -
7313 * we're already using the machine cred for exchange_id
7314 * and will never use a different cred.
7315 */
7316 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7317 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7318 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7319 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7320 dfprintk(MOUNT, "sp4_mach_cred:\n");
7321 dfprintk(MOUNT, " minimal mode enabled\n");
7322 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7323 } else {
7324 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7325 return -EINVAL;
7326 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007327
7328 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007329 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7330 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007331 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7332 dfprintk(MOUNT, " cleanup mode enabled\n");
7333 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7334 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007335
Andrew Elble99ade3c2015-12-02 09:39:51 -05007336 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7337 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7338 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7339 &clp->cl_sp4_flags);
7340 }
7341
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007342 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7343 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7344 dfprintk(MOUNT, " secinfo mode enabled\n");
7345 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7346 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007347
7348 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7349 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7350 dfprintk(MOUNT, " stateid mode enabled\n");
7351 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7352 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007353
7354 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7355 dfprintk(MOUNT, " write mode enabled\n");
7356 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7357 }
7358
7359 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7360 dfprintk(MOUNT, " commit mode enabled\n");
7361 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7362 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007363 }
7364
7365 return 0;
7366}
7367
Andy Adamson8d89bd72016-09-09 09:22:18 -04007368struct nfs41_exchange_id_data {
7369 struct nfs41_exchange_id_res res;
7370 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007371 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007372 int rpc_status;
7373};
7374
7375static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007376{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007377 struct nfs41_exchange_id_data *cdata =
7378 (struct nfs41_exchange_id_data *)data;
7379 struct nfs_client *clp = cdata->args.client;
7380 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007381
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007382 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007383
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007384 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007385 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007386
7387 if (cdata->xprt && status == 0) {
7388 status = nfs4_detect_session_trunking(clp, &cdata->res,
7389 cdata->xprt);
7390 goto out;
7391 }
7392
Andy Adamson8d89bd72016-09-09 09:22:18 -04007393 if (status == 0)
7394 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007395
Chuck Lever177313f2012-05-21 22:44:58 -04007396 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007397 clp->cl_clientid = cdata->res.clientid;
7398 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007399 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007400 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007401 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007402 &clp->cl_session->session_state);
7403 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007404 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007405
Chuck Leveracdeb692012-05-21 22:46:16 -04007406 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007407 clp->cl_serverowner = cdata->res.server_owner;
7408 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007409
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007410 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007411 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412 clp->cl_implid = cdata->res.impl_id;
7413 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007414
Chuck Lever177313f2012-05-21 22:44:58 -04007415 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007416 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007417 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007418 dprintk("%s: server_scope mismatch detected\n",
7419 __func__);
7420 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007421 kfree(clp->cl_serverscope);
7422 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007423 }
7424
Chuck Lever177313f2012-05-21 22:44:58 -04007425 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007426 clp->cl_serverscope = cdata->res.server_scope;
7427 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007428 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007429 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007430 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007431 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007432 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007433out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007434 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007435 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007436}
7437
7438static void nfs4_exchange_id_release(void *data)
7439{
7440 struct nfs41_exchange_id_data *cdata =
7441 (struct nfs41_exchange_id_data *)data;
7442
Andy Adamsonad0849a2016-09-09 09:22:28 -04007443 if (cdata->xprt) {
7444 xprt_put(cdata->xprt);
7445 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7446 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007447 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007448 kfree(cdata->res.impl_id);
7449 kfree(cdata->res.server_scope);
7450 kfree(cdata->res.server_owner);
7451 kfree(cdata);
7452}
7453
7454static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7455 .rpc_call_done = nfs4_exchange_id_done,
7456 .rpc_release = nfs4_exchange_id_release,
7457};
7458
Benny Halevy99fe60d2009-04-01 09:22:29 -04007459/*
7460 * _nfs4_proc_exchange_id()
7461 *
7462 * Wrapper for EXCHANGE_ID operation.
7463 */
7464static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007465 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007466{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007467 struct rpc_message msg = {
7468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007469 .rpc_cred = cred,
7470 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 struct rpc_task_setup task_setup_data = {
7472 .rpc_client = clp->cl_rpcclient,
7473 .callback_ops = &nfs4_exchange_id_call_ops,
7474 .rpc_message = &msg,
7475 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7476 };
7477 struct nfs41_exchange_id_data *calldata;
7478 struct rpc_task *task;
7479 int status = -EIO;
7480
7481 if (!atomic_inc_not_zero(&clp->cl_count))
7482 goto out;
7483
7484 status = -ENOMEM;
7485 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7486 if (!calldata)
7487 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007488
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007489 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007490
7491 status = nfs4_init_uniform_client_string(clp);
7492 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007493 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007494
7495 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7496 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7497 clp->cl_owner_id);
7498
Andy Adamson8d89bd72016-09-09 09:22:18 -04007499 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7500 GFP_NOFS);
7501 status = -ENOMEM;
7502 if (unlikely(calldata->res.server_owner == NULL))
7503 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007504
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007506 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007507 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007508 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007509
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7511 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007512 goto out_server_scope;
7513
7514 switch (sp4_how) {
7515 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007516 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007517 break;
7518
7519 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007521 break;
7522
7523 default:
7524 /* unsupported! */
7525 WARN_ON_ONCE(1);
7526 status = -EINVAL;
7527 goto out_impl_id;
7528 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007529 if (xprt) {
7530 calldata->xprt = xprt;
7531 task_setup_data.rpc_xprt = xprt;
7532 task_setup_data.flags =
7533 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007534 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7535 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007536 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007537 calldata->args.client = clp;
7538#ifdef CONFIG_NFS_V4_1_MIGRATION
7539 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7540 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7541 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7542#else
7543 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7544 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7545#endif
7546 msg.rpc_argp = &calldata->args;
7547 msg.rpc_resp = &calldata->res;
7548 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007549
Andy Adamson8d89bd72016-09-09 09:22:18 -04007550 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007551 if (IS_ERR(task))
7552 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007553
Andy Adamsonad0849a2016-09-09 09:22:28 -04007554 if (!xprt) {
7555 status = rpc_wait_for_completion_task(task);
7556 if (!status)
7557 status = calldata->rpc_status;
7558 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007559 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007560
Andy Adamson8d89bd72016-09-09 09:22:18 -04007561 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007562out:
Chuck Lever177313f2012-05-21 22:44:58 -04007563 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007564 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007565 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007566 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007567 clp->cl_implid->date.seconds,
7568 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007569 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007570 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007571
7572out_impl_id:
7573 kfree(calldata->res.impl_id);
7574out_server_scope:
7575 kfree(calldata->res.server_scope);
7576out_server_owner:
7577 kfree(calldata->res.server_owner);
7578out_calldata:
7579 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007580 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007581 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007582}
7583
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007584/*
7585 * nfs4_proc_exchange_id()
7586 *
7587 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7588 *
7589 * Since the clientid has expired, all compounds using sessions
7590 * associated with the stale clientid will be returning
7591 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7592 * be in some phase of session reset.
7593 *
7594 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7595 */
7596int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7597{
7598 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7599 int status;
7600
7601 /* try SP4_MACH_CRED if krb5i/p */
7602 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7603 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007604 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007605 if (!status)
7606 return 0;
7607 }
7608
7609 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007610 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007611}
7612
Andy Adamson04fa2c62016-09-09 09:22:29 -04007613/**
7614 * nfs4_test_session_trunk
7615 *
7616 * This is an add_xprt_test() test function called from
7617 * rpc_clnt_setup_test_and_add_xprt.
7618 *
7619 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7620 * and is dereferrenced in nfs4_exchange_id_release
7621 *
7622 * Upon success, add the new transport to the rpc_clnt
7623 *
7624 * @clnt: struct rpc_clnt to get new transport
7625 * @xprt: the rpc_xprt to test
7626 * @data: call data for _nfs4_proc_exchange_id.
7627 */
7628int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7629 void *data)
7630{
7631 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7632 u32 sp4_how;
7633
7634 dprintk("--> %s try %s\n", __func__,
7635 xprt->address_strings[RPC_DISPLAY_ADDR]);
7636
7637 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7638
7639 /* Test connection for session trunking. Async exchange_id call */
7640 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7641}
7642EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7643
Trond Myklebust66245532012-05-25 17:18:09 -04007644static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7645 struct rpc_cred *cred)
7646{
7647 struct rpc_message msg = {
7648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7649 .rpc_argp = clp,
7650 .rpc_cred = cred,
7651 };
7652 int status;
7653
7654 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007655 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007656 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007657 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007658 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7659 return status;
7660}
7661
7662static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7663 struct rpc_cred *cred)
7664{
7665 unsigned int loop;
7666 int ret;
7667
7668 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7669 ret = _nfs4_proc_destroy_clientid(clp, cred);
7670 switch (ret) {
7671 case -NFS4ERR_DELAY:
7672 case -NFS4ERR_CLIENTID_BUSY:
7673 ssleep(1);
7674 break;
7675 default:
7676 return ret;
7677 }
7678 }
7679 return 0;
7680}
7681
7682int nfs4_destroy_clientid(struct nfs_client *clp)
7683{
7684 struct rpc_cred *cred;
7685 int ret = 0;
7686
7687 if (clp->cl_mvops->minor_version < 1)
7688 goto out;
7689 if (clp->cl_exchange_flags == 0)
7690 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007691 if (clp->cl_preserve_clid)
7692 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007693 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007694 ret = nfs4_proc_destroy_clientid(clp, cred);
7695 if (cred)
7696 put_rpccred(cred);
7697 switch (ret) {
7698 case 0:
7699 case -NFS4ERR_STALE_CLIENTID:
7700 clp->cl_exchange_flags = 0;
7701 }
7702out:
7703 return ret;
7704}
7705
Andy Adamson2050f0c2009-04-01 09:22:30 -04007706struct nfs4_get_lease_time_data {
7707 struct nfs4_get_lease_time_args *args;
7708 struct nfs4_get_lease_time_res *res;
7709 struct nfs_client *clp;
7710};
7711
7712static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7713 void *calldata)
7714{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007715 struct nfs4_get_lease_time_data *data =
7716 (struct nfs4_get_lease_time_data *)calldata;
7717
7718 dprintk("--> %s\n", __func__);
7719 /* just setup sequence, do not trigger session recovery
7720 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007721 nfs41_setup_sequence(data->clp->cl_session,
7722 &data->args->la_seq_args,
7723 &data->res->lr_seq_res,
7724 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007725 dprintk("<-- %s\n", __func__);
7726}
7727
7728/*
7729 * Called from nfs4_state_manager thread for session setup, so don't recover
7730 * from sequence operation or clientid errors.
7731 */
7732static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7733{
7734 struct nfs4_get_lease_time_data *data =
7735 (struct nfs4_get_lease_time_data *)calldata;
7736
7737 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007738 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7739 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007740 switch (task->tk_status) {
7741 case -NFS4ERR_DELAY:
7742 case -NFS4ERR_GRACE:
7743 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7744 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7745 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007746 /* fall through */
7747 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007748 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007749 return;
7750 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007751 dprintk("<-- %s\n", __func__);
7752}
7753
Trond Myklebust17280172012-03-11 13:11:00 -04007754static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007755 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7756 .rpc_call_done = nfs4_get_lease_time_done,
7757};
7758
7759int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7760{
7761 struct rpc_task *task;
7762 struct nfs4_get_lease_time_args args;
7763 struct nfs4_get_lease_time_res res = {
7764 .lr_fsinfo = fsinfo,
7765 };
7766 struct nfs4_get_lease_time_data data = {
7767 .args = &args,
7768 .res = &res,
7769 .clp = clp,
7770 };
7771 struct rpc_message msg = {
7772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7773 .rpc_argp = &args,
7774 .rpc_resp = &res,
7775 };
7776 struct rpc_task_setup task_setup = {
7777 .rpc_client = clp->cl_rpcclient,
7778 .rpc_message = &msg,
7779 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007780 .callback_data = &data,
7781 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007782 };
7783 int status;
7784
Chuck Levera9c92d62013-08-09 12:48:18 -04007785 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007786 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007787 dprintk("--> %s\n", __func__);
7788 task = rpc_run_task(&task_setup);
7789
7790 if (IS_ERR(task))
7791 status = PTR_ERR(task);
7792 else {
7793 status = task->tk_status;
7794 rpc_put_task(task);
7795 }
7796 dprintk("<-- %s return %d\n", __func__, status);
7797
7798 return status;
7799}
7800
Andy Adamsonfc931582009-04-01 09:22:31 -04007801/*
7802 * Initialize the values to be used by the client in CREATE_SESSION
7803 * If nfs4_init_session set the fore channel request and response sizes,
7804 * use them.
7805 *
7806 * Set the back channel max_resp_sz_cached to zero to force the client to
7807 * always set csa_cachethis to FALSE because the current implementation
7808 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7809 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007810static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7811 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007812{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007813 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007814 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007815
Andy Adamson18aad3d2013-06-26 12:21:49 -04007816 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7817 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7818
Andy Adamsonfc931582009-04-01 09:22:31 -04007819 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007820 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7821 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007822 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007823 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007824
7825 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007826 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007827 __func__,
7828 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007829 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007830
7831 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007832 args->bc_attrs.max_rqst_sz = max_bc_payload;
7833 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007834 args->bc_attrs.max_resp_sz_cached = 0;
7835 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007836 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007837
7838 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7839 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7840 __func__,
7841 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7842 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7843 args->bc_attrs.max_reqs);
7844}
7845
Trond Myklebust79969dd2015-02-18 11:30:18 -08007846static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7847 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007848{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007849 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007850 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007851
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007852 if (rcvd->max_resp_sz > sent->max_resp_sz)
7853 return -EINVAL;
7854 /*
7855 * Our requested max_ops is the minimum we need; we're not
7856 * prepared to break up compounds into smaller pieces than that.
7857 * So, no point even trying to continue if the server won't
7858 * cooperate:
7859 */
7860 if (rcvd->max_ops < sent->max_ops)
7861 return -EINVAL;
7862 if (rcvd->max_reqs == 0)
7863 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007864 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7865 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007866 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007867}
7868
Trond Myklebust79969dd2015-02-18 11:30:18 -08007869static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7870 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007871{
7872 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007873 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007874
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007875 if (!(res->flags & SESSION4_BACK_CHAN))
7876 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007877 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7878 return -EINVAL;
7879 if (rcvd->max_resp_sz < sent->max_resp_sz)
7880 return -EINVAL;
7881 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7882 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007883 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007884 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007885 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007886 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007887out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007888 return 0;
7889}
Andy Adamson8d353012009-04-01 09:22:32 -04007890
Andy Adamson8d353012009-04-01 09:22:32 -04007891static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007892 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007893{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007894 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007895
Trond Myklebust79969dd2015-02-18 11:30:18 -08007896 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007897 if (ret)
7898 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007899 return nfs4_verify_back_channel_attrs(args, res);
7900}
7901
7902static void nfs4_update_session(struct nfs4_session *session,
7903 struct nfs41_create_session_res *res)
7904{
7905 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007906 /* Mark client id and session as being confirmed */
7907 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7908 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007909 session->flags = res->flags;
7910 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007911 if (res->flags & SESSION4_BACK_CHAN)
7912 memcpy(&session->bc_attrs, &res->bc_attrs,
7913 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007914}
7915
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007916static int _nfs4_proc_create_session(struct nfs_client *clp,
7917 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007918{
7919 struct nfs4_session *session = clp->cl_session;
7920 struct nfs41_create_session_args args = {
7921 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007922 .clientid = clp->cl_clientid,
7923 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007924 .cb_program = NFS4_CALLBACK,
7925 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007926 struct nfs41_create_session_res res;
7927
Andy Adamsonfc931582009-04-01 09:22:31 -04007928 struct rpc_message msg = {
7929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7930 .rpc_argp = &args,
7931 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007932 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007933 };
7934 int status;
7935
Chuck Lever6b26cc82016-05-02 14:40:40 -04007936 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007937 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007938
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007939 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007940 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007941
Trond Myklebustb519d402016-09-11 14:50:01 -04007942 switch (status) {
7943 case -NFS4ERR_STALE_CLIENTID:
7944 case -NFS4ERR_DELAY:
7945 case -ETIMEDOUT:
7946 case -EACCES:
7947 case -EAGAIN:
7948 goto out;
7949 };
7950
7951 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007952 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007953 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007954 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007955 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007956 if (status)
7957 goto out;
7958 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007959 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007960out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007961 return status;
7962}
7963
7964/*
7965 * Issues a CREATE_SESSION operation to the server.
7966 * It is the responsibility of the caller to verify the session is
7967 * expired before calling this routine.
7968 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007969int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007970{
7971 int status;
7972 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007973 struct nfs4_session *session = clp->cl_session;
7974
7975 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7976
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007977 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007978 if (status)
7979 goto out;
7980
Andy Adamsonaacd5532011-11-09 13:58:21 -05007981 /* Init or reset the session slot tables */
7982 status = nfs4_setup_session_slot_tables(session);
7983 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007984 if (status)
7985 goto out;
7986
7987 ptr = (unsigned *)&session->sess_id.data[0];
7988 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7989 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007990out:
7991 dprintk("<-- %s\n", __func__);
7992 return status;
7993}
7994
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007995/*
7996 * Issue the over-the-wire RPC DESTROY_SESSION.
7997 * The caller must serialize access to this routine.
7998 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007999int nfs4_proc_destroy_session(struct nfs4_session *session,
8000 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008001{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008002 struct rpc_message msg = {
8003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8004 .rpc_argp = session,
8005 .rpc_cred = cred,
8006 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008007 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008008
8009 dprintk("--> nfs4_proc_destroy_session\n");
8010
8011 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008012 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8013 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008014
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008015 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008016 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008017
8018 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008019 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008020 "Session has been destroyed regardless...\n", status);
8021
8022 dprintk("<-- nfs4_proc_destroy_session\n");
8023 return status;
8024}
8025
Trond Myklebust7b38c362012-05-23 13:23:31 -04008026/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008027 * Renew the cl_session lease.
8028 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008029struct nfs4_sequence_data {
8030 struct nfs_client *clp;
8031 struct nfs4_sequence_args args;
8032 struct nfs4_sequence_res res;
8033};
8034
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008035static void nfs41_sequence_release(void *data)
8036{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008037 struct nfs4_sequence_data *calldata = data;
8038 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008039
Alexandros Batsakis71358402010-02-05 03:45:05 -08008040 if (atomic_read(&clp->cl_count) > 1)
8041 nfs4_schedule_state_renewal(clp);
8042 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008043 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008044}
8045
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008046static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8047{
8048 switch(task->tk_status) {
8049 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008050 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8051 return -EAGAIN;
8052 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008053 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008054 }
8055 return 0;
8056}
8057
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008058static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008059{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008060 struct nfs4_sequence_data *calldata = data;
8061 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008062
Trond Myklebust14516c32010-07-31 14:29:06 -04008063 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8064 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008065
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008066 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008067 if (task->tk_status < 0) {
8068 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008069 if (atomic_read(&clp->cl_count) == 1)
8070 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008071
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008072 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8073 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074 return;
8075 }
8076 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008078out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008079 dprintk("<-- %s\n", __func__);
8080}
8081
8082static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8083{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008084 struct nfs4_sequence_data *calldata = data;
8085 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086 struct nfs4_sequence_args *args;
8087 struct nfs4_sequence_res *res;
8088
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089 args = task->tk_msg.rpc_argp;
8090 res = task->tk_msg.rpc_resp;
8091
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008092 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093}
8094
8095static const struct rpc_call_ops nfs41_sequence_ops = {
8096 .rpc_call_done = nfs41_sequence_call_done,
8097 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008098 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008099};
8100
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8102 struct rpc_cred *cred,
8103 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008104{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008105 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008106 struct rpc_message msg = {
8107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8108 .rpc_cred = cred,
8109 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008110 struct rpc_task_setup task_setup_data = {
8111 .rpc_client = clp->cl_rpcclient,
8112 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008113 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008114 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008115 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008116
Alexandros Batsakis71358402010-02-05 03:45:05 -08008117 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008118 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008119 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008120 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008121 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008122 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008123 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008124 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008125 if (is_privileged)
8126 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008127 msg.rpc_argp = &calldata->args;
8128 msg.rpc_resp = &calldata->res;
8129 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008130 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008131
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008132 return rpc_run_task(&task_setup_data);
8133}
8134
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008135static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008136{
8137 struct rpc_task *task;
8138 int ret = 0;
8139
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008140 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008141 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008142 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008143 if (IS_ERR(task))
8144 ret = PTR_ERR(task);
8145 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008146 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008147 dprintk("<-- %s status=%d\n", __func__, ret);
8148 return ret;
8149}
8150
8151static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8152{
8153 struct rpc_task *task;
8154 int ret;
8155
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008156 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008157 if (IS_ERR(task)) {
8158 ret = PTR_ERR(task);
8159 goto out;
8160 }
8161 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008162 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008163 ret = task->tk_status;
8164 rpc_put_task(task);
8165out:
8166 dprintk("<-- %s status=%d\n", __func__, ret);
8167 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008168}
8169
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008170struct nfs4_reclaim_complete_data {
8171 struct nfs_client *clp;
8172 struct nfs41_reclaim_complete_args arg;
8173 struct nfs41_reclaim_complete_res res;
8174};
8175
8176static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8177{
8178 struct nfs4_reclaim_complete_data *calldata = data;
8179
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008180 nfs41_setup_sequence(calldata->clp->cl_session,
8181 &calldata->arg.seq_args,
8182 &calldata->res.seq_res,
8183 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008184}
8185
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008186static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8187{
8188 switch(task->tk_status) {
8189 case 0:
8190 case -NFS4ERR_COMPLETE_ALREADY:
8191 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8192 break;
8193 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008194 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008195 /* fall through */
8196 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008197 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008198 case -NFS4ERR_BADSESSION:
8199 case -NFS4ERR_DEADSESSION:
8200 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8201 nfs4_schedule_session_recovery(clp->cl_session,
8202 task->tk_status);
8203 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008204 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008205 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008206 }
8207 return 0;
8208}
8209
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008210static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8211{
8212 struct nfs4_reclaim_complete_data *calldata = data;
8213 struct nfs_client *clp = calldata->clp;
8214 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8215
8216 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008217 if (!nfs41_sequence_done(task, res))
8218 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008219
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008220 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008221 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8222 rpc_restart_call_prepare(task);
8223 return;
8224 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008225 dprintk("<-- %s\n", __func__);
8226}
8227
8228static void nfs4_free_reclaim_complete_data(void *data)
8229{
8230 struct nfs4_reclaim_complete_data *calldata = data;
8231
8232 kfree(calldata);
8233}
8234
8235static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8236 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8237 .rpc_call_done = nfs4_reclaim_complete_done,
8238 .rpc_release = nfs4_free_reclaim_complete_data,
8239};
8240
8241/*
8242 * Issue a global reclaim complete.
8243 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008244static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8245 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008246{
8247 struct nfs4_reclaim_complete_data *calldata;
8248 struct rpc_task *task;
8249 struct rpc_message msg = {
8250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008251 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252 };
8253 struct rpc_task_setup task_setup_data = {
8254 .rpc_client = clp->cl_rpcclient,
8255 .rpc_message = &msg,
8256 .callback_ops = &nfs4_reclaim_complete_call_ops,
8257 .flags = RPC_TASK_ASYNC,
8258 };
8259 int status = -ENOMEM;
8260
8261 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008262 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008263 if (calldata == NULL)
8264 goto out;
8265 calldata->clp = clp;
8266 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008267
Chuck Levera9c92d62013-08-09 12:48:18 -04008268 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008269 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008270 msg.rpc_argp = &calldata->arg;
8271 msg.rpc_resp = &calldata->res;
8272 task_setup_data.callback_data = calldata;
8273 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008274 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008275 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008276 goto out;
8277 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008278 status = nfs4_wait_for_completion_rpc_task(task);
8279 if (status == 0)
8280 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008281 rpc_put_task(task);
8282out:
8283 dprintk("<-- %s status=%d\n", __func__, status);
8284 return status;
8285}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008286
8287static void
8288nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8289{
8290 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008291 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008292 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008293
8294 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008295 nfs41_setup_sequence(session, &lgp->args.seq_args,
8296 &lgp->res.seq_res, task);
8297 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008298}
8299
8300static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8301{
8302 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008303
8304 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008305 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008306 dprintk("<-- %s\n", __func__);
8307}
8308
8309static int
8310nfs4_layoutget_handle_exception(struct rpc_task *task,
8311 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8312{
Trond Myklebustee314c22012-10-01 17:25:48 -07008313 struct inode *inode = lgp->args.inode;
8314 struct nfs_server *server = NFS_SERVER(inode);
8315 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008316 int nfs4err = task->tk_status;
8317 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008318 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008319
Boaz Harroshed7e5422014-01-22 20:34:54 +02008320 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008321
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008322 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008323 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008324 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008325
8326 /*
8327 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8328 * on the file. set tk_status to -ENODATA to tell upper layer to
8329 * retry go inband.
8330 */
8331 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008332 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008333 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008334 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008335 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8336 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8337 */
8338 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008339 status = -EOVERFLOW;
8340 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008341 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008342 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008343 * (or clients) writing to the same RAID stripe except when
8344 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008345 *
8346 * Treat it like we would RECALLCONFLICT -- we retry for a little
8347 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008348 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008349 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008350 if (lgp->args.minlength == 0) {
8351 status = -EOVERFLOW;
8352 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008353 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008354 status = -EBUSY;
8355 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008356 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008357 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008358 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008359 case -NFS4ERR_DELEG_REVOKED:
8360 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008361 case -NFS4ERR_EXPIRED:
8362 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008363 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008364 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008365 lo = NFS_I(inode)->layout;
8366 /* If the open stateid was bad, then recover it. */
8367 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8368 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008369 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008370 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008371 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008372 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008373 break;
8374 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008375
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008376 /*
8377 * Mark the bad layout state as invalid, then retry
8378 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008379 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008380 spin_unlock(&inode->i_lock);
8381 pnfs_free_lseg_list(&head);
8382 status = -EAGAIN;
8383 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008384 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008385
Trond Myklebust0a702352017-01-23 22:44:12 -05008386 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008387 err = nfs4_handle_exception(server, nfs4err, exception);
8388 if (!status) {
8389 if (exception->retry)
8390 status = -EAGAIN;
8391 else
8392 status = err;
8393 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008394out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008395 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008396 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008397}
8398
Idan Kedar85541162012-08-02 11:47:10 +03008399static size_t max_response_pages(struct nfs_server *server)
8400{
8401 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8402 return nfs_page_array_len(0, max_resp_sz);
8403}
8404
8405static void nfs4_free_pages(struct page **pages, size_t size)
8406{
8407 int i;
8408
8409 if (!pages)
8410 return;
8411
8412 for (i = 0; i < size; i++) {
8413 if (!pages[i])
8414 break;
8415 __free_page(pages[i]);
8416 }
8417 kfree(pages);
8418}
8419
8420static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8421{
8422 struct page **pages;
8423 int i;
8424
8425 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8426 if (!pages) {
8427 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8428 return NULL;
8429 }
8430
8431 for (i = 0; i < size; i++) {
8432 pages[i] = alloc_page(gfp_flags);
8433 if (!pages[i]) {
8434 dprintk("%s: failed to allocate page\n", __func__);
8435 nfs4_free_pages(pages, size);
8436 return NULL;
8437 }
8438 }
8439
8440 return pages;
8441}
8442
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008443static void nfs4_layoutget_release(void *calldata)
8444{
8445 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008446 struct inode *inode = lgp->args.inode;
8447 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008448 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008449
8450 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008451 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008452 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008453 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008454 put_nfs_open_context(lgp->args.ctx);
8455 kfree(calldata);
8456 dprintk("<-- %s\n", __func__);
8457}
8458
8459static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8460 .rpc_call_prepare = nfs4_layoutget_prepare,
8461 .rpc_call_done = nfs4_layoutget_done,
8462 .rpc_release = nfs4_layoutget_release,
8463};
8464
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008465struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008466nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008467{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008468 struct inode *inode = lgp->args.inode;
8469 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008470 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008471 struct rpc_task *task;
8472 struct rpc_message msg = {
8473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8474 .rpc_argp = &lgp->args,
8475 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008476 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477 };
8478 struct rpc_task_setup task_setup_data = {
8479 .rpc_client = server->client,
8480 .rpc_message = &msg,
8481 .callback_ops = &nfs4_layoutget_call_ops,
8482 .callback_data = lgp,
8483 .flags = RPC_TASK_ASYNC,
8484 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008485 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008486 struct nfs4_exception exception = {
8487 .inode = inode,
8488 .timeout = *timeout,
8489 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 int status = 0;
8491
8492 dprintk("--> %s\n", __func__);
8493
Peng Tao4bd5a982014-11-17 11:05:17 +08008494 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8495 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8496
Idan Kedar85541162012-08-02 11:47:10 +03008497 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8498 if (!lgp->args.layout.pages) {
8499 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008500 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008501 }
8502 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8503
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008504 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008505 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008506 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008507
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008508 task = rpc_run_task(&task_setup_data);
8509 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008510 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008512 if (status == 0) {
8513 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8514 *timeout = exception.timeout;
8515 }
8516
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008517 trace_nfs4_layoutget(lgp->args.ctx,
8518 &lgp->args.range,
8519 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008520 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008521 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008522
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008523 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8524 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008525 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008526 rpc_put_task(task);
8527 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008528 if (status)
8529 return ERR_PTR(status);
8530 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008531}
8532
Benny Halevycbe82602011-05-22 19:52:37 +03008533static void
8534nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8535{
8536 struct nfs4_layoutreturn *lrp = calldata;
8537
8538 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008539 nfs41_setup_sequence(lrp->clp->cl_session,
8540 &lrp->args.seq_args,
8541 &lrp->res.seq_res,
8542 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008543}
8544
8545static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8546{
8547 struct nfs4_layoutreturn *lrp = calldata;
8548 struct nfs_server *server;
8549
8550 dprintk("--> %s\n", __func__);
8551
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008552 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008553 return;
8554
8555 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008556 switch (task->tk_status) {
8557 default:
8558 task->tk_status = 0;
8559 case 0:
8560 break;
8561 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008562 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008563 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008564 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008565 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008566 return;
8567 }
Benny Halevycbe82602011-05-22 19:52:37 +03008568 dprintk("<-- %s\n", __func__);
8569}
8570
8571static void nfs4_layoutreturn_release(void *calldata)
8572{
8573 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008574 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008575 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008576
8577 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008578 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008579 if (lrp->res.lrs_present) {
8580 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8581 &lrp->args.range,
8582 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008583 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008584 } else
8585 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008586 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008587 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008588 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008589 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008590 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008591 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008592 kfree(calldata);
8593 dprintk("<-- %s\n", __func__);
8594}
8595
8596static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8597 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8598 .rpc_call_done = nfs4_layoutreturn_done,
8599 .rpc_release = nfs4_layoutreturn_release,
8600};
8601
Peng Tao6c166052014-11-17 09:30:40 +08008602int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008603{
8604 struct rpc_task *task;
8605 struct rpc_message msg = {
8606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8607 .rpc_argp = &lrp->args,
8608 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008609 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008610 };
8611 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008612 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008613 .rpc_message = &msg,
8614 .callback_ops = &nfs4_layoutreturn_call_ops,
8615 .callback_data = lrp,
8616 };
Peng Tao6c166052014-11-17 09:30:40 +08008617 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008618
Andrew Elble99ade3c2015-12-02 09:39:51 -05008619 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8620 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8621 &task_setup_data.rpc_client, &msg);
8622
Benny Halevycbe82602011-05-22 19:52:37 +03008623 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008624 if (!sync) {
8625 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8626 if (!lrp->inode) {
8627 nfs4_layoutreturn_release(lrp);
8628 return -EAGAIN;
8629 }
8630 task_setup_data.flags |= RPC_TASK_ASYNC;
8631 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008632 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008633 task = rpc_run_task(&task_setup_data);
8634 if (IS_ERR(task))
8635 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008636 if (sync)
8637 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008638 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008639 dprintk("<-- %s status=%d\n", __func__, status);
8640 rpc_put_task(task);
8641 return status;
8642}
8643
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008644static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008645_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8646 struct pnfs_device *pdev,
8647 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008648{
8649 struct nfs4_getdeviceinfo_args args = {
8650 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008651 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8652 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008653 };
8654 struct nfs4_getdeviceinfo_res res = {
8655 .pdev = pdev,
8656 };
8657 struct rpc_message msg = {
8658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8659 .rpc_argp = &args,
8660 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008661 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008662 };
8663 int status;
8664
8665 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008666 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008667 if (res.notification & ~args.notify_types)
8668 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008669 if (res.notification != args.notify_types)
8670 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008671
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008672 dprintk("<-- %s status=%d\n", __func__, status);
8673
8674 return status;
8675}
8676
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008677int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8678 struct pnfs_device *pdev,
8679 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008680{
8681 struct nfs4_exception exception = { };
8682 int err;
8683
8684 do {
8685 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008686 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008687 &exception);
8688 } while (exception.retry);
8689 return err;
8690}
8691EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8692
Andy Adamson863a3c62011-03-23 13:27:54 +00008693static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8694{
8695 struct nfs4_layoutcommit_data *data = calldata;
8696 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008697 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008698
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008699 nfs41_setup_sequence(session,
8700 &data->args.seq_args,
8701 &data->res.seq_res,
8702 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008703}
8704
8705static void
8706nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8707{
8708 struct nfs4_layoutcommit_data *data = calldata;
8709 struct nfs_server *server = NFS_SERVER(data->args.inode);
8710
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008711 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008712 return;
8713
8714 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008715 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8716 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8717 case -NFS4ERR_BADLAYOUT: /* no layout */
8718 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008719 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008720 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008721 break;
8722 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008723 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008724 rpc_restart_call_prepare(task);
8725 return;
8726 }
8727 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008728}
8729
8730static void nfs4_layoutcommit_release(void *calldata)
8731{
8732 struct nfs4_layoutcommit_data *data = calldata;
8733
Andy Adamsondb29c082011-07-30 20:52:38 -04008734 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008735 nfs_post_op_update_inode_force_wcc(data->args.inode,
8736 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008737 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008738 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008739 kfree(data);
8740}
8741
8742static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8743 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8744 .rpc_call_done = nfs4_layoutcommit_done,
8745 .rpc_release = nfs4_layoutcommit_release,
8746};
8747
8748int
Andy Adamsonef311532011-03-12 02:58:10 -05008749nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008750{
8751 struct rpc_message msg = {
8752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8753 .rpc_argp = &data->args,
8754 .rpc_resp = &data->res,
8755 .rpc_cred = data->cred,
8756 };
8757 struct rpc_task_setup task_setup_data = {
8758 .task = &data->task,
8759 .rpc_client = NFS_CLIENT(data->args.inode),
8760 .rpc_message = &msg,
8761 .callback_ops = &nfs4_layoutcommit_ops,
8762 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008763 };
8764 struct rpc_task *task;
8765 int status = 0;
8766
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008767 dprintk("NFS: initiating layoutcommit call. sync %d "
8768 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008769 data->args.lastbytewritten,
8770 data->args.inode->i_ino);
8771
Trond Myklebust472e2592015-02-05 16:50:30 -05008772 if (!sync) {
8773 data->inode = nfs_igrab_and_active(data->args.inode);
8774 if (data->inode == NULL) {
8775 nfs4_layoutcommit_release(data);
8776 return -EAGAIN;
8777 }
8778 task_setup_data.flags = RPC_TASK_ASYNC;
8779 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008780 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008781 task = rpc_run_task(&task_setup_data);
8782 if (IS_ERR(task))
8783 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008784 if (sync)
8785 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008786 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008787 dprintk("%s: status %d\n", __func__, status);
8788 rpc_put_task(task);
8789 return status;
8790}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008791
Andy Adamson97431202013-08-08 10:57:56 -04008792/**
8793 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8794 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8795 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008796static int
8797_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008798 struct nfs_fsinfo *info,
8799 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008800{
8801 struct nfs41_secinfo_no_name_args args = {
8802 .style = SECINFO_STYLE_CURRENT_FH,
8803 };
8804 struct nfs4_secinfo_res res = {
8805 .flavors = flavors,
8806 };
8807 struct rpc_message msg = {
8808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8809 .rpc_argp = &args,
8810 .rpc_resp = &res,
8811 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008812 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008813 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008814 int status;
8815
8816 if (use_integrity) {
8817 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008818 cred = nfs4_get_clid_cred(server->nfs_client);
8819 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008820 }
8821
8822 dprintk("--> %s\n", __func__);
8823 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8824 &res.seq_res, 0);
8825 dprintk("<-- %s status=%d\n", __func__, status);
8826
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008827 if (cred)
8828 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008829
8830 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008831}
8832
8833static int
8834nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8835 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8836{
8837 struct nfs4_exception exception = { };
8838 int err;
8839 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008840 /* first try using integrity protection */
8841 err = -NFS4ERR_WRONGSEC;
8842
8843 /* try to use integrity protection with machine cred */
8844 if (_nfs4_is_integrity_protected(server->nfs_client))
8845 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8846 flavors, true);
8847
8848 /*
8849 * if unable to use integrity protection, or SECINFO with
8850 * integrity protection returns NFS4ERR_WRONGSEC (which is
8851 * disallowed by spec, but exists in deployed servers) use
8852 * the current filesystem's rpc_client and the user cred.
8853 */
8854 if (err == -NFS4ERR_WRONGSEC)
8855 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8856 flavors, false);
8857
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008858 switch (err) {
8859 case 0:
8860 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008861 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008862 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008863 default:
8864 err = nfs4_handle_exception(server, err, &exception);
8865 }
8866 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008867out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008868 return err;
8869}
8870
8871static int
8872nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8873 struct nfs_fsinfo *info)
8874{
8875 int err;
8876 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008877 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008878 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008879 struct nfs4_secinfo4 *secinfo;
8880 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008881
8882 page = alloc_page(GFP_KERNEL);
8883 if (!page) {
8884 err = -ENOMEM;
8885 goto out;
8886 }
8887
8888 flavors = page_address(page);
8889 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8890
8891 /*
8892 * Fall back on "guess and check" method if
8893 * the server doesn't support SECINFO_NO_NAME
8894 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008895 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008896 err = nfs4_find_root_sec(server, fhandle, info);
8897 goto out_freepage;
8898 }
8899 if (err)
8900 goto out_freepage;
8901
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008902 for (i = 0; i < flavors->num_flavors; i++) {
8903 secinfo = &flavors->flavors[i];
8904
8905 switch (secinfo->flavor) {
8906 case RPC_AUTH_NULL:
8907 case RPC_AUTH_UNIX:
8908 case RPC_AUTH_GSS:
8909 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8910 &secinfo->flavor_info);
8911 break;
8912 default:
8913 flavor = RPC_AUTH_MAXFLAVOR;
8914 break;
8915 }
8916
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008917 if (!nfs_auth_info_match(&server->auth_info, flavor))
8918 flavor = RPC_AUTH_MAXFLAVOR;
8919
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008920 if (flavor != RPC_AUTH_MAXFLAVOR) {
8921 err = nfs4_lookup_root_sec(server, fhandle,
8922 info, flavor);
8923 if (!err)
8924 break;
8925 }
8926 }
8927
8928 if (flavor == RPC_AUTH_MAXFLAVOR)
8929 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008930
8931out_freepage:
8932 put_page(page);
8933 if (err == -EACCES)
8934 return -EPERM;
8935out:
8936 return err;
8937}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008938
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008939static int _nfs41_test_stateid(struct nfs_server *server,
8940 nfs4_stateid *stateid,
8941 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008942{
8943 int status;
8944 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008945 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008946 };
8947 struct nfs41_test_stateid_res res;
8948 struct rpc_message msg = {
8949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8950 .rpc_argp = &args,
8951 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008952 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008953 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008954 struct rpc_clnt *rpc_client = server->client;
8955
8956 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8957 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008958
Chuck Lever38527b12012-07-11 16:30:23 -04008959 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008960 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008961 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008962 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008963 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008964 if (status != NFS_OK) {
8965 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008966 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008967 }
8968 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008969 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008970}
8971
Trond Myklebust43912bb2016-09-22 13:38:56 -04008972static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8973 int err, struct nfs4_exception *exception)
8974{
8975 exception->retry = 0;
8976 switch(err) {
8977 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008978 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008979 nfs4_handle_exception(server, err, exception);
8980 break;
8981 case -NFS4ERR_BADSESSION:
8982 case -NFS4ERR_BADSLOT:
8983 case -NFS4ERR_BAD_HIGH_SLOT:
8984 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8985 case -NFS4ERR_DEADSESSION:
8986 nfs4_do_handle_exception(server, err, exception);
8987 }
8988}
8989
Chuck Lever38527b12012-07-11 16:30:23 -04008990/**
8991 * nfs41_test_stateid - perform a TEST_STATEID operation
8992 *
8993 * @server: server / transport on which to perform the operation
8994 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008995 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008996 *
8997 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8998 * Otherwise a negative NFS4ERR value is returned if the operation
8999 * failed or the state ID is not currently valid.
9000 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009001static int nfs41_test_stateid(struct nfs_server *server,
9002 nfs4_stateid *stateid,
9003 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009004{
9005 struct nfs4_exception exception = { };
9006 int err;
9007 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009008 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009009 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009010 } while (exception.retry);
9011 return err;
9012}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009013
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009014struct nfs_free_stateid_data {
9015 struct nfs_server *server;
9016 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009017 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009018};
9019
9020static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9021{
9022 struct nfs_free_stateid_data *data = calldata;
9023 nfs41_setup_sequence(nfs4_get_session(data->server),
9024 &data->args.seq_args,
9025 &data->res.seq_res,
9026 task);
9027}
9028
9029static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9030{
9031 struct nfs_free_stateid_data *data = calldata;
9032
9033 nfs41_sequence_done(task, &data->res.seq_res);
9034
9035 switch (task->tk_status) {
9036 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009037 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009038 rpc_restart_call_prepare(task);
9039 }
9040}
9041
9042static void nfs41_free_stateid_release(void *calldata)
9043{
9044 kfree(calldata);
9045}
9046
Trond Myklebust17f26b12013-08-21 15:48:42 -04009047static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009048 .rpc_call_prepare = nfs41_free_stateid_prepare,
9049 .rpc_call_done = nfs41_free_stateid_done,
9050 .rpc_release = nfs41_free_stateid_release,
9051};
9052
9053static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009054 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009055 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009056 bool privileged)
9057{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009058 struct rpc_message msg = {
9059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009060 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009061 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009062 struct rpc_task_setup task_setup = {
9063 .rpc_client = server->client,
9064 .rpc_message = &msg,
9065 .callback_ops = &nfs41_free_stateid_ops,
9066 .flags = RPC_TASK_ASYNC,
9067 };
9068 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009069
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009070 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9071 &task_setup.rpc_client, &msg);
9072
Chuck Lever38527b12012-07-11 16:30:23 -04009073 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009074 data = kmalloc(sizeof(*data), GFP_NOFS);
9075 if (!data)
9076 return ERR_PTR(-ENOMEM);
9077 data->server = server;
9078 nfs4_stateid_copy(&data->args.stateid, stateid);
9079
9080 task_setup.callback_data = data;
9081
9082 msg.rpc_argp = &data->args;
9083 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009084 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009085 if (privileged)
9086 nfs4_set_sequence_privileged(&data->args.seq_args);
9087
9088 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009089}
9090
Chuck Lever38527b12012-07-11 16:30:23 -04009091/**
9092 * nfs41_free_stateid - perform a FREE_STATEID operation
9093 *
9094 * @server: server / transport on which to perform the operation
9095 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009096 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009097 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009098 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009099 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009100 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009101static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009102 const nfs4_stateid *stateid,
9103 struct rpc_cred *cred,
9104 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009105{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009106 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009107
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009108 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009109 if (IS_ERR(task))
9110 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009111 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009112 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009113}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009114
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009115static void
9116nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009117{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009118 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009119
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009120 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009121 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009122}
9123
Trond Myklebust36281ca2012-03-04 18:13:56 -05009124static bool nfs41_match_stateid(const nfs4_stateid *s1,
9125 const nfs4_stateid *s2)
9126{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009127 if (s1->type != s2->type)
9128 return false;
9129
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009130 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009131 return false;
9132
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009133 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009134 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009135 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009136 return true;
9137
9138 return false;
9139}
9140
Andy Adamson557134a2009-04-01 09:21:53 -04009141#endif /* CONFIG_NFS_V4_1 */
9142
Trond Myklebust36281ca2012-03-04 18:13:56 -05009143static bool nfs4_match_stateid(const nfs4_stateid *s1,
9144 const nfs4_stateid *s2)
9145{
Trond Myklebustf597c532012-03-04 18:13:56 -05009146 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009147}
9148
9149
Trond Myklebust17280172012-03-11 13:11:00 -04009150static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009151 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009152 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009153 .recover_open = nfs4_open_reclaim,
9154 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009155 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009156 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009157};
9158
Andy Adamson591d71c2009-04-01 09:22:47 -04009159#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009160static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009161 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9162 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9163 .recover_open = nfs4_open_reclaim,
9164 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009165 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009166 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009167 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009168};
9169#endif /* CONFIG_NFS_V4_1 */
9170
Trond Myklebust17280172012-03-11 13:11:00 -04009171static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009172 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009173 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009174 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009175 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009176 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177};
9178
Andy Adamson591d71c2009-04-01 09:22:47 -04009179#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009180static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009181 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9182 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009183 .recover_open = nfs41_open_expired,
9184 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009185 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009186};
9187#endif /* CONFIG_NFS_V4_1 */
9188
Trond Myklebust17280172012-03-11 13:11:00 -04009189static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009190 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009191 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009192 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009193};
9194
9195#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009196static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009197 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009198 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009199 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009200};
9201#endif
9202
Chuck Leverec011fe2013-10-17 14:12:39 -04009203static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009204 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009205 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009206};
9207
9208#if defined(CONFIG_NFS_V4_1)
9209static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009210 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009211 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009212};
9213#endif /* CONFIG_NFS_V4_1 */
9214
Trond Myklebust97dc1352010-06-16 09:52:26 -04009215static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9216 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009217 .init_caps = NFS_CAP_READDIRPLUS
9218 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009219 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009220 .init_client = nfs40_init_client,
9221 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009222 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009223 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009224 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009225 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009226 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009227 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009228 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9229 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9230 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009231 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009232};
9233
9234#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009235static struct nfs_seqid *
9236nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9237{
9238 return NULL;
9239}
9240
Trond Myklebust97dc1352010-06-16 09:52:26 -04009241static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9242 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009243 .init_caps = NFS_CAP_READDIRPLUS
9244 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009245 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009246 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009247 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009248 .init_client = nfs41_init_client,
9249 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009250 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009251 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009252 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009253 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009254 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009255 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009256 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009257 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9258 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9259 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009260 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009261};
9262#endif
9263
Steve Dickson42c2c422013-05-22 12:50:38 -04009264#if defined(CONFIG_NFS_V4_2)
9265static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9266 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009267 .init_caps = NFS_CAP_READDIRPLUS
9268 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009269 | NFS_CAP_POSIX_LOCK
9270 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009271 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009272 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009273 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009274 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009275 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009276 | NFS_CAP_LAYOUTSTATS
9277 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009278 .init_client = nfs41_init_client,
9279 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009280 .match_stateid = nfs41_match_stateid,
9281 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009282 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009283 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009284 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009285 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009286 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009287 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9288 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9289 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009290 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009291};
9292#endif
9293
Trond Myklebust97dc1352010-06-16 09:52:26 -04009294const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9295 [0] = &nfs_v4_0_minor_ops,
9296#if defined(CONFIG_NFS_V4_1)
9297 [1] = &nfs_v4_1_minor_ops,
9298#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009299#if defined(CONFIG_NFS_V4_2)
9300 [2] = &nfs_v4_2_minor_ops,
9301#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009302};
9303
Trond Myklebust13997822016-07-24 17:10:52 -04009304static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009305{
9306 ssize_t error, error2;
9307
9308 error = generic_listxattr(dentry, list, size);
9309 if (error < 0)
9310 return error;
9311 if (list) {
9312 list += error;
9313 size -= error;
9314 }
9315
9316 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9317 if (error2 < 0)
9318 return error2;
9319 return error + error2;
9320}
9321
Trond Myklebust17f26b12013-08-21 15:48:42 -04009322static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009323 .create = nfs_create,
9324 .lookup = nfs_lookup,
9325 .atomic_open = nfs_atomic_open,
9326 .link = nfs_link,
9327 .unlink = nfs_unlink,
9328 .symlink = nfs_symlink,
9329 .mkdir = nfs_mkdir,
9330 .rmdir = nfs_rmdir,
9331 .mknod = nfs_mknod,
9332 .rename = nfs_rename,
9333 .permission = nfs_permission,
9334 .getattr = nfs_getattr,
9335 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009336 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009337};
9338
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009339static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009340 .permission = nfs_permission,
9341 .getattr = nfs_getattr,
9342 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009343 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009344};
9345
David Howells509de812006-08-22 20:06:11 -04009346const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 .version = 4, /* protocol version */
9348 .dentry_ops = &nfs4_dentry_operations,
9349 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009350 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009351 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009352 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009353 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009354 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355 .getattr = nfs4_proc_getattr,
9356 .setattr = nfs4_proc_setattr,
9357 .lookup = nfs4_proc_lookup,
9358 .access = nfs4_proc_access,
9359 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .create = nfs4_proc_create,
9361 .remove = nfs4_proc_remove,
9362 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009363 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009365 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009366 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009367 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368 .link = nfs4_proc_link,
9369 .symlink = nfs4_proc_symlink,
9370 .mkdir = nfs4_proc_mkdir,
9371 .rmdir = nfs4_proc_remove,
9372 .readdir = nfs4_proc_readdir,
9373 .mknod = nfs4_proc_mknod,
9374 .statfs = nfs4_proc_statfs,
9375 .fsinfo = nfs4_proc_fsinfo,
9376 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009377 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009379 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009381 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009383 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009384 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009385 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009386 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009388 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009389 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009390 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009391 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009392 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009393 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009394 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009395 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009396 .create_server = nfs4_create_server,
9397 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398};
9399
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009400static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009401 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009402 .list = nfs4_xattr_list_nfs4_acl,
9403 .get = nfs4_xattr_get_nfs4_acl,
9404 .set = nfs4_xattr_set_nfs4_acl,
9405};
9406
9407const struct xattr_handler *nfs4_xattr_handlers[] = {
9408 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009409#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9410 &nfs4_xattr_nfs4_label_handler,
9411#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009412 NULL
9413};
9414
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415/*
9416 * Local variables:
9417 * c-basic-offset: 8
9418 * End:
9419 */