blob: 6d0d94fc243d32f356ff344a7448e0f00639eee7 [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:
Olga Kornievskaia22a87112019-03-19 12:12:13 -04002751 if (!opendata->cancelled)
2752 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002753 return ret;
2754}
2755
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002756/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002757 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 */
Andy Adamson82be4172012-05-23 05:02:35 -04002759static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002760 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002761 int flags,
2762 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002763 struct nfs4_label *label,
2764 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct nfs4_state_owner *sp;
2767 struct nfs4_state *state = NULL;
2768 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002769 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002770 struct dentry *dentry = ctx->dentry;
2771 struct rpc_cred *cred = ctx->cred;
2772 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2773 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002774 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002775 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002776 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002780 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2781 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2783 goto out_err;
2784 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002785 status = nfs4_recover_expired_lease(server);
2786 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002787 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002788 if (d_really_is_positive(dentry))
2789 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002790 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002791 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002792 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002793 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002794 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002795 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002796 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
David Quigley14c43f72013-05-22 12:50:43 -04002798 if (label) {
2799 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2800 if (IS_ERR(olabel)) {
2801 status = PTR_ERR(olabel);
2802 goto err_opendata_put;
2803 }
2804 }
2805
Trond Myklebuste911b812014-03-26 13:24:37 -07002806 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2807 if (!opendata->f_attr.mdsthreshold) {
2808 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2809 if (!opendata->f_attr.mdsthreshold)
2810 goto err_free_label;
2811 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002812 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002813 }
David Howells2b0143b2015-03-17 22:25:59 +00002814 if (d_really_is_positive(dentry))
2815 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002816
Trond Myklebust3efb9722013-05-29 13:17:04 -04002817 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002818 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002819 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002820 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002821
NeilBrownefcbc042015-07-30 13:00:56 +10002822 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002823 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002824 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002825 /*
2826 * send create attributes which was not set by open
2827 * with an extra setattr.
2828 */
2829 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2830 nfs_fattr_init(opendata->o_res.f_attr);
2831 status = nfs4_do_setattr(state->inode, cred,
2832 opendata->o_res.f_attr, sattr,
2833 state, label, olabel);
2834 if (status == 0) {
2835 nfs_setattr_update_inode(state->inode, sattr,
2836 opendata->o_res.f_attr);
2837 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2838 }
David Quigley1775fd32013-05-22 12:50:42 -04002839 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002840 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002841 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002842 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002843
Trond Myklebuste911b812014-03-26 13:24:37 -07002844 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002845 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002846 opendata->f_attr.mdsthreshold = NULL;
2847 }
Andy Adamson82be4172012-05-23 05:02:35 -04002848
David Quigley14c43f72013-05-22 12:50:43 -04002849 nfs4_label_free(olabel);
2850
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002851 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002854err_free_label:
2855 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002856err_opendata_put:
2857 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002858err_put_state_owner:
2859 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return status;
2862}
2863
2864
Andy Adamson82be4172012-05-23 05:02:35 -04002865static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002866 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002867 int flags,
2868 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002869 struct nfs4_label *label,
2870 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002872 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct nfs4_exception exception = { };
2874 struct nfs4_state *res;
2875 int status;
2876
2877 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002878 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002879 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002880 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 if (status == 0)
2882 break;
2883 /* NOTE: BAD_SEQID means the server and client disagree about the
2884 * book-keeping w.r.t. state-changing operations
2885 * (OPEN/CLOSE/LOCK/LOCKU...)
2886 * It is actually a sign of a bug on the client or on the server.
2887 *
2888 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002889 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 * have unhashed the old state_owner for us, and that we can
2891 * therefore safely retry using a new one. We should still warn
2892 * the user though...
2893 */
2894 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002895 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002896 " returned a bad sequence-id error!\n",
2897 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 exception.retry = 1;
2899 continue;
2900 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002901 /*
2902 * BAD_STATEID on OPEN means that the server cancelled our
2903 * state before it received the OPEN_CONFIRM.
2904 * Recover by retrying the request as per the discussion
2905 * on Page 181 of RFC3530.
2906 */
2907 if (status == -NFS4ERR_BAD_STATEID) {
2908 exception.retry = 1;
2909 continue;
2910 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002911 if (status == -EAGAIN) {
2912 /* We must have found a delegation */
2913 exception.retry = 1;
2914 continue;
2915 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002916 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2917 continue;
2918 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 status, &exception));
2920 } while (exception.retry);
2921 return res;
2922}
2923
Trond Myklebust8487c472016-06-26 08:44:35 -04002924static int _nfs4_do_setattr(struct inode *inode,
2925 struct nfs_setattrargs *arg,
2926 struct nfs_setattrres *res,
2927 struct rpc_cred *cred,
2928 struct nfs4_state *state)
2929{
2930 struct nfs_server *server = NFS_SERVER(inode);
2931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2933 .rpc_argp = arg,
2934 .rpc_resp = res,
2935 .rpc_cred = cred,
2936 };
2937 struct rpc_cred *delegation_cred = NULL;
2938 unsigned long timestamp = jiffies;
2939 fmode_t fmode;
2940 bool truncate;
2941 int status;
2942
2943 nfs_fattr_init(res->fattr);
2944
2945 /* Servers should only apply open mode checks for file size changes */
2946 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2947 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2948
2949 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2950 /* Use that stateid */
2951 } else if (truncate && state != NULL) {
2952 struct nfs_lockowner lockowner = {
2953 .l_owner = current->files,
2954 .l_pid = current->tgid,
2955 };
2956 if (!nfs4_valid_open_stateid(state))
2957 return -EBADF;
2958 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2959 &arg->stateid, &delegation_cred) == -EIO)
2960 return -EBADF;
2961 } else
2962 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2963 if (delegation_cred)
2964 msg.rpc_cred = delegation_cred;
2965
2966 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2967
2968 put_rpccred(delegation_cred);
2969 if (status == 0 && state != NULL)
2970 renew_lease(server, timestamp);
2971 trace_nfs4_setattr(inode, &arg->stateid, status);
2972 return status;
2973}
2974
2975static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2976 struct nfs_fattr *fattr, struct iattr *sattr,
2977 struct nfs4_state *state, struct nfs4_label *ilabel,
2978 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002980 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002982 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 .iap = sattr,
2984 .server = server,
2985 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002986 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 };
2988 struct nfs_setattrres res = {
2989 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002990 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 .server = server,
2992 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002993 struct nfs4_exception exception = {
2994 .state = state,
2995 .inode = inode,
2996 .stateid = &arg.stateid,
2997 };
2998 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
David Quigleyaa9c2662013-05-22 12:50:44 -04003000 arg.bitmask = nfs4_bitmask(server, ilabel);
3001 if (ilabel)
3002 arg.bitmask = nfs4_bitmask(server, olabel);
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003005 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003006 switch (err) {
3007 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003008 if (!(sattr->ia_valid & ATTR_SIZE)) {
3009 pr_warn_once("NFSv4: server %s is incorrectly "
3010 "applying open mode checks to "
3011 "a SETATTR that is not "
3012 "changing file size.\n",
3013 server->nfs_client->cl_hostname);
3014 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003015 if (state && !(state->state & FMODE_WRITE)) {
3016 err = -EBADF;
3017 if (sattr->ia_valid & ATTR_OPEN)
3018 err = -EACCES;
3019 goto out;
3020 }
3021 }
3022 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003024out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return err;
3026}
3027
Peng Tao500d7012015-09-22 11:35:22 +08003028static bool
3029nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3030{
3031 if (inode == NULL || !nfs_have_layout(inode))
3032 return false;
3033
3034 return pnfs_wait_on_layoutreturn(inode, task);
3035}
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037struct nfs4_closedata {
3038 struct inode *inode;
3039 struct nfs4_state *state;
3040 struct nfs_closeargs arg;
3041 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003042 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003043 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003044 bool roc;
3045 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046};
3047
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003048static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003049{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003050 struct nfs4_closedata *calldata = data;
3051 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003052 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003053
Fred Isamanf7e89172011-01-06 11:36:32 +00003054 if (calldata->roc)
3055 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003056 nfs4_put_open_state(calldata->state);
3057 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003058 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003059 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003060 kfree(calldata);
3061}
3062
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003063static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003065 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003068 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Chuck Levera3ca5652012-03-01 17:00:40 -05003070 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003071 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3072 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003073 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 /* hmm. we are done with the inode, and in the process of freeing
3075 * the state_owner. we keep this around to process errors
3076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 switch (task->tk_status) {
3078 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003079 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003080 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003081 pnfs_roc_set_barrier(state->inode,
3082 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003083 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003084 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003085 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003087 case -NFS4ERR_EXPIRED:
3088 nfs4_free_revoked_stateid(server,
3089 &calldata->arg.stateid,
3090 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003091 case -NFS4ERR_OLD_STATEID:
3092 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003093 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003094 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003095 rpc_restart_call_prepare(task);
3096 goto out_release;
3097 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003098 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003101 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003102 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003103 goto out_release;
3104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003106 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3107 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003108out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003109 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003110 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003111 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003114static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003116 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003117 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003118 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003119 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003120 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003121
Chuck Levera3ca5652012-03-01 17:00:40 -05003122 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003123 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003124 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003125
Trond Myklebust88069f72009-12-08 08:33:16 -05003126 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003127 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003128 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3129 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3130 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003131 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003132 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003133 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003134 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003135 if (state->n_rdonly == 0)
3136 call_close |= is_rdonly;
3137 else if (is_rdonly)
3138 calldata->arg.fmode |= FMODE_READ;
3139 if (state->n_wronly == 0)
3140 call_close |= is_wronly;
3141 else if (is_wronly)
3142 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003143 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3144 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003145 } else if (is_rdwr)
3146 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3147
Trond Myklebust5cc78612016-11-14 11:19:56 -05003148 if (!nfs4_valid_open_stateid(state) ||
3149 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003150 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003151 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003152
3153 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003154 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003155 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003156 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003157
Peng Tao500d7012015-09-22 11:35:22 +08003158 if (nfs4_wait_on_layoutreturn(inode, task)) {
3159 nfs_release_seqid(calldata->arg.seqid);
3160 goto out_wait;
3161 }
3162
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003163 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003164 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003165 if (calldata->roc)
3166 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3167
Trond Myklebust6ae37332015-01-30 14:21:14 -05003168 calldata->arg.share_access =
3169 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3170 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003171
Trond Myklebust516a6af2005-10-27 22:12:41 -04003172 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003173 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003174 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003175 &calldata->arg.seq_args,
3176 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003177 task) != 0)
3178 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003179 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003180 return;
3181out_no_action:
3182 task->tk_action = NULL;
3183out_wait:
3184 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003187static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003188 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003189 .rpc_call_done = nfs4_close_done,
3190 .rpc_release = nfs4_free_closedata,
3191};
3192
Peng Taofe08c542014-07-03 13:05:01 +08003193static bool nfs4_roc(struct inode *inode)
3194{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003195 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003196 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003197 return pnfs_roc(inode);
3198}
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200/*
3201 * It is possible for data to be read/written from a mem-mapped file
3202 * after the sys_close call (which hits the vfs layer as a flush).
3203 * This means that we can't safely call nfsv4 close on a file until
3204 * the inode is cleared. This in turn means that we are not good
3205 * NFSv4 citizens - we do not indicate to the server to update the file's
3206 * share state even when we are done with one of the three share
3207 * stateid's in the inode.
3208 *
3209 * NOTE: Caller must be holding the sp->so_owner semaphore!
3210 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003211int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003213 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003214 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003216 struct nfs4_state_owner *sp = state->owner;
3217 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003218 struct rpc_message msg = {
3219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3220 .rpc_cred = state->owner->so_cred,
3221 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003222 struct rpc_task_setup task_setup_data = {
3223 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003224 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003225 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003226 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003227 .flags = RPC_TASK_ASYNC,
3228 };
Trond Myklebust95121352005-10-18 14:20:12 -07003229 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003231 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3232 &task_setup_data.rpc_client, &msg);
3233
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003234 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003236 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003237 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003238 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003240 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003242 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3243 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003244 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003245 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003246 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003247 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003248 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003249 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003250 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003251 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003252 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003253
Trond Myklebust1174dd12010-12-21 10:52:24 -05003254 msg.rpc_argp = &calldata->arg;
3255 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003256 task_setup_data.callback_data = calldata;
3257 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003258 if (IS_ERR(task))
3259 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003260 status = 0;
3261 if (wait)
3262 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003263 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003264 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003265out_free_calldata:
3266 kfree(calldata);
3267out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003268 nfs4_put_open_state(state);
3269 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003270 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
Trond Myklebust2b484292010-09-17 10:56:51 -04003273static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003274nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3275 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003278 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3279
3280 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Trond Myklebust565277f2007-10-15 18:17:53 -04003282 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003283 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003284
3285 nfs4_label_release_security(label);
3286
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003287 if (IS_ERR(state))
3288 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003289 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
Trond Myklebust1185a552009-12-03 15:54:02 -05003292static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003293{
3294 if (ctx->state == NULL)
3295 return;
3296 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003297 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003298 else
Al Viro643168c2011-06-22 18:20:23 -04003299 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003300}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Trond Myklebustb944dba2013-11-04 15:20:20 -05003302#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3303#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003304#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3307{
Kinglong Mee8c612822015-08-26 21:12:58 +08003308 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003309 struct nfs4_server_caps_arg args = {
3310 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003311 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003312 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct nfs4_server_caps_res res = {};
3314 struct rpc_message msg = {
3315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003316 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 .rpc_resp = &res,
3318 };
3319 int status;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003320 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Kinglong Mee8c612822015-08-26 21:12:58 +08003322 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3323 FATTR4_WORD0_FH_EXPIRE_TYPE |
3324 FATTR4_WORD0_LINK_SUPPORT |
3325 FATTR4_WORD0_SYMLINK_SUPPORT |
3326 FATTR4_WORD0_ACLSUPPORT;
3327 if (minorversion)
3328 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3329
Bryan Schumaker7c513052011-03-24 17:12:24 +00003330 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003332 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003333 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003334 case 0:
3335 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3336 res.attr_bitmask[2] = 0;
3337 break;
3338 case 1:
3339 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3340 break;
3341 case 2:
3342 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003345 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3346 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3347 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3348 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003349 NFS_CAP_CTIME|NFS_CAP_MTIME|
3350 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003351 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3352 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 server->caps |= NFS_CAP_ACLS;
3354 if (res.has_links != 0)
3355 server->caps |= NFS_CAP_HARDLINKS;
3356 if (res.has_symlinks != 0)
3357 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003358 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3359 server->caps |= NFS_CAP_FILEID;
3360 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3361 server->caps |= NFS_CAP_MODE;
3362 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3363 server->caps |= NFS_CAP_NLINK;
3364 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3365 server->caps |= NFS_CAP_OWNER;
3366 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3367 server->caps |= NFS_CAP_OWNER_GROUP;
3368 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3369 server->caps |= NFS_CAP_ATIME;
3370 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3371 server->caps |= NFS_CAP_CTIME;
3372 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3373 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003374#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3375 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3376 server->caps |= NFS_CAP_SECURITY_LABEL;
3377#endif
3378 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3379 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003380 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003381
Trond Myklebusta65318b2009-03-11 14:10:28 -04003382 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3383 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3384 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003385 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003386
3387 /* Avoid a regression due to buggy server */
3388 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3389 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003390 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3391 sizeof(server->exclcreat_bitmask));
Trond Myklebustfca22d52017-05-09 15:47:15 -04003392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003394 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return status;
3398}
3399
Trond Myklebust55a97592006-06-09 09:34:19 -04003400int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
3402 struct nfs4_exception exception = { };
3403 int err;
3404 do {
3405 err = nfs4_handle_exception(server,
3406 _nfs4_server_capabilities(server, fhandle),
3407 &exception);
3408 } while (exception.retry);
3409 return err;
3410}
3411
3412static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3413 struct nfs_fsinfo *info)
3414{
David Quigleyaa9c2662013-05-22 12:50:44 -04003415 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003417 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 };
3419 struct nfs4_lookup_res res = {
3420 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003421 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 .fh = fhandle,
3423 };
3424 struct rpc_message msg = {
3425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3426 .rpc_argp = &args,
3427 .rpc_resp = &res,
3428 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003429
David Quigleyaa9c2662013-05-22 12:50:44 -04003430 bitmask[0] = nfs4_fattr_bitmap[0];
3431 bitmask[1] = nfs4_fattr_bitmap[1];
3432 /*
3433 * Process the label in the upcoming getfattr
3434 */
3435 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3436
Trond Myklebust0e574af2005-10-27 22:12:38 -04003437 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003438 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
3441static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3442 struct nfs_fsinfo *info)
3443{
3444 struct nfs4_exception exception = { };
3445 int err;
3446 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003447 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003448 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003449 switch (err) {
3450 case 0:
3451 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003452 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003453 default:
3454 err = nfs4_handle_exception(server, err, &exception);
3455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003457out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return err;
3459}
3460
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003461static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3462 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3463{
Trond Myklebustc2190662013-08-26 19:23:04 -04003464 struct rpc_auth_create_args auth_args = {
3465 .pseudoflavor = flavor,
3466 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003467 struct rpc_auth *auth;
3468 int ret;
3469
Trond Myklebustc2190662013-08-26 19:23:04 -04003470 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003471 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003472 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003473 goto out;
3474 }
3475 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003476out:
3477 return ret;
3478}
3479
Chuck Lever9a744ba2013-03-16 15:56:02 -04003480/*
3481 * Retry pseudoroot lookup with various security flavors. We do this when:
3482 *
3483 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3484 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3485 *
3486 * Returns zero on success, or a negative NFS4ERR value, or a
3487 * negative errno value.
3488 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003489static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003490 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003492 /* Per 3530bis 15.33.5 */
3493 static const rpc_authflavor_t flav_array[] = {
3494 RPC_AUTH_GSS_KRB5P,
3495 RPC_AUTH_GSS_KRB5I,
3496 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003497 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003498 RPC_AUTH_NULL,
3499 };
3500 int status = -EPERM;
3501 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003503 if (server->auth_info.flavor_len > 0) {
3504 /* try each flavor specified by user */
3505 for (i = 0; i < server->auth_info.flavor_len; i++) {
3506 status = nfs4_lookup_root_sec(server, fhandle, info,
3507 server->auth_info.flavors[i]);
3508 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3509 continue;
3510 break;
3511 }
3512 } else {
3513 /* no flavors specified by user, try default list */
3514 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3515 status = nfs4_lookup_root_sec(server, fhandle, info,
3516 flav_array[i]);
3517 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3518 continue;
3519 break;
3520 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003521 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003522
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003523 /*
3524 * -EACCESS could mean that the user doesn't have correct permissions
3525 * to access the mount. It could also mean that we tried to mount
3526 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3527 * existing mount programs don't handle -EACCES very well so it should
3528 * be mapped to -EPERM instead.
3529 */
3530 if (status == -EACCES)
3531 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003532 return status;
3533}
3534
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003535/**
3536 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3537 * @server: initialized nfs_server handle
3538 * @fhandle: we fill in the pseudo-fs root file handle
3539 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003540 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003541 *
3542 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003543 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003544int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003545 struct nfs_fsinfo *info,
3546 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003547{
Andre Przywarac7757072015-04-23 17:17:40 +01003548 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003549
Andre Przywarac7757072015-04-23 17:17:40 +01003550 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003551 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003552
3553 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003554 status = server->nfs_client->cl_mvops->find_root_sec(server,
3555 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (status == 0)
3558 status = nfs4_server_capabilities(server, fhandle);
3559 if (status == 0)
3560 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003561
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003562 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Bryan Schumakerbae36242012-05-10 15:07:31 -04003565static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3566 struct nfs_fsinfo *info)
3567{
3568 int error;
3569 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003570 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003571
3572 error = nfs4_server_capabilities(server, mntfh);
3573 if (error < 0) {
3574 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3575 return error;
3576 }
3577
David Quigley14c43f72013-05-22 12:50:43 -04003578 label = nfs4_label_alloc(server, GFP_KERNEL);
3579 if (IS_ERR(label))
3580 return PTR_ERR(label);
3581
David Quigley1775fd32013-05-22 12:50:42 -04003582 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003583 if (error < 0) {
3584 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003585 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003586 }
3587
3588 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3589 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3590 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3591
David Quigley14c43f72013-05-22 12:50:43 -04003592err_free_label:
3593 nfs4_label_free(label);
3594
Bryan Schumakerbae36242012-05-10 15:07:31 -04003595 return error;
3596}
3597
Manoj Naik6b97fd32006-06-09 09:34:29 -04003598/*
3599 * Get locations and (maybe) other attributes of a referral.
3600 * Note that we'll actually follow the referral later when
3601 * we detect fsid mismatch in inode revalidation
3602 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003603static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3604 const struct qstr *name, struct nfs_fattr *fattr,
3605 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003606{
3607 int status = -ENOMEM;
3608 struct page *page = NULL;
3609 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003610
3611 page = alloc_page(GFP_KERNEL);
3612 if (page == NULL)
3613 goto out;
3614 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3615 if (locations == NULL)
3616 goto out;
3617
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003618 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003619 if (status != 0)
3620 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003621
3622 /*
3623 * If the fsid didn't change, this is a migration event, not a
3624 * referral. Cause us to drop into the exception handler, which
3625 * will kick off migration recovery.
3626 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003627 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003628 dprintk("%s: server did not return a different fsid for"
3629 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003630 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003631 goto out;
3632 }
Andy Adamson533eb462011-06-13 18:25:56 -04003633 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3634 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003635
Andy Adamson533eb462011-06-13 18:25:56 -04003636 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003637 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003638 memset(fhandle, 0, sizeof(struct nfs_fh));
3639out:
3640 if (page)
3641 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003642 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003643 return status;
3644}
3645
David Quigley1775fd32013-05-22 12:50:42 -04003646static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3647 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
3649 struct nfs4_getattr_arg args = {
3650 .fh = fhandle,
3651 .bitmask = server->attr_bitmask,
3652 };
3653 struct nfs4_getattr_res res = {
3654 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003655 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 .server = server,
3657 };
3658 struct rpc_message msg = {
3659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3660 .rpc_argp = &args,
3661 .rpc_resp = &res,
3662 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003663
3664 args.bitmask = nfs4_bitmask(server, label);
3665
Trond Myklebust0e574af2005-10-27 22:12:38 -04003666 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003667 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
David Quigley1775fd32013-05-22 12:50:42 -04003670static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3671 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
3673 struct nfs4_exception exception = { };
3674 int err;
3675 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003676 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3677 trace_nfs4_getattr(server, fhandle, fattr, err);
3678 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 &exception);
3680 } while (exception.retry);
3681 return err;
3682}
3683
3684/*
3685 * The file is not closed if it is opened due to the a request to change
3686 * the size of the file. The open call will not be needed once the
3687 * VFS layer lookup-intents are implemented.
3688 *
3689 * Close is called when the inode is destroyed.
3690 * If we haven't opened the file for O_WRONLY, we
3691 * need to in the size_change case to obtain a stateid.
3692 *
3693 * Got race?
3694 * Because OPEN is always done by name in nfsv4, it is
3695 * possible that we opened a different file by the same
3696 * name. We can recognize this race condition, but we
3697 * can't do anything about it besides returning an error.
3698 *
3699 * This will be fixed with VFS changes (lookup-intent).
3700 */
3701static int
3702nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3703 struct iattr *sattr)
3704{
David Howells2b0143b2015-03-17 22:25:59 +00003705 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003706 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003707 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003708 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 int status;
3710
Peng Tao88ac8152014-09-12 11:04:10 +08003711 if (pnfs_ld_layoutret_on_setattr(inode) &&
3712 sattr->ia_valid & ATTR_SIZE &&
3713 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003714 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003715
Trond Myklebust0e574af2005-10-27 22:12:38 -04003716 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Andy Adamson26699402012-05-30 16:12:24 -04003718 /* Deal with open(O_TRUNC) */
3719 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003720 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003721
3722 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003723 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003724 return 0;
3725
Trond Myklebustd5308382005-11-04 15:33:38 -05003726 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003727 if (sattr->ia_valid & ATTR_FILE) {
3728 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003729
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003730 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003731 if (ctx) {
3732 cred = ctx->cred;
3733 state = ctx->state;
3734 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003735 }
3736
David Quigley14c43f72013-05-22 12:50:43 -04003737 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3738 if (IS_ERR(label))
3739 return PTR_ERR(label);
3740
3741 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003742 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003743 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003744 nfs_setsecurity(inode, fattr, label);
3745 }
David Quigley14c43f72013-05-22 12:50:43 -04003746 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 return status;
3748}
3749
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003750static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3751 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003752 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003753{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003754 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003755 int status;
3756 struct nfs4_lookup_arg args = {
3757 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003758 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003759 .name = name,
3760 };
3761 struct nfs4_lookup_res res = {
3762 .server = server,
3763 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003764 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003765 .fh = fhandle,
3766 };
3767 struct rpc_message msg = {
3768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3769 .rpc_argp = &args,
3770 .rpc_resp = &res,
3771 };
3772
David Quigleyaa9c2662013-05-22 12:50:44 -04003773 args.bitmask = nfs4_bitmask(server, label);
3774
David Howells2b3de442006-08-22 20:06:09 -04003775 nfs_fattr_init(fattr);
3776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003778 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 dprintk("NFS reply lookup: %d\n", status);
3780 return status;
3781}
3782
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003783static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003784{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003785 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003786 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003787 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3788 fattr->nlink = 2;
3789}
3790
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003791static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003792 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003793 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003794{
3795 struct nfs4_exception exception = { };
3796 struct rpc_clnt *client = *clnt;
3797 int err;
3798 do {
David Quigley1775fd32013-05-22 12:50:42 -04003799 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003800 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003801 switch (err) {
3802 case -NFS4ERR_BADNAME:
3803 err = -ENOENT;
3804 goto out;
3805 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003806 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003807 if (err == -NFS4ERR_MOVED)
3808 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003809 goto out;
3810 case -NFS4ERR_WRONGSEC:
3811 err = -EPERM;
3812 if (client != *clnt)
3813 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003814 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003815 if (IS_ERR(client))
3816 return PTR_ERR(client);
3817
3818 exception.retry = 1;
3819 break;
3820 default:
3821 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3822 }
3823 } while (exception.retry);
3824
3825out:
3826 if (err == 0)
3827 *clnt = client;
3828 else if (client != *clnt)
3829 rpc_shutdown_client(client);
3830
3831 return err;
3832}
3833
Al Virobeffb8f2016-07-20 16:34:42 -04003834static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003835 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3836 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003838 int status;
3839 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003840
David Quigley1775fd32013-05-22 12:50:42 -04003841 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003842 if (client != NFS_CLIENT(dir)) {
3843 rpc_shutdown_client(client);
3844 nfs_fixup_secinfo_attributes(fattr);
3845 }
3846 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003849struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003850nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003851 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3852{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003853 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003854 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003855
David Quigley1775fd32013-05-22 12:50:42 -04003856 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003857 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003858 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003859 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003860}
3861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3863{
Trond Myklebust76b32992007-08-10 17:45:11 -04003864 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 struct nfs4_accessargs args = {
3866 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003867 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003869 struct nfs4_accessres res = {
3870 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003871 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 struct rpc_message msg = {
3873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3874 .rpc_argp = &args,
3875 .rpc_resp = &res,
3876 .rpc_cred = entry->cred,
3877 };
3878 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003879 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
3881 /*
3882 * Determine which access bits we want to ask for...
3883 */
3884 if (mode & MAY_READ)
3885 args.access |= NFS4_ACCESS_READ;
3886 if (S_ISDIR(inode->i_mode)) {
3887 if (mode & MAY_WRITE)
3888 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3889 if (mode & MAY_EXEC)
3890 args.access |= NFS4_ACCESS_LOOKUP;
3891 } else {
3892 if (mode & MAY_WRITE)
3893 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3894 if (mode & MAY_EXEC)
3895 args.access |= NFS4_ACCESS_EXECUTE;
3896 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003897
3898 res.fattr = nfs_alloc_fattr();
3899 if (res.fattr == NULL)
3900 return -ENOMEM;
3901
Bryan Schumaker7c513052011-03-24 17:12:24 +00003902 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003904 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003905 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003907 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 return status;
3909}
3910
3911static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3912{
3913 struct nfs4_exception exception = { };
3914 int err;
3915 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003916 err = _nfs4_proc_access(inode, entry);
3917 trace_nfs4_access(inode, err);
3918 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 &exception);
3920 } while (exception.retry);
3921 return err;
3922}
3923
3924/*
3925 * TODO: For the time being, we don't try to get any attributes
3926 * along with any of the zero-copy operations READ, READDIR,
3927 * READLINK, WRITE.
3928 *
3929 * In the case of the first three, we want to put the GETATTR
3930 * after the read-type operation -- this is because it is hard
3931 * to predict the length of a GETATTR response in v4, and thus
3932 * align the READ data correctly. This means that the GETATTR
3933 * may end up partially falling into the page cache, and we should
3934 * shift it into the 'tail' of the xdr_buf before processing.
3935 * To do this efficiently, we need to know the total length
3936 * of data received, which doesn't seem to be available outside
3937 * of the RPC layer.
3938 *
3939 * In the case of WRITE, we also want to put the GETATTR after
3940 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003941 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 *
3943 * Both of these changes to the XDR layer would in fact be quite
3944 * minor, but I decided to leave them for a subsequent patch.
3945 */
3946static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3947 unsigned int pgbase, unsigned int pglen)
3948{
3949 struct nfs4_readlink args = {
3950 .fh = NFS_FH(inode),
3951 .pgbase = pgbase,
3952 .pglen = pglen,
3953 .pages = &page,
3954 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003955 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 struct rpc_message msg = {
3957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3958 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003959 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 };
3961
Bryan Schumaker7c513052011-03-24 17:12:24 +00003962 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 -07003963}
3964
3965static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3966 unsigned int pgbase, unsigned int pglen)
3967{
3968 struct nfs4_exception exception = { };
3969 int err;
3970 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003971 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3972 trace_nfs4_readlink(inode, err);
3973 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 &exception);
3975 } while (exception.retry);
3976 return err;
3977}
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003980 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982static int
3983nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003984 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
David Quigleyaa9c2662013-05-22 12:50:44 -04003986 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003987 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 int status = 0;
3990
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003991 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3992 if (IS_ERR(ctx))
3993 return PTR_ERR(ctx);
3994
David Quigleyaa9c2662013-05-22 12:50:44 -04003995 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3996
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003997 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003998 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 if (IS_ERR(state)) {
4000 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004004 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004005 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 return status;
4007}
4008
Al Virobeffb8f2016-07-20 16:34:42 -04004009static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Trond Myklebust16e42952005-10-27 22:12:44 -04004011 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004012 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004014 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004016 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004017 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4021 .rpc_argp = &args,
4022 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004024 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004025
Bryan Schumaker7c513052011-03-24 17:12:24 +00004026 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004027 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004028 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 return status;
4030}
4031
Al Virobeffb8f2016-07-20 16:34:42 -04004032static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
4034 struct nfs4_exception exception = { };
4035 int err;
4036 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004037 err = _nfs4_proc_remove(dir, name);
4038 trace_nfs4_remove(dir, name, err);
4039 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 &exception);
4041 } while (exception.retry);
4042 return err;
4043}
4044
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004045static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004047 struct nfs_server *server = NFS_SERVER(dir);
4048 struct nfs_removeargs *args = msg->rpc_argp;
4049 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004051 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004053 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004054
4055 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004058static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4059{
Al Viro884be172016-04-28 23:56:31 -04004060 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004061 &data->args.seq_args,
4062 &data->res.seq_res,
4063 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004066static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004068 struct nfs_unlinkdata *data = task->tk_calldata;
4069 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004070
Trond Myklebust14516c32010-07-31 14:29:06 -04004071 if (!nfs4_sequence_done(task, &res->seq_res))
4072 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004073 if (nfs4_async_handle_error(task, res->server, NULL,
4074 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004075 return 0;
4076 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004077 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
Jeff Laytond3d41522010-09-17 17:31:57 -04004080static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4081{
4082 struct nfs_server *server = NFS_SERVER(dir);
4083 struct nfs_renameargs *arg = msg->rpc_argp;
4084 struct nfs_renameres *res = msg->rpc_resp;
4085
4086 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004087 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004088 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004089}
4090
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004091static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4092{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004093 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4094 &data->args.seq_args,
4095 &data->res.seq_res,
4096 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004097}
4098
4099static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4100 struct inode *new_dir)
4101{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004102 struct nfs_renamedata *data = task->tk_calldata;
4103 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004104
4105 if (!nfs4_sequence_done(task, &res->seq_res))
4106 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004107 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004108 return 0;
4109
4110 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004111 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004112 return 1;
4113}
4114
Al Virobeffb8f2016-07-20 16:34:42 -04004115static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004117 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 struct nfs4_link_arg arg = {
4119 .fh = NFS_FH(inode),
4120 .dir_fh = NFS_FH(dir),
4121 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004122 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004124 struct nfs4_link_res res = {
4125 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004126 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004127 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 struct rpc_message msg = {
4129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4130 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004131 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004133 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Trond Myklebust136f2622010-04-16 16:22:49 -04004135 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004136 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004137 goto out;
4138
David Quigley14c43f72013-05-22 12:50:43 -04004139 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4140 if (IS_ERR(res.label)) {
4141 status = PTR_ERR(res.label);
4142 goto out;
4143 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004144 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004145
Bryan Schumaker7c513052011-03-24 17:12:24 +00004146 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004147 if (!status) {
4148 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004149 status = nfs_post_op_update_inode(inode, res.fattr);
4150 if (!status)
4151 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004152 }
David Quigley14c43f72013-05-22 12:50:43 -04004153
4154
4155 nfs4_label_free(res.label);
4156
Trond Myklebust136f2622010-04-16 16:22:49 -04004157out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004158 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 return status;
4160}
4161
Al Virobeffb8f2016-07-20 16:34:42 -04004162static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
4164 struct nfs4_exception exception = { };
4165 int err;
4166 do {
4167 err = nfs4_handle_exception(NFS_SERVER(inode),
4168 _nfs4_proc_link(inode, dir, name),
4169 &exception);
4170 } while (exception.retry);
4171 return err;
4172}
4173
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004174struct nfs4_createdata {
4175 struct rpc_message msg;
4176 struct nfs4_create_arg arg;
4177 struct nfs4_create_res res;
4178 struct nfs_fh fh;
4179 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004180 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004181};
4182
4183static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004184 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004185{
4186 struct nfs4_createdata *data;
4187
4188 data = kzalloc(sizeof(*data), GFP_KERNEL);
4189 if (data != NULL) {
4190 struct nfs_server *server = NFS_SERVER(dir);
4191
David Quigley14c43f72013-05-22 12:50:43 -04004192 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4193 if (IS_ERR(data->label))
4194 goto out_free;
4195
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004196 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4197 data->msg.rpc_argp = &data->arg;
4198 data->msg.rpc_resp = &data->res;
4199 data->arg.dir_fh = NFS_FH(dir);
4200 data->arg.server = server;
4201 data->arg.name = name;
4202 data->arg.attrs = sattr;
4203 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004204 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004205 data->res.server = server;
4206 data->res.fh = &data->fh;
4207 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004208 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004209 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004210 }
4211 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004212out_free:
4213 kfree(data);
4214 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004215}
4216
4217static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4218{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004219 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004220 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221 if (status == 0) {
4222 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004223 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004224 }
4225 return status;
4226}
4227
4228static void nfs4_free_createdata(struct nfs4_createdata *data)
4229{
David Quigley14c43f72013-05-22 12:50:43 -04004230 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004231 kfree(data);
4232}
4233
Chuck Lever4f390c12006-08-22 20:06:22 -04004234static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004235 struct page *page, unsigned int len, struct iattr *sattr,
4236 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004238 struct nfs4_createdata *data;
4239 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Chuck Lever94a6d752006-08-22 20:06:23 -04004241 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004243
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004244 status = -ENOMEM;
4245 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4246 if (data == NULL)
4247 goto out;
4248
4249 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4250 data->arg.u.symlink.pages = &page;
4251 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004252 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004254 status = nfs4_do_create(dir, dentry, data);
4255
4256 nfs4_free_createdata(data);
4257out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return status;
4259}
4260
Chuck Lever4f390c12006-08-22 20:06:22 -04004261static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004262 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004265 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004267
4268 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004271 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4272 trace_nfs4_symlink(dir, &dentry->d_name, err);
4273 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 &exception);
4275 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004276
4277 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return err;
4279}
4280
4281static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004282 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004284 struct nfs4_createdata *data;
4285 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004287 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4288 if (data == NULL)
4289 goto out;
4290
David Quigley1775fd32013-05-22 12:50:42 -04004291 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004292 status = nfs4_do_create(dir, dentry, data);
4293
4294 nfs4_free_createdata(data);
4295out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 return status;
4297}
4298
4299static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4300 struct iattr *sattr)
4301{
4302 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004303 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004305
David Quigleyaa9c2662013-05-22 12:50:44 -04004306 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4307
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004308 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004310 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4311 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4312 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 &exception);
4314 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004315 nfs4_label_release_security(label);
4316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return err;
4318}
4319
4320static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004321 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
David Howells2b0143b2015-03-17 22:25:59 +00004323 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 struct nfs4_readdir_arg args = {
4325 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004326 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 .pgbase = 0,
4328 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004329 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004330 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 };
4332 struct nfs4_readdir_res res;
4333 struct rpc_message msg = {
4334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4335 .rpc_argp = &args,
4336 .rpc_resp = &res,
4337 .rpc_cred = cred,
4338 };
4339 int status;
4340
Al Viro6de14722013-09-16 10:53:17 -04004341 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4342 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004343 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004344 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004346 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 -05004347 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004348 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004349 status += args.pgbase;
4350 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004351
4352 nfs_invalidate_atime(dir);
4353
Harvey Harrison3110ff82008-05-02 13:42:44 -07004354 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 return status;
4356}
4357
4358static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004359 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
4361 struct nfs4_exception exception = { };
4362 int err;
4363 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004364 err = _nfs4_proc_readdir(dentry, cred, cookie,
4365 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004366 trace_nfs4_readdir(d_inode(dentry), err);
4367 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 &exception);
4369 } while (exception.retry);
4370 return err;
4371}
4372
4373static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004374 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004376 struct nfs4_createdata *data;
4377 int mode = sattr->ia_mode;
4378 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004380 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4381 if (data == NULL)
4382 goto out;
4383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004385 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004387 data->arg.ftype = NF4BLK;
4388 data->arg.u.device.specdata1 = MAJOR(rdev);
4389 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 }
4391 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004392 data->arg.ftype = NF4CHR;
4393 data->arg.u.device.specdata1 = MAJOR(rdev);
4394 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004395 } else if (!S_ISSOCK(mode)) {
4396 status = -EINVAL;
4397 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 }
David Quigley1775fd32013-05-22 12:50:42 -04004399
David Quigleyaa9c2662013-05-22 12:50:44 -04004400 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004402out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004403 nfs4_free_createdata(data);
4404out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 return status;
4406}
4407
4408static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4409 struct iattr *sattr, dev_t rdev)
4410{
4411 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004412 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004414
David Quigleyaa9c2662013-05-22 12:50:44 -04004415 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4416
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004417 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004419 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4420 trace_nfs4_mknod(dir, &dentry->d_name, err);
4421 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 &exception);
4423 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004424
4425 nfs4_label_release_security(label);
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 return err;
4428}
4429
4430static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4431 struct nfs_fsstat *fsstat)
4432{
4433 struct nfs4_statfs_arg args = {
4434 .fh = fhandle,
4435 .bitmask = server->attr_bitmask,
4436 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004437 struct nfs4_statfs_res res = {
4438 .fsstat = fsstat,
4439 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 struct rpc_message msg = {
4441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4442 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004443 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 };
4445
Trond Myklebust0e574af2005-10-27 22:12:38 -04004446 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004447 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448}
4449
4450static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4451{
4452 struct nfs4_exception exception = { };
4453 int err;
4454 do {
4455 err = nfs4_handle_exception(server,
4456 _nfs4_proc_statfs(server, fhandle, fsstat),
4457 &exception);
4458 } while (exception.retry);
4459 return err;
4460}
4461
4462static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4463 struct nfs_fsinfo *fsinfo)
4464{
4465 struct nfs4_fsinfo_arg args = {
4466 .fh = fhandle,
4467 .bitmask = server->attr_bitmask,
4468 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004469 struct nfs4_fsinfo_res res = {
4470 .fsinfo = fsinfo,
4471 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 struct rpc_message msg = {
4473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4474 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004475 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 };
4477
Bryan Schumaker7c513052011-03-24 17:12:24 +00004478 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479}
4480
4481static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4482{
4483 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004484 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 int err;
4486
4487 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004488 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004489 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004490 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004491 nfs4_set_lease_period(server->nfs_client,
4492 fsinfo->lease_time * HZ,
4493 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004494 break;
4495 }
4496 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 } while (exception.retry);
4498 return err;
4499}
4500
4501static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4502{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004503 int error;
4504
Trond Myklebust0e574af2005-10-27 22:12:38 -04004505 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004506 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004507 if (error == 0) {
4508 /* block layout checks this! */
4509 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004510 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004511 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004512
4513 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
4516static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4517 struct nfs_pathconf *pathconf)
4518{
4519 struct nfs4_pathconf_arg args = {
4520 .fh = fhandle,
4521 .bitmask = server->attr_bitmask,
4522 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004523 struct nfs4_pathconf_res res = {
4524 .pathconf = pathconf,
4525 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 struct rpc_message msg = {
4527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4528 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004529 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 };
4531
4532 /* None of the pathconf attributes are mandatory to implement */
4533 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4534 memset(pathconf, 0, sizeof(*pathconf));
4535 return 0;
4536 }
4537
Trond Myklebust0e574af2005-10-27 22:12:38 -04004538 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004539 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540}
4541
4542static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4543 struct nfs_pathconf *pathconf)
4544{
4545 struct nfs4_exception exception = { };
4546 int err;
4547
4548 do {
4549 err = nfs4_handle_exception(server,
4550 _nfs4_proc_pathconf(server, fhandle, pathconf),
4551 &exception);
4552 } while (exception.retry);
4553 return err;
4554}
4555
Trond Myklebust5521abf2013-03-16 20:54:34 -04004556int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004557 const struct nfs_open_context *ctx,
4558 const struct nfs_lock_context *l_ctx,
4559 fmode_t fmode)
4560{
4561 const struct nfs_lockowner *lockowner = NULL;
4562
4563 if (l_ctx != NULL)
4564 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004565 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004566}
4567EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4568
Trond Myklebust5521abf2013-03-16 20:54:34 -04004569static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4570 const struct nfs_open_context *ctx,
4571 const struct nfs_lock_context *l_ctx,
4572 fmode_t fmode)
4573{
4574 nfs4_stateid current_stateid;
4575
Trond Myklebuste1253be2014-03-05 08:44:23 -05004576 /* If the current stateid represents a lost lock, then exit */
4577 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4578 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004579 return nfs4_stateid_match(stateid, &current_stateid);
4580}
4581
4582static bool nfs4_error_stateid_expired(int err)
4583{
4584 switch (err) {
4585 case -NFS4ERR_DELEG_REVOKED:
4586 case -NFS4ERR_ADMIN_REVOKED:
4587 case -NFS4ERR_BAD_STATEID:
4588 case -NFS4ERR_STALE_STATEID:
4589 case -NFS4ERR_OLD_STATEID:
4590 case -NFS4ERR_OPENMODE:
4591 case -NFS4ERR_EXPIRED:
4592 return true;
4593 }
4594 return false;
4595}
4596
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004597static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004599 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004601 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004602 if (task->tk_status < 0) {
4603 struct nfs4_exception exception = {
4604 .inode = hdr->inode,
4605 .state = hdr->args.context->state,
4606 .stateid = &hdr->args.stateid,
4607 };
4608 task->tk_status = nfs4_async_handle_exception(task,
4609 server, task->tk_status, &exception);
4610 if (exception.retry) {
4611 rpc_restart_call_prepare(task);
4612 return -EAGAIN;
4613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004617 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619}
4620
Trond Myklebust5521abf2013-03-16 20:54:34 -04004621static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004622 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004623{
4624
4625 if (!nfs4_error_stateid_expired(task->tk_status) ||
4626 nfs4_stateid_is_current(&args->stateid,
4627 args->context,
4628 args->lock_context,
4629 FMODE_READ))
4630 return false;
4631 rpc_restart_call_prepare(task);
4632 return true;
4633}
4634
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004635static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004636{
4637
4638 dprintk("--> %s\n", __func__);
4639
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004641 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004642 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004643 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004644 if (task->tk_status > 0)
4645 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004646 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4647 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004648}
4649
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004650static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4651 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004654 if (!hdr->pgio_done_cb)
4655 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004656 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004660static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4661 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004662{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4664 &hdr->args.seq_args,
4665 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004666 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004667 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4669 hdr->args.lock_context,
4670 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004671 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004673 return -EIO;
4674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677static int nfs4_write_done_cb(struct rpc_task *task,
4678 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004681
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004682 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004683 if (task->tk_status < 0) {
4684 struct nfs4_exception exception = {
4685 .inode = hdr->inode,
4686 .state = hdr->args.context->state,
4687 .stateid = &hdr->args.stateid,
4688 };
4689 task->tk_status = nfs4_async_handle_exception(task,
4690 NFS_SERVER(inode), task->tk_status,
4691 &exception);
4692 if (exception.retry) {
4693 rpc_restart_call_prepare(task);
4694 return -EAGAIN;
4695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004697 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004698 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004699 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004700 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
Trond Myklebust5521abf2013-03-16 20:54:34 -04004704static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004705 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004706{
4707
4708 if (!nfs4_error_stateid_expired(task->tk_status) ||
4709 nfs4_stateid_is_current(&args->stateid,
4710 args->context,
4711 args->lock_context,
4712 FMODE_WRITE))
4713 return false;
4714 rpc_restart_call_prepare(task);
4715 return true;
4716}
4717
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004718static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004719{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004720 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004721 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004722 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004723 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004724 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4725 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004726}
4727
Trond Myklebust5a37f852012-04-28 14:55:16 -04004728static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004729bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004730{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004731 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004732 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004733 return false;
4734 /* Otherwise, request attributes if and only if we don't hold
4735 * a delegation
4736 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004737 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004738}
Fred Isamana69aef12011-03-03 15:13:47 +00004739
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4741 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004743 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004744
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004745 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4746 hdr->args.bitmask = NULL;
4747 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004748 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004749 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004750
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004751 if (!hdr->pgio_done_cb)
4752 hdr->pgio_done_cb = nfs4_write_done_cb;
4753 hdr->res.server = server;
4754 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004756 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004757 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
Fred Isaman0b7c0152012-04-20 14:47:39 -04004760static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4761{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004762 nfs4_setup_sequence(NFS_SERVER(data->inode),
4763 &data->args.seq_args,
4764 &data->res.seq_res,
4765 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
Fred Isaman0b7c0152012-04-20 14:47:39 -04004768static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004771
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004772 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004773 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4774 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004775 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004776 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
4780
Fred Isaman0b7c0152012-04-20 14:47:39 -04004781static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004782{
4783 if (!nfs4_sequence_done(task, &data->res.seq_res))
4784 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004785 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004786}
4787
Fred Isaman0b7c0152012-04-20 14:47:39 -04004788static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004790 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004791
Fred Isaman0b7c0152012-04-20 14:47:39 -04004792 if (data->commit_done_cb == NULL)
4793 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004794 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004795 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004796 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004799struct nfs4_renewdata {
4800 struct nfs_client *client;
4801 unsigned long timestamp;
4802};
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804/*
4805 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4806 * standalone procedure for queueing an asynchronous RENEW.
4807 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004808static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004809{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004810 struct nfs4_renewdata *data = calldata;
4811 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004812
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004813 if (atomic_read(&clp->cl_count) > 1)
4814 nfs4_schedule_state_renewal(clp);
4815 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004816 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004817}
4818
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004819static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004821 struct nfs4_renewdata *data = calldata;
4822 struct nfs_client *clp = data->client;
4823 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004825 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004826 switch (task->tk_status) {
4827 case 0:
4828 break;
4829 case -NFS4ERR_LEASE_MOVED:
4830 nfs4_schedule_lease_moved_recovery(clp);
4831 break;
4832 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004833 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004834 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4835 return;
4836 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004837 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004838 return;
4839 }
4840 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004842 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004845static const struct rpc_call_ops nfs4_renew_ops = {
4846 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004847 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004848};
4849
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004850static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
4852 struct rpc_message msg = {
4853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4854 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004855 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004857 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004859 if (renew_flags == 0)
4860 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004861 if (!atomic_inc_not_zero(&clp->cl_count))
4862 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004863 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004864 if (data == NULL)
4865 return -ENOMEM;
4866 data->client = clp;
4867 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004868 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004869 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870}
4871
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004872static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
4874 struct rpc_message msg = {
4875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4876 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004877 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 };
4879 unsigned long now = jiffies;
4880 int status;
4881
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004882 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 if (status < 0)
4884 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004885 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 return 0;
4887}
4888
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004889static inline int nfs4_server_supports_acls(struct nfs_server *server)
4890{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004891 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004892}
4893
Trond Myklebust21f498c2012-08-24 10:59:25 -04004894/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4895 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004896 * the stack.
4897 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004898#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004899
Neil Hormane9e3d722011-03-04 19:26:03 -05004900static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004901 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004902{
4903 struct page *newpage, **spages;
4904 int rc = 0;
4905 size_t len;
4906 spages = pages;
4907
4908 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004909 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004910 newpage = alloc_page(GFP_KERNEL);
4911
4912 if (newpage == NULL)
4913 goto unwind;
4914 memcpy(page_address(newpage), buf, len);
4915 buf += len;
4916 buflen -= len;
4917 *pages++ = newpage;
4918 rc++;
4919 } while (buflen != 0);
4920
4921 return rc;
4922
4923unwind:
4924 for(; rc > 0; rc--)
4925 __free_page(spages[rc-1]);
4926 return -ENOMEM;
4927}
4928
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004929struct nfs4_cached_acl {
4930 int cached;
4931 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004932 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004933};
4934
4935static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004936{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004937 struct nfs_inode *nfsi = NFS_I(inode);
4938
4939 spin_lock(&inode->i_lock);
4940 kfree(nfsi->nfs4_acl);
4941 nfsi->nfs4_acl = acl;
4942 spin_unlock(&inode->i_lock);
4943}
4944
4945static void nfs4_zap_acl_attr(struct inode *inode)
4946{
4947 nfs4_set_cached_acl(inode, NULL);
4948}
4949
4950static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4951{
4952 struct nfs_inode *nfsi = NFS_I(inode);
4953 struct nfs4_cached_acl *acl;
4954 int ret = -ENOENT;
4955
4956 spin_lock(&inode->i_lock);
4957 acl = nfsi->nfs4_acl;
4958 if (acl == NULL)
4959 goto out;
4960 if (buf == NULL) /* user is just asking for length */
4961 goto out_len;
4962 if (acl->cached == 0)
4963 goto out;
4964 ret = -ERANGE; /* see getxattr(2) man page */
4965 if (acl->len > buflen)
4966 goto out;
4967 memcpy(buf, acl->data, acl->len);
4968out_len:
4969 ret = acl->len;
4970out:
4971 spin_unlock(&inode->i_lock);
4972 return ret;
4973}
4974
Sachin Prabhu5794d212012-04-17 14:36:40 +01004975static 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 +00004976{
4977 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004978 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004979
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004980 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004981 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004982 if (acl == NULL)
4983 goto out;
4984 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004985 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004986 } else {
4987 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4988 if (acl == NULL)
4989 goto out;
4990 acl->cached = 0;
4991 }
4992 acl->len = acl_len;
4993out:
4994 nfs4_set_cached_acl(inode, acl);
4995}
4996
Andy Adamsonbf118a32011-12-07 11:55:27 -05004997/*
4998 * The getxattr API returns the required buffer length when called with a
4999 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5000 * the required buf. On a NULL buf, we send a page of data to the server
5001 * guessing that the ACL request can be serviced by a page. If so, we cache
5002 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5003 * the cache. If not so, we throw away the page, and cache the required
5004 * length. The next getxattr call will then produce another round trip to
5005 * the server, this time with the input buf of the required size.
5006 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005007static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005008{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005009 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005010 struct nfs_getaclargs args = {
5011 .fh = NFS_FH(inode),
5012 .acl_pages = pages,
5013 .acl_len = buflen,
5014 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005015 struct nfs_getaclres res = {
5016 .acl_len = buflen,
5017 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005018 struct rpc_message msg = {
5019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5020 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005021 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005022 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005023 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005024 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005025
Trond Myklebust21f498c2012-08-24 10:59:25 -04005026 if (npages > ARRAY_SIZE(pages))
5027 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005028
Andy Adamsonbf118a32011-12-07 11:55:27 -05005029 for (i = 0; i < npages; i++) {
5030 pages[i] = alloc_page(GFP_KERNEL);
5031 if (!pages[i])
5032 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005033 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005034
5035 /* for decoding across pages */
5036 res.acl_scratch = alloc_page(GFP_KERNEL);
5037 if (!res.acl_scratch)
5038 goto out_free;
5039
Andy Adamsonbf118a32011-12-07 11:55:27 -05005040 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005041
Peng Taode040be2012-01-10 22:42:47 +08005042 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005043 __func__, buf, buflen, npages, args.acl_len);
5044 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5045 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005046 if (ret)
5047 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005048
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005049 /* Handle the case where the passed-in buffer is too short */
5050 if (res.acl_flags & NFS4_ACL_TRUNC) {
5051 /* Did the user only issue a request for the acl length? */
5052 if (buf == NULL)
5053 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005054 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005055 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005056 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005057 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005058 if (buf) {
5059 if (res.acl_len > buflen) {
5060 ret = -ERANGE;
5061 goto out_free;
5062 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005063 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005064 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005065out_ok:
5066 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005067out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005068 for (i = 0; i < npages; i++)
5069 if (pages[i])
5070 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005071 if (res.acl_scratch)
5072 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005073 return ret;
5074}
5075
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005076static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5077{
5078 struct nfs4_exception exception = { };
5079 ssize_t ret;
5080 do {
5081 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005082 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005083 if (ret >= 0)
5084 break;
5085 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5086 } while (exception.retry);
5087 return ret;
5088}
5089
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005090static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5091{
5092 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005093 int ret;
5094
5095 if (!nfs4_server_supports_acls(server))
5096 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005097 ret = nfs_revalidate_inode(server, inode);
5098 if (ret < 0)
5099 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005100 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5101 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005102 ret = nfs4_read_cached_acl(inode, buf, buflen);
5103 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005104 /* -ENOENT is returned if there is no ACL or if there is an ACL
5105 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005106 return ret;
5107 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005108}
5109
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005110static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005111{
5112 struct nfs_server *server = NFS_SERVER(inode);
5113 struct page *pages[NFS4ACL_MAXPAGES];
5114 struct nfs_setaclargs arg = {
5115 .fh = NFS_FH(inode),
5116 .acl_pages = pages,
5117 .acl_len = buflen,
5118 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005119 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005120 struct rpc_message msg = {
5121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5122 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005123 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005124 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005125 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005126 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005127
5128 if (!nfs4_server_supports_acls(server))
5129 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005130 if (npages > ARRAY_SIZE(pages))
5131 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005132 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005133 if (i < 0)
5134 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005135 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005136 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005137
5138 /*
5139 * Free each page after tx, so the only ref left is
5140 * held by the network stack
5141 */
5142 for (; i > 0; i--)
5143 put_page(pages[i-1]);
5144
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005145 /*
5146 * Acl update can result in inode attribute update.
5147 * so mark the attribute cache invalid.
5148 */
5149 spin_lock(&inode->i_lock);
5150 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5151 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005152 nfs_access_zap_cache(inode);
5153 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005154 return ret;
5155}
5156
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005157static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5158{
5159 struct nfs4_exception exception = { };
5160 int err;
5161 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005162 err = __nfs4_proc_set_acl(inode, buf, buflen);
5163 trace_nfs4_set_acl(inode, err);
5164 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005165 &exception);
5166 } while (exception.retry);
5167 return err;
5168}
5169
David Quigleyaa9c2662013-05-22 12:50:44 -04005170#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5171static int _nfs4_get_security_label(struct inode *inode, void *buf,
5172 size_t buflen)
5173{
5174 struct nfs_server *server = NFS_SERVER(inode);
5175 struct nfs_fattr fattr;
5176 struct nfs4_label label = {0, 0, buflen, buf};
5177
5178 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005179 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005180 .fh = NFS_FH(inode),
5181 .bitmask = bitmask,
5182 };
5183 struct nfs4_getattr_res res = {
5184 .fattr = &fattr,
5185 .label = &label,
5186 .server = server,
5187 };
5188 struct rpc_message msg = {
5189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005190 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005191 .rpc_resp = &res,
5192 };
5193 int ret;
5194
5195 nfs_fattr_init(&fattr);
5196
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005197 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005198 if (ret)
5199 return ret;
5200 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5201 return -ENOENT;
5202 if (buflen < label.len)
5203 return -ERANGE;
5204 return 0;
5205}
5206
5207static int nfs4_get_security_label(struct inode *inode, void *buf,
5208 size_t buflen)
5209{
5210 struct nfs4_exception exception = { };
5211 int err;
5212
5213 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5214 return -EOPNOTSUPP;
5215
5216 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005217 err = _nfs4_get_security_label(inode, buf, buflen);
5218 trace_nfs4_get_security_label(inode, err);
5219 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005220 &exception);
5221 } while (exception.retry);
5222 return err;
5223}
5224
5225static int _nfs4_do_set_security_label(struct inode *inode,
5226 struct nfs4_label *ilabel,
5227 struct nfs_fattr *fattr,
5228 struct nfs4_label *olabel)
5229{
5230
5231 struct iattr sattr = {0};
5232 struct nfs_server *server = NFS_SERVER(inode);
5233 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005234 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005235 .fh = NFS_FH(inode),
5236 .iap = &sattr,
5237 .server = server,
5238 .bitmask = bitmask,
5239 .label = ilabel,
5240 };
5241 struct nfs_setattrres res = {
5242 .fattr = fattr,
5243 .label = olabel,
5244 .server = server,
5245 };
5246 struct rpc_message msg = {
5247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005248 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005249 .rpc_resp = &res,
5250 };
5251 int status;
5252
Jeff Layton12207f62013-11-01 10:49:32 -04005253 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005254
Jeff Layton12207f62013-11-01 10:49:32 -04005255 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005256 if (status)
5257 dprintk("%s failed: %d\n", __func__, status);
5258
5259 return status;
5260}
5261
5262static int nfs4_do_set_security_label(struct inode *inode,
5263 struct nfs4_label *ilabel,
5264 struct nfs_fattr *fattr,
5265 struct nfs4_label *olabel)
5266{
5267 struct nfs4_exception exception = { };
5268 int err;
5269
5270 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005271 err = _nfs4_do_set_security_label(inode, ilabel,
5272 fattr, olabel);
5273 trace_nfs4_set_security_label(inode, err);
5274 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005275 &exception);
5276 } while (exception.retry);
5277 return err;
5278}
5279
5280static int
Al Viro59301222016-05-27 10:19:30 -04005281nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005282{
5283 struct nfs4_label ilabel, *olabel = NULL;
5284 struct nfs_fattr fattr;
5285 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005286 int status;
5287
5288 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5289 return -EOPNOTSUPP;
5290
5291 nfs_fattr_init(&fattr);
5292
5293 ilabel.pi = 0;
5294 ilabel.lfs = 0;
5295 ilabel.label = (char *)buf;
5296 ilabel.len = buflen;
5297
5298 cred = rpc_lookup_cred();
5299 if (IS_ERR(cred))
5300 return PTR_ERR(cred);
5301
5302 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5303 if (IS_ERR(olabel)) {
5304 status = -PTR_ERR(olabel);
5305 goto out;
5306 }
5307
5308 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5309 if (status == 0)
5310 nfs_setsecurity(inode, &fattr, olabel);
5311
5312 nfs4_label_free(olabel);
5313out:
5314 put_rpccred(cred);
5315 return status;
5316}
5317#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5318
5319
Chuck Leverf0920752012-05-21 22:45:41 -04005320static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5321 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005322{
5323 __be32 verf[2];
5324
Chuck Lever2c820d92012-05-21 22:45:33 -04005325 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5326 /* An impossible timestamp guarantees this value
5327 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005328 verf[0] = cpu_to_be32(U32_MAX);
5329 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005330 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005331 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005332 u64 ns = ktime_to_ns(nn->boot_time);
5333
5334 verf[0] = cpu_to_be32(ns >> 32);
5335 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005336 }
Chuck Levercd937102012-03-02 17:14:31 -05005337 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5338}
5339
Jeff Laytona3192682015-06-09 19:43:59 -04005340static int
5341nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005342{
Jeff Laytona3192682015-06-09 19:43:59 -04005343 size_t len;
5344 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005345
Trond Myklebustceb3a162015-01-03 15:16:04 -05005346 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005347 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005348
Jeff Laytona3192682015-06-09 19:43:59 -04005349 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005350 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005351 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5352 1 +
5353 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5354 1;
5355 rcu_read_unlock();
5356
5357 if (len > NFS4_OPAQUE_LIMIT + 1)
5358 return -EINVAL;
5359
5360 /*
5361 * Since this string is allocated at mount time, and held until the
5362 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5363 * about a memory-reclaim deadlock.
5364 */
5365 str = kmalloc(len, GFP_KERNEL);
5366 if (!str)
5367 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005368
Chuck Levere984a552012-09-14 17:24:21 -04005369 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005370 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005371 clp->cl_ipaddr,
5372 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5373 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005374 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005375
Jeff Laytona3192682015-06-09 19:43:59 -04005376 clp->cl_owner_id = str;
5377 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005378}
5379
Jeff Layton873e3852015-06-09 19:44:00 -04005380static int
5381nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005382{
Jeff Layton873e3852015-06-09 19:44:00 -04005383 size_t len;
5384 char *str;
5385
5386 len = 10 + 10 + 1 + 10 + 1 +
5387 strlen(nfs4_client_id_uniquifier) + 1 +
5388 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5389
5390 if (len > NFS4_OPAQUE_LIMIT + 1)
5391 return -EINVAL;
5392
5393 /*
5394 * Since this string is allocated at mount time, and held until the
5395 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5396 * about a memory-reclaim deadlock.
5397 */
5398 str = kmalloc(len, GFP_KERNEL);
5399 if (!str)
5400 return -ENOMEM;
5401
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005402 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005403 clp->rpc_ops->version, clp->cl_minorversion,
5404 nfs4_client_id_uniquifier,
5405 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005406 clp->cl_owner_id = str;
5407 return 0;
5408}
5409
5410static int
5411nfs4_init_uniform_client_string(struct nfs_client *clp)
5412{
Jeff Layton873e3852015-06-09 19:44:00 -04005413 size_t len;
5414 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005415
5416 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005417 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005418
5419 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005420 return nfs4_init_uniquifier_client_string(clp);
5421
5422 len = 10 + 10 + 1 + 10 + 1 +
5423 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5424
5425 if (len > NFS4_OPAQUE_LIMIT + 1)
5426 return -EINVAL;
5427
5428 /*
5429 * Since this string is allocated at mount time, and held until the
5430 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5431 * about a memory-reclaim deadlock.
5432 */
5433 str = kmalloc(len, GFP_KERNEL);
5434 if (!str)
5435 return -ENOMEM;
5436
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005437 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005438 clp->rpc_ops->version, clp->cl_minorversion,
5439 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005440 clp->cl_owner_id = str;
5441 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005442}
5443
Chuck Lever706cb8d2014-03-12 12:51:47 -04005444/*
5445 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5446 * services. Advertise one based on the address family of the
5447 * clientaddr.
5448 */
5449static unsigned int
5450nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5451{
5452 if (strchr(clp->cl_ipaddr, ':') != NULL)
5453 return scnprintf(buf, len, "tcp6");
5454 else
5455 return scnprintf(buf, len, "tcp");
5456}
5457
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005458static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5459{
5460 struct nfs4_setclientid *sc = calldata;
5461
5462 if (task->tk_status == 0)
5463 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5464}
5465
5466static const struct rpc_call_ops nfs4_setclientid_ops = {
5467 .rpc_call_done = nfs4_setclientid_done,
5468};
5469
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005470/**
5471 * nfs4_proc_setclientid - Negotiate client ID
5472 * @clp: state data structure
5473 * @program: RPC program for NFSv4 callback service
5474 * @port: IP port number for NFS4 callback service
5475 * @cred: RPC credential to use for this call
5476 * @res: where to place the result
5477 *
5478 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5479 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005480int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5481 unsigned short port, struct rpc_cred *cred,
5482 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
5484 nfs4_verifier sc_verifier;
5485 struct nfs4_setclientid setclientid = {
5486 .sc_verifier = &sc_verifier,
5487 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005488 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 };
5490 struct rpc_message msg = {
5491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5492 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005493 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005494 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005496 struct rpc_task *task;
5497 struct rpc_task_setup task_setup_data = {
5498 .rpc_client = clp->cl_rpcclient,
5499 .rpc_message = &msg,
5500 .callback_ops = &nfs4_setclientid_ops,
5501 .callback_data = &setclientid,
5502 .flags = RPC_TASK_TIMEOUT,
5503 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005504 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
Chuck Leverde734832012-07-11 16:30:50 -04005506 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005507 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005508
5509 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5510 status = nfs4_init_uniform_client_string(clp);
5511 else
Jeff Laytona3192682015-06-09 19:43:59 -04005512 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005513
5514 if (status)
5515 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005516
Chuck Leverde734832012-07-11 16:30:50 -04005517 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005518 setclientid.sc_netid_len =
5519 nfs4_init_callback_netid(clp,
5520 setclientid.sc_netid,
5521 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005522 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005523 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 clp->cl_ipaddr, port >> 8, port & 255);
5525
Jeff Layton3a6bb732015-06-09 19:43:57 -04005526 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005527 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005528 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005529 task = rpc_run_task(&task_setup_data);
5530 if (IS_ERR(task)) {
5531 status = PTR_ERR(task);
5532 goto out;
5533 }
5534 status = task->tk_status;
5535 if (setclientid.sc_cred) {
5536 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5537 put_rpccred(setclientid.sc_cred);
5538 }
5539 rpc_put_task(task);
5540out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005541 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005542 dprintk("NFS reply setclientid: %d\n", status);
5543 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544}
5545
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005546/**
5547 * nfs4_proc_setclientid_confirm - Confirm client ID
5548 * @clp: state data structure
5549 * @res: result of a previous SETCLIENTID
5550 * @cred: RPC credential to use for this call
5551 *
5552 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5553 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005554int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005555 struct nfs4_setclientid_res *arg,
5556 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 struct rpc_message msg = {
5559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005560 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005561 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 int status;
5564
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005565 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5566 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5567 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005568 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005569 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005570 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 return status;
5572}
5573
Trond Myklebustfe650402006-01-03 09:55:18 +01005574struct nfs4_delegreturndata {
5575 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005576 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005577 struct nfs_fh fh;
5578 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005579 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005580 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005581 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005582 struct inode *inode;
5583 bool roc;
5584 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005585};
5586
Trond Myklebustfe650402006-01-03 09:55:18 +01005587static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5588{
5589 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005590
Trond Myklebust14516c32010-07-31 14:29:06 -04005591 if (!nfs4_sequence_done(task, &data->res.seq_res))
5592 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005593
Trond Myklebustca8acf82013-08-13 10:36:56 -04005594 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005595 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005596 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005597 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005598 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005599 case -NFS4ERR_ADMIN_REVOKED:
5600 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005601 case -NFS4ERR_EXPIRED:
5602 nfs4_free_revoked_stateid(data->res.server,
5603 data->args.stateid,
5604 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005605 case -NFS4ERR_BAD_STATEID:
5606 case -NFS4ERR_OLD_STATEID:
5607 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005608 task->tk_status = 0;
5609 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005610 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005611 if (nfs4_async_handle_error(task, data->res.server,
5612 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005613 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005614 return;
5615 }
5616 }
5617 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005618 if (data->roc && data->rpc_status == 0)
5619 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005620}
5621
5622static void nfs4_delegreturn_release(void *calldata)
5623{
Peng Tao039b7562014-07-03 13:05:02 +08005624 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005625 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005626
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005627 if (inode) {
5628 if (data->roc)
5629 pnfs_roc_release(inode);
5630 nfs_iput_and_deactive(inode);
5631 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005632 kfree(calldata);
5633}
5634
Andy Adamson938e1012009-04-01 09:22:28 -04005635static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5636{
5637 struct nfs4_delegreturndata *d_data;
5638
5639 d_data = (struct nfs4_delegreturndata *)data;
5640
Peng Tao500d7012015-09-22 11:35:22 +08005641 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5642 return;
5643
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005644 if (d_data->roc)
5645 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005646
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005647 nfs4_setup_sequence(d_data->res.server,
5648 &d_data->args.seq_args,
5649 &d_data->res.seq_res,
5650 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005651}
Andy Adamson938e1012009-04-01 09:22:28 -04005652
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005653static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005654 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005655 .rpc_call_done = nfs4_delegreturn_done,
5656 .rpc_release = nfs4_delegreturn_release,
5657};
5658
Trond Myklebuste6f81072008-01-24 18:14:34 -05005659static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005660{
5661 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005662 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005663 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005664 struct rpc_message msg = {
5665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5666 .rpc_cred = cred,
5667 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005668 struct rpc_task_setup task_setup_data = {
5669 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005670 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005671 .callback_ops = &nfs4_delegreturn_ops,
5672 .flags = RPC_TASK_ASYNC,
5673 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005674 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005675
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005676 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005677 if (data == NULL)
5678 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005679 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005680
5681 nfs4_state_protect(server->nfs_client,
5682 NFS_SP4_MACH_CRED_CLEANUP,
5683 &task_setup_data.rpc_client, &msg);
5684
Trond Myklebustfe650402006-01-03 09:55:18 +01005685 data->args.fhandle = &data->fh;
5686 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005687 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005688 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005689 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005690 data->res.fattr = &data->fattr;
5691 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005692 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005693 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005694 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005695 data->inode = nfs_igrab_and_active(inode);
5696 if (data->inode)
5697 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005698
Trond Myklebustc970aa82007-07-14 15:39:59 -04005699 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005700 msg.rpc_argp = &data->args;
5701 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005702 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005703 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005704 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005705 if (!issync)
5706 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005707 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005708 if (status != 0)
5709 goto out;
5710 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005711 if (status == 0)
5712 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5713 else
5714 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005715out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005716 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005717 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718}
5719
Trond Myklebuste6f81072008-01-24 18:14:34 -05005720int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721{
5722 struct nfs_server *server = NFS_SERVER(inode);
5723 struct nfs4_exception exception = { };
5724 int err;
5725 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005726 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005727 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 switch (err) {
5729 case -NFS4ERR_STALE_STATEID:
5730 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 case 0:
5732 return 0;
5733 }
5734 err = nfs4_handle_exception(server, err, &exception);
5735 } while (exception.retry);
5736 return err;
5737}
5738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5740{
5741 struct inode *inode = state->inode;
5742 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005743 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005744 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005746 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005748 struct nfs_lockt_res res = {
5749 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 };
5751 struct rpc_message msg = {
5752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5753 .rpc_argp = &arg,
5754 .rpc_resp = &res,
5755 .rpc_cred = state->owner->so_cred,
5756 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 struct nfs4_lock_state *lsp;
5758 int status;
5759
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005760 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005761 status = nfs4_set_lock_state(state, request);
5762 if (status != 0)
5763 goto out;
5764 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005765 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005766 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005767 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005768 switch (status) {
5769 case 0:
5770 request->fl_type = F_UNLCK;
5771 break;
5772 case -NFS4ERR_DENIED:
5773 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005775 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005776 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005777out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 return status;
5779}
5780
5781static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5782{
5783 struct nfs4_exception exception = { };
5784 int err;
5785
5786 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005787 err = _nfs4_proc_getlk(state, cmd, request);
5788 trace_nfs4_get_lock(request, state, cmd, err);
5789 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 &exception);
5791 } while (exception.retry);
5792 return err;
5793}
5794
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005795struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005796 struct nfs_locku_args arg;
5797 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005798 struct nfs4_lock_state *lsp;
5799 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005800 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005801 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005802 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005803};
5804
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005805static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5806 struct nfs_open_context *ctx,
5807 struct nfs4_lock_state *lsp,
5808 struct nfs_seqid *seqid)
5809{
5810 struct nfs4_unlockdata *p;
5811 struct inode *inode = lsp->ls_state->inode;
5812
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005813 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005814 if (p == NULL)
5815 return NULL;
5816 p->arg.fh = NFS_FH(inode);
5817 p->arg.fl = &p->fl;
5818 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005819 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005820 p->lsp = lsp;
5821 atomic_inc(&lsp->ls_count);
5822 /* Ensure we don't close file until we're done freeing locks! */
5823 p->ctx = get_nfs_open_context(ctx);
5824 memcpy(&p->fl, fl, sizeof(p->fl));
5825 p->server = NFS_SERVER(inode);
5826 return p;
5827}
5828
Trond Myklebust06f814a2006-01-03 09:55:07 +01005829static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005830{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005831 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005832 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005833 nfs4_put_lock_state(calldata->lsp);
5834 put_nfs_open_context(calldata->ctx);
5835 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005836}
5837
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005838static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005839{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005840 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005841
Trond Myklebust14516c32010-07-31 14:29:06 -04005842 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5843 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005844 switch (task->tk_status) {
5845 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005846 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005847 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005848 if (nfs4_update_lock_stateid(calldata->lsp,
5849 &calldata->res.stateid))
5850 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005851 case -NFS4ERR_ADMIN_REVOKED:
5852 case -NFS4ERR_EXPIRED:
5853 nfs4_free_revoked_stateid(calldata->server,
5854 &calldata->arg.stateid,
5855 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005856 case -NFS4ERR_BAD_STATEID:
5857 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005858 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005859 if (!nfs4_stateid_match(&calldata->arg.stateid,
5860 &calldata->lsp->ls_stateid))
5861 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005862 break;
5863 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005864 if (nfs4_async_handle_error(task, calldata->server,
5865 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005866 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005867 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005868 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005869}
5870
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005871static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005872{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005873 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005875 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005876 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005877 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005878 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005879 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005880 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005881 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005882 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005883 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005884 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005885 &calldata->res.seq_res,
5886 task) != 0)
5887 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005888 return;
5889out_no_action:
5890 task->tk_action = NULL;
5891out_wait:
5892 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893}
5894
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005895static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005896 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005897 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005898 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005899};
5900
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005901static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5902 struct nfs_open_context *ctx,
5903 struct nfs4_lock_state *lsp,
5904 struct nfs_seqid *seqid)
5905{
5906 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005907 struct rpc_message msg = {
5908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5909 .rpc_cred = ctx->cred,
5910 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005911 struct rpc_task_setup task_setup_data = {
5912 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005913 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005914 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005915 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005916 .flags = RPC_TASK_ASYNC,
5917 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005918
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005919 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5920 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5921
Frank Filz137d6ac2007-07-09 15:32:29 -07005922 /* Ensure this is an unlock - when canceling a lock, the
5923 * canceled lock is passed in, and it won't be an unlock.
5924 */
5925 fl->fl_type = F_UNLCK;
5926
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005927 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5928 if (data == NULL) {
5929 nfs_free_seqid(seqid);
5930 return ERR_PTR(-ENOMEM);
5931 }
5932
Chuck Levera9c92d62013-08-09 12:48:18 -04005933 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005934 msg.rpc_argp = &data->arg;
5935 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005936 task_setup_data.callback_data = data;
5937 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005938}
5939
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5941{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005942 struct inode *inode = state->inode;
5943 struct nfs4_state_owner *sp = state->owner;
5944 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005945 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005946 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005947 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005948 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005949 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005950 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Trond Myklebust9b073572006-06-29 16:38:34 -04005952 status = nfs4_set_lock_state(state, request);
5953 /* Unlock _before_ we do the RPC call */
5954 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005955 /* Exclude nfs_delegation_claim_locks() */
5956 mutex_lock(&sp->so_delegreturn_mutex);
5957 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005958 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005959 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005960 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005961 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005962 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005963 }
5964 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005965 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005966 if (status != 0)
5967 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005968 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005969 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005970 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5971 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005972 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5973 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005974 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005975 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005976 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005977 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005978 status = PTR_ERR(task);
5979 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005980 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005981 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005982 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005983out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005984 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005985 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005986 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987}
5988
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005989struct nfs4_lockdata {
5990 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005991 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005992 struct nfs4_lock_state *lsp;
5993 struct nfs_open_context *ctx;
5994 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005995 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005996 int rpc_status;
5997 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005998 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999};
6000
6001static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006002 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6003 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004{
6005 struct nfs4_lockdata *p;
6006 struct inode *inode = lsp->ls_state->inode;
6007 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006008 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006009
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006010 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006011 if (p == NULL)
6012 return NULL;
6013
6014 p->arg.fh = NFS_FH(inode);
6015 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006016 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006017 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006018 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006019 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6020 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006021 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006022 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006023 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006024 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006025 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006026 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006027 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006028 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006029 atomic_inc(&lsp->ls_count);
6030 p->ctx = get_nfs_open_context(ctx);
6031 memcpy(&p->fl, fl, sizeof(p->fl));
6032 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006033out_free_seqid:
6034 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006035out_free:
6036 kfree(p);
6037 return NULL;
6038}
6039
6040static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6041{
6042 struct nfs4_lockdata *data = calldata;
6043 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
Harvey Harrison3110ff82008-05-02 13:42:44 -07006045 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006046 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006047 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006048 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006049 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006050 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006051 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006052 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006053 nfs4_stateid_copy(&data->arg.open_stateid,
6054 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006055 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006056 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006057 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006058 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006059 nfs4_stateid_copy(&data->arg.lock_stateid,
6060 &data->lsp->ls_stateid);
6061 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006062 if (!nfs4_valid_open_stateid(state)) {
6063 data->rpc_status = -EBADF;
6064 task->tk_action = NULL;
6065 goto out_release_open_seqid;
6066 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006067 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006068 if (nfs4_setup_sequence(data->server,
6069 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006070 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006071 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006072 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006073out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006074 nfs_release_seqid(data->arg.open_seqid);
6075out_release_lock_seqid:
6076 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006077out_wait:
6078 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006079 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006080}
6081
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006082static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6083{
6084 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006085 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006086
Harvey Harrison3110ff82008-05-02 13:42:44 -07006087 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006088
Trond Myklebust14516c32010-07-31 14:29:06 -04006089 if (!nfs4_sequence_done(task, &data->res.seq_res))
6090 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006091
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006092 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006093 switch (task->tk_status) {
6094 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006095 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006096 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006097 if (data->arg.new_lock) {
6098 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006099 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006100 rpc_restart_call_prepare(task);
6101 break;
6102 }
6103 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006104 if (data->arg.new_lock_owner != 0) {
6105 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6106 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6107 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6108 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6109 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006110 break;
6111 case -NFS4ERR_BAD_STATEID:
6112 case -NFS4ERR_OLD_STATEID:
6113 case -NFS4ERR_STALE_STATEID:
6114 case -NFS4ERR_EXPIRED:
6115 if (data->arg.new_lock_owner != 0) {
6116 if (!nfs4_stateid_match(&data->arg.open_stateid,
6117 &lsp->ls_state->open_stateid))
6118 rpc_restart_call_prepare(task);
6119 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6120 &lsp->ls_stateid))
6121 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006122 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006123 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006124}
6125
6126static void nfs4_lock_release(void *calldata)
6127{
6128 struct nfs4_lockdata *data = calldata;
6129
Harvey Harrison3110ff82008-05-02 13:42:44 -07006130 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006131 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006132 if (data->cancelled != 0) {
6133 struct rpc_task *task;
6134 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6135 data->arg.lock_seqid);
6136 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006137 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006138 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006139 } else
6140 nfs_free_seqid(data->arg.lock_seqid);
6141 nfs4_put_lock_state(data->lsp);
6142 put_nfs_open_context(data->ctx);
6143 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006144 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006145}
6146
6147static const struct rpc_call_ops nfs4_lock_ops = {
6148 .rpc_call_prepare = nfs4_lock_prepare,
6149 .rpc_call_done = nfs4_lock_done,
6150 .rpc_release = nfs4_lock_release,
6151};
6152
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006153static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6154{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006155 switch (error) {
6156 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006157 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006158 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006159 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006160 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006161 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006162 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006163 break;
6164 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006165 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006166 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006167 };
6168}
6169
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006170static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006171{
6172 struct nfs4_lockdata *data;
6173 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006174 struct rpc_message msg = {
6175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6176 .rpc_cred = state->owner->so_cred,
6177 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006178 struct rpc_task_setup task_setup_data = {
6179 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006180 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006181 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006182 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006183 .flags = RPC_TASK_ASYNC,
6184 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185 int ret;
6186
Harvey Harrison3110ff82008-05-02 13:42:44 -07006187 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006188 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006189 fl->fl_u.nfs4_fl.owner,
6190 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006191 if (data == NULL)
6192 return -ENOMEM;
6193 if (IS_SETLKW(cmd))
6194 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006195 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006196 msg.rpc_argp = &data->arg;
6197 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006198 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006199 if (recovery_type > NFS_LOCK_NEW) {
6200 if (recovery_type == NFS_LOCK_RECLAIM)
6201 data->arg.reclaim = NFS_LOCK_RECLAIM;
6202 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006203 } else
6204 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006205 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006206 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006207 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006208 ret = nfs4_wait_for_completion_rpc_task(task);
6209 if (ret == 0) {
6210 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006211 if (ret)
6212 nfs4_handle_setlk_error(data->server, data->lsp,
6213 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006214 } else
6215 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006216 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006217 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006218 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220}
6221
6222static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6223{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006224 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006225 struct nfs4_exception exception = {
6226 .inode = state->inode,
6227 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006228 int err;
6229
6230 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006231 /* Cache the lock if possible... */
6232 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6233 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006234 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006235 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006236 break;
6237 nfs4_handle_exception(server, err, &exception);
6238 } while (exception.retry);
6239 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240}
6241
6242static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6243{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006244 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006245 struct nfs4_exception exception = {
6246 .inode = state->inode,
6247 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006248 int err;
6249
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006250 err = nfs4_set_lock_state(state, request);
6251 if (err != 0)
6252 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006253 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006254 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6255 return 0;
6256 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006257 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006258 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6259 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006260 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006261 switch (err) {
6262 default:
6263 goto out;
6264 case -NFS4ERR_GRACE:
6265 case -NFS4ERR_DELAY:
6266 nfs4_handle_exception(server, err, &exception);
6267 err = 0;
6268 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006269 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006270out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272}
6273
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006274#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006275static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6276{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006277 struct nfs4_lock_state *lsp;
6278 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006279
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006280 status = nfs4_set_lock_state(state, request);
6281 if (status != 0)
6282 return status;
6283 lsp = request->fl_u.nfs4_fl.owner;
6284 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6285 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6286 return 0;
6287 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006288 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006289}
6290#endif
6291
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6293{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006294 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006295 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006296 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006297 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Trond Myklebust01c3b862006-06-29 16:38:39 -04006299 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006300 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006301 if (status < 0)
6302 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006303 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006304 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006305 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006306 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006307 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006308 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006309 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006310 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006311 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006312 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006313 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006314 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006315 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006316 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006317out:
6318 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 return status;
6320}
6321
6322static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6323{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006324 struct nfs4_exception exception = {
6325 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006326 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006327 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 int err;
6329
6330 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006331 err = _nfs4_proc_setlk(state, cmd, request);
6332 if (err == -NFS4ERR_DENIED)
6333 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006335 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 } while (exception.retry);
6337 return err;
6338}
6339
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006340#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6341#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6342
6343static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006344nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6345 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006346{
6347 int status = -ERESTARTSYS;
6348 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6349
6350 while(!signalled()) {
6351 status = nfs4_proc_setlk(state, cmd, request);
6352 if ((status != -EAGAIN) || IS_SETLK(cmd))
6353 break;
6354 freezable_schedule_timeout_interruptible(timeout);
6355 timeout *= 2;
6356 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6357 status = -ERESTARTSYS;
6358 }
6359 return status;
6360}
6361
Jeff Laytona1d617d82016-09-17 18:17:39 -04006362#ifdef CONFIG_NFS_V4_1
6363struct nfs4_lock_waiter {
6364 struct task_struct *task;
6365 struct inode *inode;
6366 struct nfs_lowner *owner;
6367 bool notified;
6368};
6369
6370static int
6371nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6372{
6373 int ret;
6374 struct cb_notify_lock_args *cbnl = key;
6375 struct nfs4_lock_waiter *waiter = wait->private;
6376 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6377 *wowner = waiter->owner;
6378
6379 /* Only wake if the callback was for the same owner */
6380 if (lowner->clientid != wowner->clientid ||
6381 lowner->id != wowner->id ||
6382 lowner->s_dev != wowner->s_dev)
6383 return 0;
6384
6385 /* Make sure it's for the right inode */
6386 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6387 return 0;
6388
6389 waiter->notified = true;
6390
6391 /* override "private" so we can use default_wake_function */
6392 wait->private = waiter->task;
6393 ret = autoremove_wake_function(wait, mode, flags, key);
6394 wait->private = waiter;
6395 return ret;
6396}
6397
6398static int
6399nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6400{
6401 int status = -ERESTARTSYS;
6402 unsigned long flags;
6403 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6404 struct nfs_server *server = NFS_SERVER(state->inode);
6405 struct nfs_client *clp = server->nfs_client;
6406 wait_queue_head_t *q = &clp->cl_lock_waitq;
6407 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6408 .id = lsp->ls_seqid.owner_id,
6409 .s_dev = server->s_dev };
6410 struct nfs4_lock_waiter waiter = { .task = current,
6411 .inode = state->inode,
6412 .owner = &owner,
6413 .notified = false };
6414 wait_queue_t wait;
6415
6416 /* Don't bother with waitqueue if we don't expect a callback */
6417 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6418 return nfs4_retry_setlk_simple(state, cmd, request);
6419
6420 init_wait(&wait);
6421 wait.private = &waiter;
6422 wait.func = nfs4_wake_lock_waiter;
6423 add_wait_queue(q, &wait);
6424
6425 while(!signalled()) {
6426 status = nfs4_proc_setlk(state, cmd, request);
6427 if ((status != -EAGAIN) || IS_SETLK(cmd))
6428 break;
6429
6430 status = -ERESTARTSYS;
6431 spin_lock_irqsave(&q->lock, flags);
6432 if (waiter.notified) {
6433 spin_unlock_irqrestore(&q->lock, flags);
6434 continue;
6435 }
6436 set_current_state(TASK_INTERRUPTIBLE);
6437 spin_unlock_irqrestore(&q->lock, flags);
6438
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006439 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006440 }
6441
6442 finish_wait(q, &wait);
6443 return status;
6444}
6445#else /* !CONFIG_NFS_V4_1 */
6446static inline int
6447nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6448{
6449 return nfs4_retry_setlk_simple(state, cmd, request);
6450}
6451#endif
6452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453static int
6454nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6455{
6456 struct nfs_open_context *ctx;
6457 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 int status;
6459
6460 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006461 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 state = ctx->state;
6463
6464 if (request->fl_start < 0 || request->fl_end < 0)
6465 return -EINVAL;
6466
Trond Myklebustd9531262009-07-21 19:22:38 -04006467 if (IS_GETLK(cmd)) {
6468 if (state != NULL)
6469 return nfs4_proc_getlk(state, F_GETLK, request);
6470 return 0;
6471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
6473 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6474 return -EINVAL;
6475
Trond Myklebustd9531262009-07-21 19:22:38 -04006476 if (request->fl_type == F_UNLCK) {
6477 if (state != NULL)
6478 return nfs4_proc_unlck(state, cmd, request);
6479 return 0;
6480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481
Trond Myklebustd9531262009-07-21 19:22:38 -04006482 if (state == NULL)
6483 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006484
6485 if ((request->fl_flags & FL_POSIX) &&
6486 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6487 return -ENOLCK;
6488
Trond Myklebust55725512012-04-18 12:48:35 -04006489 /*
6490 * Don't rely on the VFS having checked the file open mode,
6491 * since it won't do this for flock() locks.
6492 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006493 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006494 case F_RDLCK:
6495 if (!(filp->f_mode & FMODE_READ))
6496 return -EBADF;
6497 break;
6498 case F_WRLCK:
6499 if (!(filp->f_mode & FMODE_WRITE))
6500 return -EBADF;
6501 }
6502
Jeff Layton1ea67db2016-09-17 18:17:37 -04006503 status = nfs4_set_lock_state(state, request);
6504 if (status != 0)
6505 return status;
6506
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006507 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508}
6509
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006510int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006511{
6512 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006513 int err;
6514
6515 err = nfs4_set_lock_state(state, fl);
6516 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006517 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006518 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrown70f254a2017-12-13 09:57:09 +11006519 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006520}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006521
Trond Myklebustcf470c32012-03-07 13:49:12 -05006522struct nfs_release_lockowner_data {
6523 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006524 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006525 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006526 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006527 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006528};
6529
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006530static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6531{
6532 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006533 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006534 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6535 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006536 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006537 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006538}
6539
6540static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6541{
6542 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006543 struct nfs_server *server = data->server;
6544
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006545 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006546
6547 switch (task->tk_status) {
6548 case 0:
6549 renew_lease(server, data->timestamp);
6550 break;
6551 case -NFS4ERR_STALE_CLIENTID:
6552 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006553 nfs4_schedule_lease_recovery(server->nfs_client);
6554 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006555 case -NFS4ERR_LEASE_MOVED:
6556 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006557 if (nfs4_async_handle_error(task, server,
6558 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006559 rpc_restart_call_prepare(task);
6560 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006561}
6562
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006563static void nfs4_release_lockowner_release(void *calldata)
6564{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006565 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006566 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006567 kfree(calldata);
6568}
6569
Trond Myklebust17280172012-03-11 13:11:00 -04006570static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006571 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6572 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006573 .rpc_release = nfs4_release_lockowner_release,
6574};
6575
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006576static void
6577nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006578{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006579 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006580 struct rpc_message msg = {
6581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6582 };
6583
6584 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006585 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006586
Trond Myklebustcf470c32012-03-07 13:49:12 -05006587 data = kmalloc(sizeof(*data), GFP_NOFS);
6588 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006589 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006590 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006591 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006592 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6593 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6594 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006595
Trond Myklebustcf470c32012-03-07 13:49:12 -05006596 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006597 msg.rpc_resp = &data->res;
6598 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006599 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006600}
6601
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006602#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6603
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006604static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006605 struct dentry *unused, struct inode *inode,
6606 const char *key, const void *buf,
6607 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006608{
Al Viro59301222016-05-27 10:19:30 -04006609 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006610}
6611
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006612static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006613 struct dentry *unused, struct inode *inode,
6614 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006615{
Al Virob2968212016-04-10 20:48:24 -04006616 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006617}
6618
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006619static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006620{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006621 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006622}
6623
David Quigleyc9bccef2013-05-22 12:50:45 -04006624#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006625
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006626static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006627 struct dentry *unused, struct inode *inode,
6628 const char *key, const void *buf,
6629 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006630{
6631 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006632 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006633
6634 return -EOPNOTSUPP;
6635}
6636
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006637static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006638 struct dentry *unused, struct inode *inode,
6639 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006640{
6641 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006642 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006643 return -EOPNOTSUPP;
6644}
6645
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006646static ssize_t
6647nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006648{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006649 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006650
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006651 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6652 len = security_inode_listsecurity(inode, list, list_len);
6653 if (list_len && len > list_len)
6654 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006655 }
6656 return len;
6657}
6658
6659static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6660 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006661 .get = nfs4_xattr_get_nfs4_label,
6662 .set = nfs4_xattr_set_nfs4_label,
6663};
David Quigleyc9bccef2013-05-22 12:50:45 -04006664
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006665#else
6666
6667static ssize_t
6668nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6669{
6670 return 0;
6671}
6672
6673#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006674
Andy Adamson533eb462011-06-13 18:25:56 -04006675/*
6676 * nfs_fhget will use either the mounted_on_fileid or the fileid
6677 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006678static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6679{
Andy Adamson533eb462011-06-13 18:25:56 -04006680 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6681 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6682 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006683 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006684 return;
6685
6686 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006687 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006688 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6689 fattr->nlink = 2;
6690}
6691
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006692static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6693 const struct qstr *name,
6694 struct nfs4_fs_locations *fs_locations,
6695 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006696{
6697 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006698 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006699 struct nfs4_fs_locations_arg args = {
6700 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006701 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006702 .page = page,
6703 .bitmask = bitmask,
6704 };
Benny Halevy22958462009-04-01 09:22:02 -04006705 struct nfs4_fs_locations_res res = {
6706 .fs_locations = fs_locations,
6707 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006708 struct rpc_message msg = {
6709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6710 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006711 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006712 };
6713 int status;
6714
Harvey Harrison3110ff82008-05-02 13:42:44 -07006715 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006716
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006717 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6718 bitmask[1] = nfs4_fattr_bitmap[1];
6719
Andy Adamson533eb462011-06-13 18:25:56 -04006720 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6721 * is not supported */
6722 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006723 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006724 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006725 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006726
Trond Myklebustc228fd32007-01-13 02:28:11 -05006727 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006728 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006729 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006730 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006731 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006732 return status;
6733}
6734
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006735int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6736 const struct qstr *name,
6737 struct nfs4_fs_locations *fs_locations,
6738 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006739{
6740 struct nfs4_exception exception = { };
6741 int err;
6742 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006743 err = _nfs4_proc_fs_locations(client, dir, name,
6744 fs_locations, page);
6745 trace_nfs4_get_fs_locations(dir, name, err);
6746 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006747 &exception);
6748 } while (exception.retry);
6749 return err;
6750}
6751
Chuck Leverb03d7352013-10-17 14:12:50 -04006752/*
6753 * This operation also signals the server that this client is
6754 * performing migration recovery. The server can stop returning
6755 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6756 * appended to this compound to identify the client ID which is
6757 * performing recovery.
6758 */
6759static int _nfs40_proc_get_locations(struct inode *inode,
6760 struct nfs4_fs_locations *locations,
6761 struct page *page, struct rpc_cred *cred)
6762{
6763 struct nfs_server *server = NFS_SERVER(inode);
6764 struct rpc_clnt *clnt = server->client;
6765 u32 bitmask[2] = {
6766 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6767 };
6768 struct nfs4_fs_locations_arg args = {
6769 .clientid = server->nfs_client->cl_clientid,
6770 .fh = NFS_FH(inode),
6771 .page = page,
6772 .bitmask = bitmask,
6773 .migration = 1, /* skip LOOKUP */
6774 .renew = 1, /* append RENEW */
6775 };
6776 struct nfs4_fs_locations_res res = {
6777 .fs_locations = locations,
6778 .migration = 1,
6779 .renew = 1,
6780 };
6781 struct rpc_message msg = {
6782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6783 .rpc_argp = &args,
6784 .rpc_resp = &res,
6785 .rpc_cred = cred,
6786 };
6787 unsigned long now = jiffies;
6788 int status;
6789
6790 nfs_fattr_init(&locations->fattr);
6791 locations->server = server;
6792 locations->nlocations = 0;
6793
6794 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6795 nfs4_set_sequence_privileged(&args.seq_args);
6796 status = nfs4_call_sync_sequence(clnt, server, &msg,
6797 &args.seq_args, &res.seq_res);
6798 if (status)
6799 return status;
6800
6801 renew_lease(server, now);
6802 return 0;
6803}
6804
6805#ifdef CONFIG_NFS_V4_1
6806
6807/*
6808 * This operation also signals the server that this client is
6809 * performing migration recovery. The server can stop asserting
6810 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6811 * performing this operation is identified in the SEQUENCE
6812 * operation in this compound.
6813 *
6814 * When the client supports GETATTR(fs_locations_info), it can
6815 * be plumbed in here.
6816 */
6817static int _nfs41_proc_get_locations(struct inode *inode,
6818 struct nfs4_fs_locations *locations,
6819 struct page *page, struct rpc_cred *cred)
6820{
6821 struct nfs_server *server = NFS_SERVER(inode);
6822 struct rpc_clnt *clnt = server->client;
6823 u32 bitmask[2] = {
6824 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6825 };
6826 struct nfs4_fs_locations_arg args = {
6827 .fh = NFS_FH(inode),
6828 .page = page,
6829 .bitmask = bitmask,
6830 .migration = 1, /* skip LOOKUP */
6831 };
6832 struct nfs4_fs_locations_res res = {
6833 .fs_locations = locations,
6834 .migration = 1,
6835 };
6836 struct rpc_message msg = {
6837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6838 .rpc_argp = &args,
6839 .rpc_resp = &res,
6840 .rpc_cred = cred,
6841 };
6842 int status;
6843
6844 nfs_fattr_init(&locations->fattr);
6845 locations->server = server;
6846 locations->nlocations = 0;
6847
6848 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6849 nfs4_set_sequence_privileged(&args.seq_args);
6850 status = nfs4_call_sync_sequence(clnt, server, &msg,
6851 &args.seq_args, &res.seq_res);
6852 if (status == NFS4_OK &&
6853 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6854 status = -NFS4ERR_LEASE_MOVED;
6855 return status;
6856}
6857
6858#endif /* CONFIG_NFS_V4_1 */
6859
6860/**
6861 * nfs4_proc_get_locations - discover locations for a migrated FSID
6862 * @inode: inode on FSID that is migrating
6863 * @locations: result of query
6864 * @page: buffer
6865 * @cred: credential to use for this operation
6866 *
6867 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6868 * operation failed, or a negative errno if a local error occurred.
6869 *
6870 * On success, "locations" is filled in, but if the server has
6871 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6872 * asserted.
6873 *
6874 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6875 * from this client that require migration recovery.
6876 */
6877int nfs4_proc_get_locations(struct inode *inode,
6878 struct nfs4_fs_locations *locations,
6879 struct page *page, struct rpc_cred *cred)
6880{
6881 struct nfs_server *server = NFS_SERVER(inode);
6882 struct nfs_client *clp = server->nfs_client;
6883 const struct nfs4_mig_recovery_ops *ops =
6884 clp->cl_mvops->mig_recovery_ops;
6885 struct nfs4_exception exception = { };
6886 int status;
6887
6888 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6889 (unsigned long long)server->fsid.major,
6890 (unsigned long long)server->fsid.minor,
6891 clp->cl_hostname);
6892 nfs_display_fhandle(NFS_FH(inode), __func__);
6893
6894 do {
6895 status = ops->get_locations(inode, locations, page, cred);
6896 if (status != -NFS4ERR_DELAY)
6897 break;
6898 nfs4_handle_exception(server, status, &exception);
6899 } while (exception.retry);
6900 return status;
6901}
6902
Chuck Lever44c99932013-10-17 14:13:30 -04006903/*
6904 * This operation also signals the server that this client is
6905 * performing "lease moved" recovery. The server can stop
6906 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6907 * is appended to this compound to identify the client ID which is
6908 * performing recovery.
6909 */
6910static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6911{
6912 struct nfs_server *server = NFS_SERVER(inode);
6913 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6914 struct rpc_clnt *clnt = server->client;
6915 struct nfs4_fsid_present_arg args = {
6916 .fh = NFS_FH(inode),
6917 .clientid = clp->cl_clientid,
6918 .renew = 1, /* append RENEW */
6919 };
6920 struct nfs4_fsid_present_res res = {
6921 .renew = 1,
6922 };
6923 struct rpc_message msg = {
6924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6925 .rpc_argp = &args,
6926 .rpc_resp = &res,
6927 .rpc_cred = cred,
6928 };
6929 unsigned long now = jiffies;
6930 int status;
6931
6932 res.fh = nfs_alloc_fhandle();
6933 if (res.fh == NULL)
6934 return -ENOMEM;
6935
6936 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6937 nfs4_set_sequence_privileged(&args.seq_args);
6938 status = nfs4_call_sync_sequence(clnt, server, &msg,
6939 &args.seq_args, &res.seq_res);
6940 nfs_free_fhandle(res.fh);
6941 if (status)
6942 return status;
6943
6944 do_renew_lease(clp, now);
6945 return 0;
6946}
6947
6948#ifdef CONFIG_NFS_V4_1
6949
6950/*
6951 * This operation also signals the server that this client is
6952 * performing "lease moved" recovery. The server can stop asserting
6953 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6954 * this operation is identified in the SEQUENCE operation in this
6955 * compound.
6956 */
6957static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6958{
6959 struct nfs_server *server = NFS_SERVER(inode);
6960 struct rpc_clnt *clnt = server->client;
6961 struct nfs4_fsid_present_arg args = {
6962 .fh = NFS_FH(inode),
6963 };
6964 struct nfs4_fsid_present_res res = {
6965 };
6966 struct rpc_message msg = {
6967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6968 .rpc_argp = &args,
6969 .rpc_resp = &res,
6970 .rpc_cred = cred,
6971 };
6972 int status;
6973
6974 res.fh = nfs_alloc_fhandle();
6975 if (res.fh == NULL)
6976 return -ENOMEM;
6977
6978 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6979 nfs4_set_sequence_privileged(&args.seq_args);
6980 status = nfs4_call_sync_sequence(clnt, server, &msg,
6981 &args.seq_args, &res.seq_res);
6982 nfs_free_fhandle(res.fh);
6983 if (status == NFS4_OK &&
6984 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6985 status = -NFS4ERR_LEASE_MOVED;
6986 return status;
6987}
6988
6989#endif /* CONFIG_NFS_V4_1 */
6990
6991/**
6992 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6993 * @inode: inode on FSID to check
6994 * @cred: credential to use for this operation
6995 *
6996 * Server indicates whether the FSID is present, moved, or not
6997 * recognized. This operation is necessary to clear a LEASE_MOVED
6998 * condition for this client ID.
6999 *
7000 * Returns NFS4_OK if the FSID is present on this server,
7001 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7002 * NFS4ERR code if some error occurred on the server, or a
7003 * negative errno if a local failure occurred.
7004 */
7005int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7006{
7007 struct nfs_server *server = NFS_SERVER(inode);
7008 struct nfs_client *clp = server->nfs_client;
7009 const struct nfs4_mig_recovery_ops *ops =
7010 clp->cl_mvops->mig_recovery_ops;
7011 struct nfs4_exception exception = { };
7012 int status;
7013
7014 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7015 (unsigned long long)server->fsid.major,
7016 (unsigned long long)server->fsid.minor,
7017 clp->cl_hostname);
7018 nfs_display_fhandle(NFS_FH(inode), __func__);
7019
7020 do {
7021 status = ops->fsid_present(inode, cred);
7022 if (status != -NFS4ERR_DELAY)
7023 break;
7024 nfs4_handle_exception(server, status, &exception);
7025 } while (exception.retry);
7026 return status;
7027}
7028
Andy Adamson5ec16a82013-08-08 10:57:55 -04007029/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007030 * If 'use_integrity' is true and the state managment nfs_client
7031 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7032 * and the machine credential as per RFC3530bis and RFC5661 Security
7033 * Considerations sections. Otherwise, just use the user cred with the
7034 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007035 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007036static 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 +00007037{
7038 int status;
7039 struct nfs4_secinfo_arg args = {
7040 .dir_fh = NFS_FH(dir),
7041 .name = name,
7042 };
7043 struct nfs4_secinfo_res res = {
7044 .flavors = flavors,
7045 };
7046 struct rpc_message msg = {
7047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7048 .rpc_argp = &args,
7049 .rpc_resp = &res,
7050 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007051 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007052 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007053
7054 if (use_integrity) {
7055 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007056 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7057 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007058 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007059
7060 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007061
7062 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7063 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7064
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007065 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7066 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007067 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007068
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007069 if (cred)
7070 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007071
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007072 return status;
7073}
7074
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007075int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7076 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007077{
7078 struct nfs4_exception exception = { };
7079 int err;
7080 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007081 err = -NFS4ERR_WRONGSEC;
7082
7083 /* try to use integrity protection with machine cred */
7084 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7085 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7086
7087 /*
7088 * if unable to use integrity protection, or SECINFO with
7089 * integrity protection returns NFS4ERR_WRONGSEC (which is
7090 * disallowed by spec, but exists in deployed servers) use
7091 * the current filesystem's rpc_client and the user cred.
7092 */
7093 if (err == -NFS4ERR_WRONGSEC)
7094 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7095
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007096 trace_nfs4_secinfo(dir, name, err);
7097 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007098 &exception);
7099 } while (exception.retry);
7100 return err;
7101}
7102
Andy Adamson557134a2009-04-01 09:21:53 -04007103#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007104/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007105 * Check the exchange flags returned by the server for invalid flags, having
7106 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7107 * DS flags set.
7108 */
7109static int nfs4_check_cl_exchange_flags(u32 flags)
7110{
7111 if (flags & ~EXCHGID4_FLAG_MASK_R)
7112 goto out_inval;
7113 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7114 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7115 goto out_inval;
7116 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7117 goto out_inval;
7118 return NFS_OK;
7119out_inval:
7120 return -NFS4ERR_INVAL;
7121}
7122
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007123static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007124nfs41_same_server_scope(struct nfs41_server_scope *a,
7125 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007126{
7127 if (a->server_scope_sz == b->server_scope_sz &&
7128 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7129 return true;
7130
7131 return false;
7132}
7133
Andy Adamson02a95de2016-02-05 16:08:37 -05007134static void
7135nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7136{
7137}
7138
7139static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7140 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7141};
7142
Andy Adamson357f54d2010-12-14 10:11:57 -05007143/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007144 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007145 *
7146 * The 4.1 client currently uses the same TCP connection for the
7147 * fore and backchannel.
7148 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007149static
7150int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7151 struct rpc_xprt *xprt,
7152 struct nfs_client *clp,
7153 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007154{
7155 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007156 struct nfs41_bind_conn_to_session_args args = {
7157 .client = clp,
7158 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7159 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007160 struct nfs41_bind_conn_to_session_res res;
7161 struct rpc_message msg = {
7162 .rpc_proc =
7163 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007164 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007165 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007166 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007167 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007168 struct rpc_task_setup task_setup_data = {
7169 .rpc_client = clnt,
7170 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007171 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007172 .rpc_message = &msg,
7173 .flags = RPC_TASK_TIMEOUT,
7174 };
7175 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007176
7177 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007178
Trond Myklebust71a097c2015-02-18 09:27:18 -08007179 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7180 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7181 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007182
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007183 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7184 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7185 args.dir = NFS4_CDFC4_FORE;
7186
7187 task = rpc_run_task(&task_setup_data);
7188 if (!IS_ERR(task)) {
7189 status = task->tk_status;
7190 rpc_put_task(task);
7191 } else
7192 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007193 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007194 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007195 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007196 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7197 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7198 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007199 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007200 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007201 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007202 dprintk("NFS: %s: Unexpected direction from server\n",
7203 __func__);
7204 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007205 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007206 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007207 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007208 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7209 __func__);
7210 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007211 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007212 }
7213 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007214out:
7215 dprintk("<-- %s status= %d\n", __func__, status);
7216 return status;
7217}
7218
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007219struct rpc_bind_conn_calldata {
7220 struct nfs_client *clp;
7221 struct rpc_cred *cred;
7222};
7223
7224static int
7225nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7226 struct rpc_xprt *xprt,
7227 void *calldata)
7228{
7229 struct rpc_bind_conn_calldata *p = calldata;
7230
7231 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7232}
7233
7234int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7235{
7236 struct rpc_bind_conn_calldata data = {
7237 .clp = clp,
7238 .cred = cred,
7239 };
7240 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7241 nfs4_proc_bind_conn_to_session_callback, &data);
7242}
7243
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007244/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007245 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7246 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007247 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007248static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7249 .how = SP4_MACH_CRED,
7250 .enforce.u.words = {
7251 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7252 1 << (OP_EXCHANGE_ID - 32) |
7253 1 << (OP_CREATE_SESSION - 32) |
7254 1 << (OP_DESTROY_SESSION - 32) |
7255 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007256 },
7257 .allow.u.words = {
7258 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007259 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007260 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007261 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007262 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007263 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007264 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007265 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007266 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007267 1 << (OP_FREE_STATEID - 32) |
7268 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007269 }
7270};
7271
7272/*
7273 * Select the state protection mode for client `clp' given the server results
7274 * from exchange_id in `sp'.
7275 *
7276 * Returns 0 on success, negative errno otherwise.
7277 */
7278static int nfs4_sp4_select_mode(struct nfs_client *clp,
7279 struct nfs41_state_protection *sp)
7280{
7281 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7282 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7283 1 << (OP_EXCHANGE_ID - 32) |
7284 1 << (OP_CREATE_SESSION - 32) |
7285 1 << (OP_DESTROY_SESSION - 32) |
7286 1 << (OP_DESTROY_CLIENTID - 32)
7287 };
7288 unsigned int i;
7289
7290 if (sp->how == SP4_MACH_CRED) {
7291 /* Print state protect result */
7292 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7293 for (i = 0; i <= LAST_NFS4_OP; i++) {
7294 if (test_bit(i, sp->enforce.u.longs))
7295 dfprintk(MOUNT, " enforce op %d\n", i);
7296 if (test_bit(i, sp->allow.u.longs))
7297 dfprintk(MOUNT, " allow op %d\n", i);
7298 }
7299
7300 /* make sure nothing is on enforce list that isn't supported */
7301 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7302 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7303 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7304 return -EINVAL;
7305 }
7306 }
7307
7308 /*
7309 * Minimal mode - state operations are allowed to use machine
7310 * credential. Note this already happens by default, so the
7311 * client doesn't have to do anything more than the negotiation.
7312 *
7313 * NOTE: we don't care if EXCHANGE_ID is in the list -
7314 * we're already using the machine cred for exchange_id
7315 * and will never use a different cred.
7316 */
7317 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7318 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7319 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7320 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7321 dfprintk(MOUNT, "sp4_mach_cred:\n");
7322 dfprintk(MOUNT, " minimal mode enabled\n");
7323 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7324 } else {
7325 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7326 return -EINVAL;
7327 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007328
7329 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007330 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7331 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007332 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7333 dfprintk(MOUNT, " cleanup mode enabled\n");
7334 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7335 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007336
Andrew Elble99ade3c2015-12-02 09:39:51 -05007337 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7338 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7339 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7340 &clp->cl_sp4_flags);
7341 }
7342
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007343 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7344 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7345 dfprintk(MOUNT, " secinfo mode enabled\n");
7346 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7347 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007348
7349 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7350 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7351 dfprintk(MOUNT, " stateid mode enabled\n");
7352 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7353 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007354
7355 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7356 dfprintk(MOUNT, " write mode enabled\n");
7357 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7358 }
7359
7360 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7361 dfprintk(MOUNT, " commit mode enabled\n");
7362 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7363 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007364 }
7365
7366 return 0;
7367}
7368
Andy Adamson8d89bd72016-09-09 09:22:18 -04007369struct nfs41_exchange_id_data {
7370 struct nfs41_exchange_id_res res;
7371 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007372 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007373 int rpc_status;
7374};
7375
7376static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007377{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007378 struct nfs41_exchange_id_data *cdata =
7379 (struct nfs41_exchange_id_data *)data;
7380 struct nfs_client *clp = cdata->args.client;
7381 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007382
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007383 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007384
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007385 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007386 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007387
7388 if (cdata->xprt && status == 0) {
7389 status = nfs4_detect_session_trunking(clp, &cdata->res,
7390 cdata->xprt);
7391 goto out;
7392 }
7393
Andy Adamson8d89bd72016-09-09 09:22:18 -04007394 if (status == 0)
7395 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007396
Chuck Lever177313f2012-05-21 22:44:58 -04007397 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007398 clp->cl_clientid = cdata->res.clientid;
7399 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007400 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007401 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007402 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007403 &clp->cl_session->session_state);
7404 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007405 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007406
Chuck Leveracdeb692012-05-21 22:46:16 -04007407 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007408 clp->cl_serverowner = cdata->res.server_owner;
7409 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007410
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007411 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007412 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007413 clp->cl_implid = cdata->res.impl_id;
7414 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007415
Chuck Lever177313f2012-05-21 22:44:58 -04007416 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007417 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007418 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007419 dprintk("%s: server_scope mismatch detected\n",
7420 __func__);
7421 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007422 kfree(clp->cl_serverscope);
7423 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007424 }
7425
Chuck Lever177313f2012-05-21 22:44:58 -04007426 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007427 clp->cl_serverscope = cdata->res.server_scope;
7428 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007429 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007430 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007431 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007432 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007433 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007434out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007435 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007436 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007437}
7438
7439static void nfs4_exchange_id_release(void *data)
7440{
7441 struct nfs41_exchange_id_data *cdata =
7442 (struct nfs41_exchange_id_data *)data;
7443
Andy Adamsonad0849a2016-09-09 09:22:28 -04007444 if (cdata->xprt) {
7445 xprt_put(cdata->xprt);
7446 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7447 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007448 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007449 kfree(cdata->res.impl_id);
7450 kfree(cdata->res.server_scope);
7451 kfree(cdata->res.server_owner);
7452 kfree(cdata);
7453}
7454
7455static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7456 .rpc_call_done = nfs4_exchange_id_done,
7457 .rpc_release = nfs4_exchange_id_release,
7458};
7459
Benny Halevy99fe60d2009-04-01 09:22:29 -04007460/*
7461 * _nfs4_proc_exchange_id()
7462 *
7463 * Wrapper for EXCHANGE_ID operation.
7464 */
7465static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007466 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007467{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007468 struct rpc_message msg = {
7469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007470 .rpc_cred = cred,
7471 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007472 struct rpc_task_setup task_setup_data = {
7473 .rpc_client = clp->cl_rpcclient,
7474 .callback_ops = &nfs4_exchange_id_call_ops,
7475 .rpc_message = &msg,
7476 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7477 };
7478 struct nfs41_exchange_id_data *calldata;
7479 struct rpc_task *task;
7480 int status = -EIO;
7481
7482 if (!atomic_inc_not_zero(&clp->cl_count))
7483 goto out;
7484
7485 status = -ENOMEM;
7486 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7487 if (!calldata)
7488 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007489
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007490 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007491
7492 status = nfs4_init_uniform_client_string(clp);
7493 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007495
7496 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7497 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7498 clp->cl_owner_id);
7499
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7501 GFP_NOFS);
7502 status = -ENOMEM;
7503 if (unlikely(calldata->res.server_owner == NULL))
7504 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007505
Andy Adamson8d89bd72016-09-09 09:22:18 -04007506 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007507 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007508 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007509 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007510
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7512 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007513 goto out_server_scope;
7514
7515 switch (sp4_how) {
7516 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007517 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007518 break;
7519
7520 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007521 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007522 break;
7523
7524 default:
7525 /* unsupported! */
7526 WARN_ON_ONCE(1);
7527 status = -EINVAL;
7528 goto out_impl_id;
7529 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007530 if (xprt) {
7531 calldata->xprt = xprt;
7532 task_setup_data.rpc_xprt = xprt;
7533 task_setup_data.flags =
7534 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007535 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7536 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007537 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 calldata->args.client = clp;
7539#ifdef CONFIG_NFS_V4_1_MIGRATION
7540 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7541 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7542 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7543#else
7544 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7545 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7546#endif
7547 msg.rpc_argp = &calldata->args;
7548 msg.rpc_resp = &calldata->res;
7549 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007550
Andy Adamson8d89bd72016-09-09 09:22:18 -04007551 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007552 if (IS_ERR(task))
7553 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007554
Andy Adamsonad0849a2016-09-09 09:22:28 -04007555 if (!xprt) {
7556 status = rpc_wait_for_completion_task(task);
7557 if (!status)
7558 status = calldata->rpc_status;
7559 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007560 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007561
Andy Adamson8d89bd72016-09-09 09:22:18 -04007562 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007563out:
Chuck Lever177313f2012-05-21 22:44:58 -04007564 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007565 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007566 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007567 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007568 clp->cl_implid->date.seconds,
7569 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007570 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007571 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007572
7573out_impl_id:
7574 kfree(calldata->res.impl_id);
7575out_server_scope:
7576 kfree(calldata->res.server_scope);
7577out_server_owner:
7578 kfree(calldata->res.server_owner);
7579out_calldata:
7580 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007581 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007582 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007583}
7584
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007585/*
7586 * nfs4_proc_exchange_id()
7587 *
7588 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7589 *
7590 * Since the clientid has expired, all compounds using sessions
7591 * associated with the stale clientid will be returning
7592 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7593 * be in some phase of session reset.
7594 *
7595 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7596 */
7597int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7598{
7599 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7600 int status;
7601
7602 /* try SP4_MACH_CRED if krb5i/p */
7603 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7604 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007605 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007606 if (!status)
7607 return 0;
7608 }
7609
7610 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007611 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007612}
7613
Andy Adamson04fa2c62016-09-09 09:22:29 -04007614/**
7615 * nfs4_test_session_trunk
7616 *
7617 * This is an add_xprt_test() test function called from
7618 * rpc_clnt_setup_test_and_add_xprt.
7619 *
7620 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7621 * and is dereferrenced in nfs4_exchange_id_release
7622 *
7623 * Upon success, add the new transport to the rpc_clnt
7624 *
7625 * @clnt: struct rpc_clnt to get new transport
7626 * @xprt: the rpc_xprt to test
7627 * @data: call data for _nfs4_proc_exchange_id.
7628 */
7629int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7630 void *data)
7631{
7632 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7633 u32 sp4_how;
7634
7635 dprintk("--> %s try %s\n", __func__,
7636 xprt->address_strings[RPC_DISPLAY_ADDR]);
7637
7638 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7639
7640 /* Test connection for session trunking. Async exchange_id call */
7641 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7642}
7643EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7644
Trond Myklebust66245532012-05-25 17:18:09 -04007645static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7646 struct rpc_cred *cred)
7647{
7648 struct rpc_message msg = {
7649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7650 .rpc_argp = clp,
7651 .rpc_cred = cred,
7652 };
7653 int status;
7654
7655 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007656 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007657 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007658 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007659 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7660 return status;
7661}
7662
7663static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7664 struct rpc_cred *cred)
7665{
7666 unsigned int loop;
7667 int ret;
7668
7669 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7670 ret = _nfs4_proc_destroy_clientid(clp, cred);
7671 switch (ret) {
7672 case -NFS4ERR_DELAY:
7673 case -NFS4ERR_CLIENTID_BUSY:
7674 ssleep(1);
7675 break;
7676 default:
7677 return ret;
7678 }
7679 }
7680 return 0;
7681}
7682
7683int nfs4_destroy_clientid(struct nfs_client *clp)
7684{
7685 struct rpc_cred *cred;
7686 int ret = 0;
7687
7688 if (clp->cl_mvops->minor_version < 1)
7689 goto out;
7690 if (clp->cl_exchange_flags == 0)
7691 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007692 if (clp->cl_preserve_clid)
7693 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007694 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007695 ret = nfs4_proc_destroy_clientid(clp, cred);
7696 if (cred)
7697 put_rpccred(cred);
7698 switch (ret) {
7699 case 0:
7700 case -NFS4ERR_STALE_CLIENTID:
7701 clp->cl_exchange_flags = 0;
7702 }
7703out:
7704 return ret;
7705}
7706
Andy Adamson2050f0c2009-04-01 09:22:30 -04007707struct nfs4_get_lease_time_data {
7708 struct nfs4_get_lease_time_args *args;
7709 struct nfs4_get_lease_time_res *res;
7710 struct nfs_client *clp;
7711};
7712
7713static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7714 void *calldata)
7715{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007716 struct nfs4_get_lease_time_data *data =
7717 (struct nfs4_get_lease_time_data *)calldata;
7718
7719 dprintk("--> %s\n", __func__);
7720 /* just setup sequence, do not trigger session recovery
7721 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007722 nfs41_setup_sequence(data->clp->cl_session,
7723 &data->args->la_seq_args,
7724 &data->res->lr_seq_res,
7725 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007726 dprintk("<-- %s\n", __func__);
7727}
7728
7729/*
7730 * Called from nfs4_state_manager thread for session setup, so don't recover
7731 * from sequence operation or clientid errors.
7732 */
7733static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7734{
7735 struct nfs4_get_lease_time_data *data =
7736 (struct nfs4_get_lease_time_data *)calldata;
7737
7738 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007739 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7740 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007741 switch (task->tk_status) {
7742 case -NFS4ERR_DELAY:
7743 case -NFS4ERR_GRACE:
7744 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7745 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7746 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007747 /* fall through */
7748 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007749 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007750 return;
7751 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007752 dprintk("<-- %s\n", __func__);
7753}
7754
Trond Myklebust17280172012-03-11 13:11:00 -04007755static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007756 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7757 .rpc_call_done = nfs4_get_lease_time_done,
7758};
7759
7760int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7761{
7762 struct rpc_task *task;
7763 struct nfs4_get_lease_time_args args;
7764 struct nfs4_get_lease_time_res res = {
7765 .lr_fsinfo = fsinfo,
7766 };
7767 struct nfs4_get_lease_time_data data = {
7768 .args = &args,
7769 .res = &res,
7770 .clp = clp,
7771 };
7772 struct rpc_message msg = {
7773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7774 .rpc_argp = &args,
7775 .rpc_resp = &res,
7776 };
7777 struct rpc_task_setup task_setup = {
7778 .rpc_client = clp->cl_rpcclient,
7779 .rpc_message = &msg,
7780 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007781 .callback_data = &data,
7782 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007783 };
7784 int status;
7785
Chuck Levera9c92d62013-08-09 12:48:18 -04007786 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007787 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007788 dprintk("--> %s\n", __func__);
7789 task = rpc_run_task(&task_setup);
7790
7791 if (IS_ERR(task))
7792 status = PTR_ERR(task);
7793 else {
7794 status = task->tk_status;
7795 rpc_put_task(task);
7796 }
7797 dprintk("<-- %s return %d\n", __func__, status);
7798
7799 return status;
7800}
7801
Andy Adamsonfc931582009-04-01 09:22:31 -04007802/*
7803 * Initialize the values to be used by the client in CREATE_SESSION
7804 * If nfs4_init_session set the fore channel request and response sizes,
7805 * use them.
7806 *
7807 * Set the back channel max_resp_sz_cached to zero to force the client to
7808 * always set csa_cachethis to FALSE because the current implementation
7809 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7810 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007811static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7812 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007813{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007814 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007815 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007816
Andy Adamson18aad3d2013-06-26 12:21:49 -04007817 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7818 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7819
Andy Adamsonfc931582009-04-01 09:22:31 -04007820 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007821 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7822 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007823 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007824 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007825
7826 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007827 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007828 __func__,
7829 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007830 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007831
7832 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007833 args->bc_attrs.max_rqst_sz = max_bc_payload;
7834 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007835 args->bc_attrs.max_resp_sz_cached = 0;
7836 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007837 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007838
7839 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7840 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7841 __func__,
7842 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7843 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7844 args->bc_attrs.max_reqs);
7845}
7846
Trond Myklebust79969dd2015-02-18 11:30:18 -08007847static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7848 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007849{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007850 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007851 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007852
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007853 if (rcvd->max_resp_sz > sent->max_resp_sz)
7854 return -EINVAL;
7855 /*
7856 * Our requested max_ops is the minimum we need; we're not
7857 * prepared to break up compounds into smaller pieces than that.
7858 * So, no point even trying to continue if the server won't
7859 * cooperate:
7860 */
7861 if (rcvd->max_ops < sent->max_ops)
7862 return -EINVAL;
7863 if (rcvd->max_reqs == 0)
7864 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007865 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7866 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007867 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007868}
7869
Trond Myklebust79969dd2015-02-18 11:30:18 -08007870static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7871 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007872{
7873 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007874 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007875
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007876 if (!(res->flags & SESSION4_BACK_CHAN))
7877 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007878 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7879 return -EINVAL;
7880 if (rcvd->max_resp_sz < sent->max_resp_sz)
7881 return -EINVAL;
7882 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7883 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007884 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007885 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007886 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007887 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007888out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007889 return 0;
7890}
Andy Adamson8d353012009-04-01 09:22:32 -04007891
Andy Adamson8d353012009-04-01 09:22:32 -04007892static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007893 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007894{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007895 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007896
Trond Myklebust79969dd2015-02-18 11:30:18 -08007897 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007898 if (ret)
7899 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007900 return nfs4_verify_back_channel_attrs(args, res);
7901}
7902
7903static void nfs4_update_session(struct nfs4_session *session,
7904 struct nfs41_create_session_res *res)
7905{
7906 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007907 /* Mark client id and session as being confirmed */
7908 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7909 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007910 session->flags = res->flags;
7911 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007912 if (res->flags & SESSION4_BACK_CHAN)
7913 memcpy(&session->bc_attrs, &res->bc_attrs,
7914 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007915}
7916
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007917static int _nfs4_proc_create_session(struct nfs_client *clp,
7918 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007919{
7920 struct nfs4_session *session = clp->cl_session;
7921 struct nfs41_create_session_args args = {
7922 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007923 .clientid = clp->cl_clientid,
7924 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007925 .cb_program = NFS4_CALLBACK,
7926 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007927 struct nfs41_create_session_res res;
7928
Andy Adamsonfc931582009-04-01 09:22:31 -04007929 struct rpc_message msg = {
7930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7931 .rpc_argp = &args,
7932 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007933 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007934 };
7935 int status;
7936
Chuck Lever6b26cc82016-05-02 14:40:40 -04007937 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007938 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007939
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007940 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007941 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007942
Trond Myklebustb519d402016-09-11 14:50:01 -04007943 switch (status) {
7944 case -NFS4ERR_STALE_CLIENTID:
7945 case -NFS4ERR_DELAY:
7946 case -ETIMEDOUT:
7947 case -EACCES:
7948 case -EAGAIN:
7949 goto out;
7950 };
7951
7952 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007953 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007954 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007955 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007956 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007957 if (status)
7958 goto out;
7959 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007960 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007961out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007962 return status;
7963}
7964
7965/*
7966 * Issues a CREATE_SESSION operation to the server.
7967 * It is the responsibility of the caller to verify the session is
7968 * expired before calling this routine.
7969 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007970int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007971{
7972 int status;
7973 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007974 struct nfs4_session *session = clp->cl_session;
7975
7976 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7977
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007978 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007979 if (status)
7980 goto out;
7981
Andy Adamsonaacd5532011-11-09 13:58:21 -05007982 /* Init or reset the session slot tables */
7983 status = nfs4_setup_session_slot_tables(session);
7984 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007985 if (status)
7986 goto out;
7987
7988 ptr = (unsigned *)&session->sess_id.data[0];
7989 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7990 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007991out:
7992 dprintk("<-- %s\n", __func__);
7993 return status;
7994}
7995
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007996/*
7997 * Issue the over-the-wire RPC DESTROY_SESSION.
7998 * The caller must serialize access to this routine.
7999 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008000int nfs4_proc_destroy_session(struct nfs4_session *session,
8001 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008002{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008003 struct rpc_message msg = {
8004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8005 .rpc_argp = session,
8006 .rpc_cred = cred,
8007 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008008 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008009
8010 dprintk("--> nfs4_proc_destroy_session\n");
8011
8012 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008013 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8014 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008015
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008016 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008017 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008018
8019 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008020 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008021 "Session has been destroyed regardless...\n", status);
8022
8023 dprintk("<-- nfs4_proc_destroy_session\n");
8024 return status;
8025}
8026
Trond Myklebust7b38c362012-05-23 13:23:31 -04008027/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008028 * Renew the cl_session lease.
8029 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008030struct nfs4_sequence_data {
8031 struct nfs_client *clp;
8032 struct nfs4_sequence_args args;
8033 struct nfs4_sequence_res res;
8034};
8035
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008036static void nfs41_sequence_release(void *data)
8037{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008038 struct nfs4_sequence_data *calldata = data;
8039 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008040
Alexandros Batsakis71358402010-02-05 03:45:05 -08008041 if (atomic_read(&clp->cl_count) > 1)
8042 nfs4_schedule_state_renewal(clp);
8043 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008044 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008045}
8046
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008047static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8048{
8049 switch(task->tk_status) {
8050 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008051 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8052 return -EAGAIN;
8053 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008054 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008055 }
8056 return 0;
8057}
8058
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008059static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008060{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008061 struct nfs4_sequence_data *calldata = data;
8062 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008063
Trond Myklebust14516c32010-07-31 14:29:06 -04008064 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8065 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008066
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008067 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008068 if (task->tk_status < 0) {
8069 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008070 if (atomic_read(&clp->cl_count) == 1)
8071 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008072
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008073 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8074 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008075 return;
8076 }
8077 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008079out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008080 dprintk("<-- %s\n", __func__);
8081}
8082
8083static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8084{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008085 struct nfs4_sequence_data *calldata = data;
8086 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008087 struct nfs4_sequence_args *args;
8088 struct nfs4_sequence_res *res;
8089
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008090 args = task->tk_msg.rpc_argp;
8091 res = task->tk_msg.rpc_resp;
8092
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008093 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008094}
8095
8096static const struct rpc_call_ops nfs41_sequence_ops = {
8097 .rpc_call_done = nfs41_sequence_call_done,
8098 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008099 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008100};
8101
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8103 struct rpc_cred *cred,
8104 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008105{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008106 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008107 struct rpc_message msg = {
8108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8109 .rpc_cred = cred,
8110 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008111 struct rpc_task_setup task_setup_data = {
8112 .rpc_client = clp->cl_rpcclient,
8113 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008114 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008115 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008116 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008117
Alexandros Batsakis71358402010-02-05 03:45:05 -08008118 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008119 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008120 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008121 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008122 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008123 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008124 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008125 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008126 if (is_privileged)
8127 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008128 msg.rpc_argp = &calldata->args;
8129 msg.rpc_resp = &calldata->res;
8130 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008131 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008132
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008133 return rpc_run_task(&task_setup_data);
8134}
8135
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008136static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008137{
8138 struct rpc_task *task;
8139 int ret = 0;
8140
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008141 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008142 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008143 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008144 if (IS_ERR(task))
8145 ret = PTR_ERR(task);
8146 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008147 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008148 dprintk("<-- %s status=%d\n", __func__, ret);
8149 return ret;
8150}
8151
8152static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8153{
8154 struct rpc_task *task;
8155 int ret;
8156
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008157 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008158 if (IS_ERR(task)) {
8159 ret = PTR_ERR(task);
8160 goto out;
8161 }
8162 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008163 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008164 ret = task->tk_status;
8165 rpc_put_task(task);
8166out:
8167 dprintk("<-- %s status=%d\n", __func__, ret);
8168 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008169}
8170
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008171struct nfs4_reclaim_complete_data {
8172 struct nfs_client *clp;
8173 struct nfs41_reclaim_complete_args arg;
8174 struct nfs41_reclaim_complete_res res;
8175};
8176
8177static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8178{
8179 struct nfs4_reclaim_complete_data *calldata = data;
8180
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008181 nfs41_setup_sequence(calldata->clp->cl_session,
8182 &calldata->arg.seq_args,
8183 &calldata->res.seq_res,
8184 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008185}
8186
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008187static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8188{
8189 switch(task->tk_status) {
8190 case 0:
8191 case -NFS4ERR_COMPLETE_ALREADY:
8192 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8193 break;
8194 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008195 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008196 /* fall through */
8197 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008198 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008199 case -NFS4ERR_BADSESSION:
8200 case -NFS4ERR_DEADSESSION:
8201 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8202 nfs4_schedule_session_recovery(clp->cl_session,
8203 task->tk_status);
8204 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008205 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008206 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008207 }
8208 return 0;
8209}
8210
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008211static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8212{
8213 struct nfs4_reclaim_complete_data *calldata = data;
8214 struct nfs_client *clp = calldata->clp;
8215 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8216
8217 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008218 if (!nfs41_sequence_done(task, res))
8219 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008220
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008221 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008222 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8223 rpc_restart_call_prepare(task);
8224 return;
8225 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008226 dprintk("<-- %s\n", __func__);
8227}
8228
8229static void nfs4_free_reclaim_complete_data(void *data)
8230{
8231 struct nfs4_reclaim_complete_data *calldata = data;
8232
8233 kfree(calldata);
8234}
8235
8236static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8237 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8238 .rpc_call_done = nfs4_reclaim_complete_done,
8239 .rpc_release = nfs4_free_reclaim_complete_data,
8240};
8241
8242/*
8243 * Issue a global reclaim complete.
8244 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008245static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8246 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008247{
8248 struct nfs4_reclaim_complete_data *calldata;
8249 struct rpc_task *task;
8250 struct rpc_message msg = {
8251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008252 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008253 };
8254 struct rpc_task_setup task_setup_data = {
8255 .rpc_client = clp->cl_rpcclient,
8256 .rpc_message = &msg,
8257 .callback_ops = &nfs4_reclaim_complete_call_ops,
8258 .flags = RPC_TASK_ASYNC,
8259 };
8260 int status = -ENOMEM;
8261
8262 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008263 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008264 if (calldata == NULL)
8265 goto out;
8266 calldata->clp = clp;
8267 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008268
Chuck Levera9c92d62013-08-09 12:48:18 -04008269 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008270 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008271 msg.rpc_argp = &calldata->arg;
8272 msg.rpc_resp = &calldata->res;
8273 task_setup_data.callback_data = calldata;
8274 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008275 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008276 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008277 goto out;
8278 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008279 status = nfs4_wait_for_completion_rpc_task(task);
8280 if (status == 0)
8281 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282 rpc_put_task(task);
8283out:
8284 dprintk("<-- %s status=%d\n", __func__, status);
8285 return status;
8286}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008287
8288static void
8289nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8290{
8291 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008292 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008293 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008294
8295 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008296 nfs41_setup_sequence(session, &lgp->args.seq_args,
8297 &lgp->res.seq_res, task);
8298 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008299}
8300
8301static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8302{
8303 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008304
8305 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008306 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008307 dprintk("<-- %s\n", __func__);
8308}
8309
8310static int
8311nfs4_layoutget_handle_exception(struct rpc_task *task,
8312 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8313{
Trond Myklebustee314c22012-10-01 17:25:48 -07008314 struct inode *inode = lgp->args.inode;
8315 struct nfs_server *server = NFS_SERVER(inode);
8316 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008317 int nfs4err = task->tk_status;
8318 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008319 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008320
Boaz Harroshed7e5422014-01-22 20:34:54 +02008321 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008322
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008323 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008324 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008325 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008326
8327 /*
8328 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8329 * on the file. set tk_status to -ENODATA to tell upper layer to
8330 * retry go inband.
8331 */
8332 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008333 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008334 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008335 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008336 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8337 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8338 */
8339 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008340 status = -EOVERFLOW;
8341 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008342 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008343 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008344 * (or clients) writing to the same RAID stripe except when
8345 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008346 *
8347 * Treat it like we would RECALLCONFLICT -- we retry for a little
8348 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008349 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008350 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008351 if (lgp->args.minlength == 0) {
8352 status = -EOVERFLOW;
8353 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008354 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008355 status = -EBUSY;
8356 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008357 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008358 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008359 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008360 case -NFS4ERR_DELEG_REVOKED:
8361 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008362 case -NFS4ERR_EXPIRED:
8363 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008364 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008365 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008366 lo = NFS_I(inode)->layout;
8367 /* If the open stateid was bad, then recover it. */
8368 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8369 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008370 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008371 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008372 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008373 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008374 break;
8375 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008376
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008377 /*
8378 * Mark the bad layout state as invalid, then retry
8379 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008380 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008381 spin_unlock(&inode->i_lock);
8382 pnfs_free_lseg_list(&head);
8383 status = -EAGAIN;
8384 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008385 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008386
Trond Myklebust0a702352017-01-23 22:44:12 -05008387 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008388 err = nfs4_handle_exception(server, nfs4err, exception);
8389 if (!status) {
8390 if (exception->retry)
8391 status = -EAGAIN;
8392 else
8393 status = err;
8394 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008395out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008396 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008397 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008398}
8399
Idan Kedar85541162012-08-02 11:47:10 +03008400static size_t max_response_pages(struct nfs_server *server)
8401{
8402 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8403 return nfs_page_array_len(0, max_resp_sz);
8404}
8405
8406static void nfs4_free_pages(struct page **pages, size_t size)
8407{
8408 int i;
8409
8410 if (!pages)
8411 return;
8412
8413 for (i = 0; i < size; i++) {
8414 if (!pages[i])
8415 break;
8416 __free_page(pages[i]);
8417 }
8418 kfree(pages);
8419}
8420
8421static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8422{
8423 struct page **pages;
8424 int i;
8425
8426 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8427 if (!pages) {
8428 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8429 return NULL;
8430 }
8431
8432 for (i = 0; i < size; i++) {
8433 pages[i] = alloc_page(gfp_flags);
8434 if (!pages[i]) {
8435 dprintk("%s: failed to allocate page\n", __func__);
8436 nfs4_free_pages(pages, size);
8437 return NULL;
8438 }
8439 }
8440
8441 return pages;
8442}
8443
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008444static void nfs4_layoutget_release(void *calldata)
8445{
8446 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008447 struct inode *inode = lgp->args.inode;
8448 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008449 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008450
8451 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008452 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008453 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008454 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008455 put_nfs_open_context(lgp->args.ctx);
8456 kfree(calldata);
8457 dprintk("<-- %s\n", __func__);
8458}
8459
8460static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8461 .rpc_call_prepare = nfs4_layoutget_prepare,
8462 .rpc_call_done = nfs4_layoutget_done,
8463 .rpc_release = nfs4_layoutget_release,
8464};
8465
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008466struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008467nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008468{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008469 struct inode *inode = lgp->args.inode;
8470 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008471 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472 struct rpc_task *task;
8473 struct rpc_message msg = {
8474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8475 .rpc_argp = &lgp->args,
8476 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008477 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478 };
8479 struct rpc_task_setup task_setup_data = {
8480 .rpc_client = server->client,
8481 .rpc_message = &msg,
8482 .callback_ops = &nfs4_layoutget_call_ops,
8483 .callback_data = lgp,
8484 .flags = RPC_TASK_ASYNC,
8485 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008486 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008487 struct nfs4_exception exception = {
8488 .inode = inode,
8489 .timeout = *timeout,
8490 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008491 int status = 0;
8492
8493 dprintk("--> %s\n", __func__);
8494
Peng Tao4bd5a982014-11-17 11:05:17 +08008495 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8496 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8497
Idan Kedar85541162012-08-02 11:47:10 +03008498 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8499 if (!lgp->args.layout.pages) {
8500 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008501 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008502 }
8503 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8504
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008505 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008506 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008507 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008508
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008509 task = rpc_run_task(&task_setup_data);
8510 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008511 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008512 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008513 if (status == 0) {
8514 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8515 *timeout = exception.timeout;
8516 }
8517
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008518 trace_nfs4_layoutget(lgp->args.ctx,
8519 &lgp->args.range,
8520 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008521 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008522 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008523
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008524 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8525 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008526 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008527 rpc_put_task(task);
8528 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008529 if (status)
8530 return ERR_PTR(status);
8531 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008532}
8533
Benny Halevycbe82602011-05-22 19:52:37 +03008534static void
8535nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8536{
8537 struct nfs4_layoutreturn *lrp = calldata;
8538
8539 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008540 nfs41_setup_sequence(lrp->clp->cl_session,
8541 &lrp->args.seq_args,
8542 &lrp->res.seq_res,
8543 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008544}
8545
8546static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8547{
8548 struct nfs4_layoutreturn *lrp = calldata;
8549 struct nfs_server *server;
8550
8551 dprintk("--> %s\n", __func__);
8552
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008553 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008554 return;
8555
8556 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008557 switch (task->tk_status) {
8558 default:
8559 task->tk_status = 0;
8560 case 0:
8561 break;
8562 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008563 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008564 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008565 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008566 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008567 return;
8568 }
Benny Halevycbe82602011-05-22 19:52:37 +03008569 dprintk("<-- %s\n", __func__);
8570}
8571
8572static void nfs4_layoutreturn_release(void *calldata)
8573{
8574 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008575 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008576 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008577
8578 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008579 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008580 if (lrp->res.lrs_present) {
8581 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8582 &lrp->args.range,
8583 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008584 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008585 } else
8586 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008587 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008588 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008589 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008590 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008591 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008592 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008593 kfree(calldata);
8594 dprintk("<-- %s\n", __func__);
8595}
8596
8597static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8598 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8599 .rpc_call_done = nfs4_layoutreturn_done,
8600 .rpc_release = nfs4_layoutreturn_release,
8601};
8602
Peng Tao6c166052014-11-17 09:30:40 +08008603int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008604{
8605 struct rpc_task *task;
8606 struct rpc_message msg = {
8607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8608 .rpc_argp = &lrp->args,
8609 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008610 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008611 };
8612 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008613 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008614 .rpc_message = &msg,
8615 .callback_ops = &nfs4_layoutreturn_call_ops,
8616 .callback_data = lrp,
8617 };
Peng Tao6c166052014-11-17 09:30:40 +08008618 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008619
Andrew Elble99ade3c2015-12-02 09:39:51 -05008620 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8621 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8622 &task_setup_data.rpc_client, &msg);
8623
Benny Halevycbe82602011-05-22 19:52:37 +03008624 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008625 if (!sync) {
8626 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8627 if (!lrp->inode) {
8628 nfs4_layoutreturn_release(lrp);
8629 return -EAGAIN;
8630 }
8631 task_setup_data.flags |= RPC_TASK_ASYNC;
8632 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008633 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008634 task = rpc_run_task(&task_setup_data);
8635 if (IS_ERR(task))
8636 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008637 if (sync)
8638 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008639 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008640 dprintk("<-- %s status=%d\n", __func__, status);
8641 rpc_put_task(task);
8642 return status;
8643}
8644
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008645static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008646_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8647 struct pnfs_device *pdev,
8648 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008649{
8650 struct nfs4_getdeviceinfo_args args = {
8651 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008652 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8653 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008654 };
8655 struct nfs4_getdeviceinfo_res res = {
8656 .pdev = pdev,
8657 };
8658 struct rpc_message msg = {
8659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8660 .rpc_argp = &args,
8661 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008662 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663 };
8664 int status;
8665
8666 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008667 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008668 if (res.notification & ~args.notify_types)
8669 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008670 if (res.notification != args.notify_types)
8671 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008672
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008673 dprintk("<-- %s status=%d\n", __func__, status);
8674
8675 return status;
8676}
8677
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008678int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8679 struct pnfs_device *pdev,
8680 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008681{
8682 struct nfs4_exception exception = { };
8683 int err;
8684
8685 do {
8686 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008687 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008688 &exception);
8689 } while (exception.retry);
8690 return err;
8691}
8692EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8693
Andy Adamson863a3c62011-03-23 13:27:54 +00008694static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8695{
8696 struct nfs4_layoutcommit_data *data = calldata;
8697 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008698 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008699
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008700 nfs41_setup_sequence(session,
8701 &data->args.seq_args,
8702 &data->res.seq_res,
8703 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008704}
8705
8706static void
8707nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8708{
8709 struct nfs4_layoutcommit_data *data = calldata;
8710 struct nfs_server *server = NFS_SERVER(data->args.inode);
8711
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008712 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008713 return;
8714
8715 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008716 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8717 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8718 case -NFS4ERR_BADLAYOUT: /* no layout */
8719 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008720 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008721 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008722 break;
8723 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008724 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008725 rpc_restart_call_prepare(task);
8726 return;
8727 }
8728 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008729}
8730
8731static void nfs4_layoutcommit_release(void *calldata)
8732{
8733 struct nfs4_layoutcommit_data *data = calldata;
8734
Andy Adamsondb29c082011-07-30 20:52:38 -04008735 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008736 nfs_post_op_update_inode_force_wcc(data->args.inode,
8737 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008738 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008739 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008740 kfree(data);
8741}
8742
8743static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8744 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8745 .rpc_call_done = nfs4_layoutcommit_done,
8746 .rpc_release = nfs4_layoutcommit_release,
8747};
8748
8749int
Andy Adamsonef311532011-03-12 02:58:10 -05008750nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008751{
8752 struct rpc_message msg = {
8753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8754 .rpc_argp = &data->args,
8755 .rpc_resp = &data->res,
8756 .rpc_cred = data->cred,
8757 };
8758 struct rpc_task_setup task_setup_data = {
8759 .task = &data->task,
8760 .rpc_client = NFS_CLIENT(data->args.inode),
8761 .rpc_message = &msg,
8762 .callback_ops = &nfs4_layoutcommit_ops,
8763 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008764 };
8765 struct rpc_task *task;
8766 int status = 0;
8767
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008768 dprintk("NFS: initiating layoutcommit call. sync %d "
8769 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008770 data->args.lastbytewritten,
8771 data->args.inode->i_ino);
8772
Trond Myklebust472e2592015-02-05 16:50:30 -05008773 if (!sync) {
8774 data->inode = nfs_igrab_and_active(data->args.inode);
8775 if (data->inode == NULL) {
8776 nfs4_layoutcommit_release(data);
8777 return -EAGAIN;
8778 }
8779 task_setup_data.flags = RPC_TASK_ASYNC;
8780 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008781 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008782 task = rpc_run_task(&task_setup_data);
8783 if (IS_ERR(task))
8784 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008785 if (sync)
8786 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008787 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008788 dprintk("%s: status %d\n", __func__, status);
8789 rpc_put_task(task);
8790 return status;
8791}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008792
Andy Adamson97431202013-08-08 10:57:56 -04008793/**
8794 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8795 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8796 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008797static int
8798_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008799 struct nfs_fsinfo *info,
8800 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008801{
8802 struct nfs41_secinfo_no_name_args args = {
8803 .style = SECINFO_STYLE_CURRENT_FH,
8804 };
8805 struct nfs4_secinfo_res res = {
8806 .flavors = flavors,
8807 };
8808 struct rpc_message msg = {
8809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8810 .rpc_argp = &args,
8811 .rpc_resp = &res,
8812 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008813 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008814 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008815 int status;
8816
8817 if (use_integrity) {
8818 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008819 cred = nfs4_get_clid_cred(server->nfs_client);
8820 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008821 }
8822
8823 dprintk("--> %s\n", __func__);
8824 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8825 &res.seq_res, 0);
8826 dprintk("<-- %s status=%d\n", __func__, status);
8827
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008828 if (cred)
8829 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008830
8831 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008832}
8833
8834static int
8835nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8836 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8837{
8838 struct nfs4_exception exception = { };
8839 int err;
8840 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008841 /* first try using integrity protection */
8842 err = -NFS4ERR_WRONGSEC;
8843
8844 /* try to use integrity protection with machine cred */
8845 if (_nfs4_is_integrity_protected(server->nfs_client))
8846 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8847 flavors, true);
8848
8849 /*
8850 * if unable to use integrity protection, or SECINFO with
8851 * integrity protection returns NFS4ERR_WRONGSEC (which is
8852 * disallowed by spec, but exists in deployed servers) use
8853 * the current filesystem's rpc_client and the user cred.
8854 */
8855 if (err == -NFS4ERR_WRONGSEC)
8856 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8857 flavors, false);
8858
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008859 switch (err) {
8860 case 0:
8861 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008862 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008863 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008864 default:
8865 err = nfs4_handle_exception(server, err, &exception);
8866 }
8867 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008868out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008869 return err;
8870}
8871
8872static int
8873nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8874 struct nfs_fsinfo *info)
8875{
8876 int err;
8877 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008878 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008879 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008880 struct nfs4_secinfo4 *secinfo;
8881 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008882
8883 page = alloc_page(GFP_KERNEL);
8884 if (!page) {
8885 err = -ENOMEM;
8886 goto out;
8887 }
8888
8889 flavors = page_address(page);
8890 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8891
8892 /*
8893 * Fall back on "guess and check" method if
8894 * the server doesn't support SECINFO_NO_NAME
8895 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008896 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008897 err = nfs4_find_root_sec(server, fhandle, info);
8898 goto out_freepage;
8899 }
8900 if (err)
8901 goto out_freepage;
8902
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008903 for (i = 0; i < flavors->num_flavors; i++) {
8904 secinfo = &flavors->flavors[i];
8905
8906 switch (secinfo->flavor) {
8907 case RPC_AUTH_NULL:
8908 case RPC_AUTH_UNIX:
8909 case RPC_AUTH_GSS:
8910 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8911 &secinfo->flavor_info);
8912 break;
8913 default:
8914 flavor = RPC_AUTH_MAXFLAVOR;
8915 break;
8916 }
8917
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008918 if (!nfs_auth_info_match(&server->auth_info, flavor))
8919 flavor = RPC_AUTH_MAXFLAVOR;
8920
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008921 if (flavor != RPC_AUTH_MAXFLAVOR) {
8922 err = nfs4_lookup_root_sec(server, fhandle,
8923 info, flavor);
8924 if (!err)
8925 break;
8926 }
8927 }
8928
8929 if (flavor == RPC_AUTH_MAXFLAVOR)
8930 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008931
8932out_freepage:
8933 put_page(page);
8934 if (err == -EACCES)
8935 return -EPERM;
8936out:
8937 return err;
8938}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008939
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008940static int _nfs41_test_stateid(struct nfs_server *server,
8941 nfs4_stateid *stateid,
8942 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008943{
8944 int status;
8945 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008946 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008947 };
8948 struct nfs41_test_stateid_res res;
8949 struct rpc_message msg = {
8950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8951 .rpc_argp = &args,
8952 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008953 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008954 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008955 struct rpc_clnt *rpc_client = server->client;
8956
8957 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8958 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008959
Chuck Lever38527b12012-07-11 16:30:23 -04008960 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008961 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008962 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008963 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008964 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008965 if (status != NFS_OK) {
8966 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008967 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008968 }
8969 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008970 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008971}
8972
Trond Myklebust43912bb2016-09-22 13:38:56 -04008973static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8974 int err, struct nfs4_exception *exception)
8975{
8976 exception->retry = 0;
8977 switch(err) {
8978 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008979 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008980 nfs4_handle_exception(server, err, exception);
8981 break;
8982 case -NFS4ERR_BADSESSION:
8983 case -NFS4ERR_BADSLOT:
8984 case -NFS4ERR_BAD_HIGH_SLOT:
8985 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8986 case -NFS4ERR_DEADSESSION:
8987 nfs4_do_handle_exception(server, err, exception);
8988 }
8989}
8990
Chuck Lever38527b12012-07-11 16:30:23 -04008991/**
8992 * nfs41_test_stateid - perform a TEST_STATEID operation
8993 *
8994 * @server: server / transport on which to perform the operation
8995 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008996 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008997 *
8998 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8999 * Otherwise a negative NFS4ERR value is returned if the operation
9000 * failed or the state ID is not currently valid.
9001 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009002static int nfs41_test_stateid(struct nfs_server *server,
9003 nfs4_stateid *stateid,
9004 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009005{
9006 struct nfs4_exception exception = { };
9007 int err;
9008 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009009 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009010 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009011 } while (exception.retry);
9012 return err;
9013}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009014
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009015struct nfs_free_stateid_data {
9016 struct nfs_server *server;
9017 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009018 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009019};
9020
9021static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9022{
9023 struct nfs_free_stateid_data *data = calldata;
9024 nfs41_setup_sequence(nfs4_get_session(data->server),
9025 &data->args.seq_args,
9026 &data->res.seq_res,
9027 task);
9028}
9029
9030static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9031{
9032 struct nfs_free_stateid_data *data = calldata;
9033
9034 nfs41_sequence_done(task, &data->res.seq_res);
9035
9036 switch (task->tk_status) {
9037 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009038 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009039 rpc_restart_call_prepare(task);
9040 }
9041}
9042
9043static void nfs41_free_stateid_release(void *calldata)
9044{
9045 kfree(calldata);
9046}
9047
Trond Myklebust17f26b12013-08-21 15:48:42 -04009048static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009049 .rpc_call_prepare = nfs41_free_stateid_prepare,
9050 .rpc_call_done = nfs41_free_stateid_done,
9051 .rpc_release = nfs41_free_stateid_release,
9052};
9053
9054static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009055 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009056 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009057 bool privileged)
9058{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009059 struct rpc_message msg = {
9060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009061 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009062 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009063 struct rpc_task_setup task_setup = {
9064 .rpc_client = server->client,
9065 .rpc_message = &msg,
9066 .callback_ops = &nfs41_free_stateid_ops,
9067 .flags = RPC_TASK_ASYNC,
9068 };
9069 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009070
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009071 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9072 &task_setup.rpc_client, &msg);
9073
Chuck Lever38527b12012-07-11 16:30:23 -04009074 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009075 data = kmalloc(sizeof(*data), GFP_NOFS);
9076 if (!data)
9077 return ERR_PTR(-ENOMEM);
9078 data->server = server;
9079 nfs4_stateid_copy(&data->args.stateid, stateid);
9080
9081 task_setup.callback_data = data;
9082
9083 msg.rpc_argp = &data->args;
9084 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009085 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009086 if (privileged)
9087 nfs4_set_sequence_privileged(&data->args.seq_args);
9088
9089 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009090}
9091
Chuck Lever38527b12012-07-11 16:30:23 -04009092/**
9093 * nfs41_free_stateid - perform a FREE_STATEID operation
9094 *
9095 * @server: server / transport on which to perform the operation
9096 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009097 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009098 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009099 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009100 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009101 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009102static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009103 const nfs4_stateid *stateid,
9104 struct rpc_cred *cred,
9105 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009106{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009107 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009108
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009109 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009110 if (IS_ERR(task))
9111 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009112 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009113 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009114}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009115
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009116static void
9117nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009118{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009119 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009120
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009121 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009122 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009123}
9124
Trond Myklebust36281ca2012-03-04 18:13:56 -05009125static bool nfs41_match_stateid(const nfs4_stateid *s1,
9126 const nfs4_stateid *s2)
9127{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009128 if (s1->type != s2->type)
9129 return false;
9130
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009131 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009132 return false;
9133
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009134 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009135 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009136 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009137 return true;
9138
9139 return false;
9140}
9141
Andy Adamson557134a2009-04-01 09:21:53 -04009142#endif /* CONFIG_NFS_V4_1 */
9143
Trond Myklebust36281ca2012-03-04 18:13:56 -05009144static bool nfs4_match_stateid(const nfs4_stateid *s1,
9145 const nfs4_stateid *s2)
9146{
Trond Myklebustf597c532012-03-04 18:13:56 -05009147 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009148}
9149
9150
Trond Myklebust17280172012-03-11 13:11:00 -04009151static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009152 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009153 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 .recover_open = nfs4_open_reclaim,
9155 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009156 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009157 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158};
9159
Andy Adamson591d71c2009-04-01 09:22:47 -04009160#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009161static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009162 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9163 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9164 .recover_open = nfs4_open_reclaim,
9165 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009166 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009167 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009168 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009169};
9170#endif /* CONFIG_NFS_V4_1 */
9171
Trond Myklebust17280172012-03-11 13:11:00 -04009172static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009173 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009174 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009175 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009176 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009177 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178};
9179
Andy Adamson591d71c2009-04-01 09:22:47 -04009180#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009181static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009182 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9183 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009184 .recover_open = nfs41_open_expired,
9185 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009186 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009187};
9188#endif /* CONFIG_NFS_V4_1 */
9189
Trond Myklebust17280172012-03-11 13:11:00 -04009190static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009191 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009192 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009193 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009194};
9195
9196#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009197static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009198 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009199 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009200 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009201};
9202#endif
9203
Chuck Leverec011fe2013-10-17 14:12:39 -04009204static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009205 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009206 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009207};
9208
9209#if defined(CONFIG_NFS_V4_1)
9210static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009211 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009212 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009213};
9214#endif /* CONFIG_NFS_V4_1 */
9215
Trond Myklebust97dc1352010-06-16 09:52:26 -04009216static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9217 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009218 .init_caps = NFS_CAP_READDIRPLUS
9219 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009220 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009221 .init_client = nfs40_init_client,
9222 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009223 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009224 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009225 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009226 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009227 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009228 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009229 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9230 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9231 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009232 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009233};
9234
9235#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009236static struct nfs_seqid *
9237nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9238{
9239 return NULL;
9240}
9241
Trond Myklebust97dc1352010-06-16 09:52:26 -04009242static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9243 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009244 .init_caps = NFS_CAP_READDIRPLUS
9245 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009246 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009247 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009248 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009249 .init_client = nfs41_init_client,
9250 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009251 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009252 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009253 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009254 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009255 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009256 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009257 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009258 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9259 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9260 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009261 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009262};
9263#endif
9264
Steve Dickson42c2c422013-05-22 12:50:38 -04009265#if defined(CONFIG_NFS_V4_2)
9266static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9267 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009268 .init_caps = NFS_CAP_READDIRPLUS
9269 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009270 | NFS_CAP_POSIX_LOCK
9271 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009272 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009273 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009274 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009275 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009276 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009277 | NFS_CAP_LAYOUTSTATS
9278 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009279 .init_client = nfs41_init_client,
9280 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009281 .match_stateid = nfs41_match_stateid,
9282 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009283 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009284 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009285 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009286 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009287 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009288 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9289 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9290 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009291 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009292};
9293#endif
9294
Trond Myklebust97dc1352010-06-16 09:52:26 -04009295const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9296 [0] = &nfs_v4_0_minor_ops,
9297#if defined(CONFIG_NFS_V4_1)
9298 [1] = &nfs_v4_1_minor_ops,
9299#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009300#if defined(CONFIG_NFS_V4_2)
9301 [2] = &nfs_v4_2_minor_ops,
9302#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009303};
9304
Trond Myklebust13997822016-07-24 17:10:52 -04009305static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009306{
9307 ssize_t error, error2;
9308
9309 error = generic_listxattr(dentry, list, size);
9310 if (error < 0)
9311 return error;
9312 if (list) {
9313 list += error;
9314 size -= error;
9315 }
9316
9317 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9318 if (error2 < 0)
9319 return error2;
9320 return error + error2;
9321}
9322
Trond Myklebust17f26b12013-08-21 15:48:42 -04009323static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009324 .create = nfs_create,
9325 .lookup = nfs_lookup,
9326 .atomic_open = nfs_atomic_open,
9327 .link = nfs_link,
9328 .unlink = nfs_unlink,
9329 .symlink = nfs_symlink,
9330 .mkdir = nfs_mkdir,
9331 .rmdir = nfs_rmdir,
9332 .mknod = nfs_mknod,
9333 .rename = nfs_rename,
9334 .permission = nfs_permission,
9335 .getattr = nfs_getattr,
9336 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009337 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009338};
9339
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009340static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009341 .permission = nfs_permission,
9342 .getattr = nfs_getattr,
9343 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009344 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009345};
9346
David Howells509de812006-08-22 20:06:11 -04009347const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348 .version = 4, /* protocol version */
9349 .dentry_ops = &nfs4_dentry_operations,
9350 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009351 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009352 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009353 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009354 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009355 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356 .getattr = nfs4_proc_getattr,
9357 .setattr = nfs4_proc_setattr,
9358 .lookup = nfs4_proc_lookup,
9359 .access = nfs4_proc_access,
9360 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 .create = nfs4_proc_create,
9362 .remove = nfs4_proc_remove,
9363 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009364 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009366 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009367 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009368 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009369 .link = nfs4_proc_link,
9370 .symlink = nfs4_proc_symlink,
9371 .mkdir = nfs4_proc_mkdir,
9372 .rmdir = nfs4_proc_remove,
9373 .readdir = nfs4_proc_readdir,
9374 .mknod = nfs4_proc_mknod,
9375 .statfs = nfs4_proc_statfs,
9376 .fsinfo = nfs4_proc_fsinfo,
9377 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009378 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009379 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009380 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009382 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009384 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009386 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009387 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009389 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009390 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009391 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009392 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009393 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009394 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009395 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009396 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009397 .create_server = nfs4_create_server,
9398 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399};
9400
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009401static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009402 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009403 .list = nfs4_xattr_list_nfs4_acl,
9404 .get = nfs4_xattr_get_nfs4_acl,
9405 .set = nfs4_xattr_set_nfs4_acl,
9406};
9407
9408const struct xattr_handler *nfs4_xattr_handlers[] = {
9409 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009410#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9411 &nfs4_xattr_nfs4_label_handler,
9412#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009413 NULL
9414};
9415
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416/*
9417 * Local variables:
9418 * c-basic-offset: 8
9419 * End:
9420 */