blob: 632d3c3f8dfb35cda4bf70d15ce82d1c9c86cf48 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040096 struct nfs4_state *state, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
228 | FATTR4_WORD0_CHANGE
229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500258 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500263 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD1_OWNER_GROUP
265 | FATTR4_WORD1_RAWDEV
266 | FATTR4_WORD1_SPACE_USED
267 | FATTR4_WORD1_TIME_ACCESS
268 | FATTR4_WORD1_TIME_METADATA
269 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400270 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400271};
272
Al Virobc4785c2006-10-19 23:28:51 -0700273static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct nfs4_readdir_arg *readdir)
275{
Al Viro0dbb4c62006-10-19 23:28:49 -0700276 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000279 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
281 return;
282 }
283
284 readdir->cookie = 0;
285 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
286 if (cookie == 2)
287 return;
288
289 /*
290 * NFSv4 servers do not return entries for '.' and '..'
291 * Therefore, we fake these entries here. We let '.'
292 * have cookie 0 and '..' have cookie 1. Note that
293 * when talking to the server, we always send cookie 0
294 * instead of 1 or 2.
295 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800296 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (cookie == 0) {
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_one; /* cookie, second word */
302 *p++ = xdr_one; /* entry len */
303 memcpy(p, ".\0\0\0", 4); /* entry */
304 p++;
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000308 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 *p++ = xdr_one; /* next */
312 *p++ = xdr_zero; /* cookie, first word */
313 *p++ = xdr_two; /* cookie, second word */
314 *p++ = xdr_two; /* entry len */
315 memcpy(p, "..\0\0", 4); /* entry */
316 p++;
317 *p++ = xdr_one; /* bitmap length */
318 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
319 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000320 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 readdir->pgbase = (char *)p - (char *)start;
323 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800324 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Trond Myklebust26d36302016-09-22 13:39:05 -0400327static void nfs4_test_and_free_stateid(struct nfs_server *server,
328 nfs4_stateid *stateid,
329 struct rpc_cred *cred)
330{
331 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
332
333 ops->test_and_free_expired(server, stateid, cred);
334}
335
336static void __nfs4_free_revoked_stateid(struct nfs_server *server,
337 nfs4_stateid *stateid,
338 struct rpc_cred *cred)
339{
340 stateid->type = NFS4_REVOKED_STATEID_TYPE;
341 nfs4_test_and_free_stateid(server, stateid, cred);
342}
343
344static void nfs4_free_revoked_stateid(struct nfs_server *server,
345 const nfs4_stateid *stateid,
346 struct rpc_cred *cred)
347{
348 nfs4_stateid tmp;
349
350 nfs4_stateid_copy(&tmp, stateid);
351 __nfs4_free_revoked_stateid(server, &tmp, cred);
352}
353
NeilBrown8478eaa2014-09-18 16:09:27 +1000354static long nfs4_update_delay(long *timeout)
355{
356 long ret;
357 if (!timeout)
358 return NFS4_POLL_RETRY_MAX;
359 if (*timeout <= 0)
360 *timeout = NFS4_POLL_RETRY_MIN;
361 if (*timeout > NFS4_POLL_RETRY_MAX)
362 *timeout = NFS4_POLL_RETRY_MAX;
363 ret = *timeout;
364 *timeout <<= 1;
365 return ret;
366}
367
Trond Myklebust65de8722008-12-23 15:21:44 -0500368static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
369{
370 int res = 0;
371
372 might_sleep();
373
NeilBrown8478eaa2014-09-18 16:09:27 +1000374 freezable_schedule_timeout_killable_unsafe(
375 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500376 if (fatal_signal_pending(current))
377 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 return res;
379}
380
381/* This is the error handling routine for processes that are allowed
382 * to sleep.
383 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400384static int nfs4_do_handle_exception(struct nfs_server *server,
385 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500386{
387 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500388 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400389 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500390 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500391 int ret = errorcode;
392
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400393 exception->delay = 0;
394 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400396
397 if (stateid == NULL && state != NULL)
398 stateid = &state->stateid;
399
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 switch(errorcode) {
401 case 0:
402 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400403 case -NFS4ERR_DELEG_REVOKED:
404 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400405 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 if (inode != NULL && stateid != NULL) {
408 nfs_inode_find_state_and_recover(inode,
409 stateid);
410 goto wait_on_recovery;
411 }
412 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400413 if (inode) {
414 int err;
415
416 err = nfs_async_inode_return_delegation(inode,
417 stateid);
418 if (err == 0)
419 goto wait_on_recovery;
420 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
421 exception->retry = 1;
422 break;
423 }
424 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 if (state == NULL)
426 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400427 ret = nfs4_schedule_stateid_recovery(server, state);
428 if (ret < 0)
429 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500430 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500431 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500433 nfs4_schedule_lease_recovery(clp);
434 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400435 case -NFS4ERR_MOVED:
436 ret = nfs4_schedule_migration_recovery(server);
437 if (ret < 0)
438 break;
439 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400440 case -NFS4ERR_LEASE_MOVED:
441 nfs4_schedule_lease_moved_recovery(clp);
442 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500443#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400444 case -NFS4ERR_BADSESSION:
445 case -NFS4ERR_BADSLOT:
446 case -NFS4ERR_BAD_HIGH_SLOT:
447 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
448 case -NFS4ERR_DEADSESSION:
449 case -NFS4ERR_SEQ_FALSE_RETRY:
450 case -NFS4ERR_SEQ_MISORDERED:
451 dprintk("%s ERROR: %d Reset session\n", __func__,
452 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400453 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500457 if (exception->timeout > HZ) {
458 /* We have retried a decent amount, time to
459 * fail
460 */
461 ret = -EBUSY;
462 break;
463 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400465 nfs_inc_server_stats(server, NFSIOS_DELAY);
466 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400467 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400468 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400469 exception->delay = 1;
470 return 0;
471
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400472 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_OLD_STATEID:
474 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800475 break;
476 case -NFS4ERR_BADOWNER:
477 /* The following works around a Linux server bug! */
478 case -NFS4ERR_BADNAME:
479 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
480 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
481 exception->retry = 1;
482 printk(KERN_WARNING "NFS: v4 server %s "
483 "does not accept raw "
484 "uid/gids. "
485 "Reenabling the idmapper.\n",
486 server->nfs_client->cl_hostname);
487 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500488 }
489 /* We failed to handle the error */
490 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500491wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400492 exception->recovering = 1;
493 return 0;
494}
495
496/* This is the error handling routine for processes that are allowed
497 * to sleep.
498 */
499int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
500{
501 struct nfs_client *clp = server->nfs_client;
502 int ret;
503
504 ret = nfs4_do_handle_exception(server, errorcode, exception);
505 if (exception->delay) {
506 ret = nfs4_delay(server->client, &exception->timeout);
507 goto out_retry;
508 }
509 if (exception->recovering) {
510 ret = nfs4_wait_clnt_recover(clp);
511 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
512 return -EIO;
513 goto out_retry;
514 }
515 return ret;
516out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500520}
521
Trond Myklebust037fc982015-09-20 15:51:00 -0400522static int
523nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
524 int errorcode, struct nfs4_exception *exception)
525{
526 struct nfs_client *clp = server->nfs_client;
527 int ret;
528
529 ret = nfs4_do_handle_exception(server, errorcode, exception);
530 if (exception->delay) {
531 rpc_delay(task, nfs4_update_delay(&exception->timeout));
532 goto out_retry;
533 }
534 if (exception->recovering) {
535 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
536 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
537 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
538 goto out_retry;
539 }
540 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
541 ret = -EIO;
542 return ret;
543out_retry:
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500544 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400545 exception->retry = 1;
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500546 /*
547 * For NFS4ERR_MOVED, the client transport will need to
548 * be recomputed after migration recovery has completed.
549 */
550 if (errorcode == -NFS4ERR_MOVED)
551 rpc_task_release_transport(task);
552 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
585
586 if (flavor == RPC_AUTH_GSS_KRB5I ||
587 flavor == RPC_AUTH_GSS_KRB5P)
588 return true;
589
590 return false;
591}
Trond Myklebust65de8722008-12-23 15:21:44 -0500592
Trond Myklebust452e9352010-07-31 14:29:06 -0400593static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&clp->cl_lock);
596 if (time_before(clp->cl_last_renewal,timestamp))
597 clp->cl_last_renewal = timestamp;
598 spin_unlock(&clp->cl_lock);
599}
600
Trond Myklebust452e9352010-07-31 14:29:06 -0400601static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
602{
Trond Myklebustbe824162015-07-05 14:50:46 -0400603 struct nfs_client *clp = server->nfs_client;
604
605 if (!nfs4_has_session(clp))
606 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400607}
608
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400609struct nfs4_call_sync_data {
610 const struct nfs_server *seq_server;
611 struct nfs4_sequence_args *seq_args;
612 struct nfs4_sequence_res *seq_res;
613};
614
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800615void nfs4_init_sequence(struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400617{
618 args->sa_slot = NULL;
619 args->sa_cache_this = cache_reply;
620 args->sa_privileged = 0;
621
622 res->sr_slot = NULL;
623}
624
625static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
626{
627 args->sa_privileged = 1;
628}
629
Peng Taocb04ad22014-06-11 05:24:15 +0800630int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
631 struct nfs4_sequence_args *args,
632 struct nfs4_sequence_res *res,
633 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400634{
Chuck Lever3bd23842013-08-09 12:49:19 -0400635 struct nfs4_slot *slot;
636
637 /* slot already allocated? */
638 if (res->sr_slot != NULL)
639 goto out_start;
640
641 spin_lock(&tbl->slot_tbl_lock);
642 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
643 goto out_sleep;
644
645 slot = nfs4_alloc_slot(tbl);
646 if (IS_ERR(slot)) {
647 if (slot == ERR_PTR(-ENOMEM))
648 task->tk_timeout = HZ >> 2;
649 goto out_sleep;
650 }
651 spin_unlock(&tbl->slot_tbl_lock);
652
Trond Myklebust0a014a42016-09-22 13:38:51 -0400653 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400654 args->sa_slot = slot;
655 res->sr_slot = slot;
656
657out_start:
658 rpc_call_start(task);
659 return 0;
660
661out_sleep:
662 if (args->sa_privileged)
663 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
664 NULL, RPC_PRIORITY_PRIVILEGED);
665 else
666 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
667 spin_unlock(&tbl->slot_tbl_lock);
668 return -EAGAIN;
669}
Peng Taocb04ad22014-06-11 05:24:15 +0800670EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400671
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400672static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400673{
674 struct nfs4_slot *slot = res->sr_slot;
675 struct nfs4_slot_table *tbl;
676
Chuck Lever3bd23842013-08-09 12:49:19 -0400677 tbl = slot->table;
678 spin_lock(&tbl->slot_tbl_lock);
679 if (!nfs41_wake_and_assign_slot(tbl, slot))
680 nfs4_free_slot(tbl, slot);
681 spin_unlock(&tbl->slot_tbl_lock);
682
683 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400684}
685
686static int nfs40_sequence_done(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
688{
689 if (res->sr_slot != NULL)
690 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400691 return 1;
692}
693
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694#if defined(CONFIG_NFS_V4_1)
695
Trond Myklebustd185a332010-06-16 09:52:25 -0400696static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400699 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500701 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500705
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400706 /* Bump the slot sequence number */
707 if (slot->seq_done)
708 slot->seq_nr++;
709 slot->seq_done = 0;
710
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500711 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500712 /* Be nice to the server: try to ensure that the last transmitted
713 * value for highest_user_slotid <= target_highest_slotid
714 */
715 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
716 send_new_highest_used_slotid = true;
717
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719 send_new_highest_used_slotid = false;
720 goto out_unlock;
721 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500722 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723
724 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
725 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500726out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500727 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400728 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500729 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400730 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400731 if (waitqueue_active(&tbl->slot_waitq))
732 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400733}
734
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400735static int nfs41_sequence_process(struct rpc_task *task,
736 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400737{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500738 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500741 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500742 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400743
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500744 if (slot == NULL)
745 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400746 /* don't increment the sequence number if the task wasn't sent */
747 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400748 goto out;
749
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500750 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500751
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 if (slot->interrupted) {
753 slot->interrupted = 0;
754 interrupted = true;
755 }
756
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400757 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500758 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400759 switch (res->sr_status) {
760 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400761 /* If previous op on slot was interrupted and we reused
762 * the seq# and got a reply from the cache, then retry
763 */
764 if (task->tk_status == -EREMOTEIO && interrupted) {
765 ++slot->seq_nr;
766 goto retry_nowait;
767 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400768 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400769 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500770 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500771 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500772 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400773 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
774 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500775 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500777 case 1:
778 /*
779 * sr_status remains 1 if an RPC level error occurred.
780 * The server may or may not have processed the sequence
781 * operation..
782 * Mark the slot as having hosted an interrupted RPC call.
783 */
784 slot->interrupted = 1;
785 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400786 case -NFS4ERR_DELAY:
787 /* The server detected a resend of the RPC call and
788 * returned NFS4ERR_DELAY as per Section 2.10.6.2
789 * of RFC5661.
790 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500791 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400792 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500793 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500794 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400795 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500796 case -NFS4ERR_BADSLOT:
797 /*
798 * The slot id we used was probably retired. Try again
799 * using a different slot id.
800 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500801 goto retry_nowait;
802 case -NFS4ERR_SEQ_MISORDERED:
803 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500804 * Was the last operation on this sequence interrupted?
805 * If so, retry after bumping the sequence number.
806 */
807 if (interrupted) {
808 ++slot->seq_nr;
809 goto retry_nowait;
810 }
811 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500812 * Could this slot have been previously retired?
813 * If so, then the server may be expecting seq_nr = 1!
814 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500815 if (slot->seq_nr != 1) {
816 slot->seq_nr = 1;
817 goto retry_nowait;
818 }
819 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500820 case -NFS4ERR_SEQ_FALSE_RETRY:
821 ++slot->seq_nr;
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168a2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168a2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001112 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1115 struct iattr attrs;
1116 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001118 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001119 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int cancelled;
1122};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001124struct nfs4_open_createattrs {
1125 struct nfs4_label *label;
1126 struct iattr *sattr;
1127 const __u32 verf[2];
1128};
1129
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001130static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1131 int err, struct nfs4_exception *exception)
1132{
1133 if (err != -EINVAL)
1134 return false;
1135 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1136 return false;
1137 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1138 exception->retry = 1;
1139 return true;
1140}
1141
Trond Myklebust6ae37332015-01-30 14:21:14 -05001142static u32
1143nfs4_map_atomic_open_share(struct nfs_server *server,
1144 fmode_t fmode, int openflags)
1145{
1146 u32 res = 0;
1147
1148 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1149 case FMODE_READ:
1150 res = NFS4_SHARE_ACCESS_READ;
1151 break;
1152 case FMODE_WRITE:
1153 res = NFS4_SHARE_ACCESS_WRITE;
1154 break;
1155 case FMODE_READ|FMODE_WRITE:
1156 res = NFS4_SHARE_ACCESS_BOTH;
1157 }
1158 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1159 goto out;
1160 /* Want no delegation if we're using O_DIRECT */
1161 if (openflags & O_DIRECT)
1162 res |= NFS4_SHARE_WANT_NO_DELEG;
1163out:
1164 return res;
1165}
1166
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001167static enum open_claim_type4
1168nfs4_map_atomic_open_claim(struct nfs_server *server,
1169 enum open_claim_type4 claim)
1170{
1171 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1172 return claim;
1173 switch (claim) {
1174 default:
1175 return claim;
1176 case NFS4_OPEN_CLAIM_FH:
1177 return NFS4_OPEN_CLAIM_NULL;
1178 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1179 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1180 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1182 }
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186{
1187 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001188 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->o_res.seqid = p->o_arg.seqid;
1190 p->c_res.seqid = p->c_arg.seqid;
1191 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001192 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001194 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195}
1196
Al Viro82a2c1b2011-06-22 18:30:55 -04001197static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001199 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001200 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001201 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202{
Al Viro82a2c1b2011-06-22 18:30:55 -04001203 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001204 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001207 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208 struct nfs4_opendata *p;
1209
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 if (p == NULL)
1212 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001213
1214 p->f_label = nfs4_label_alloc(server, gfp_mask);
1215 if (IS_ERR(p->f_label))
1216 goto err_free_p;
1217
Kinglong Meea49c2692015-07-27 15:31:38 +08001218 p->a_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->a_label))
1220 goto err_free_f;
1221
Trond Myklebust63f5f792015-01-23 19:19:25 -05001222 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1223 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001224 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001225 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001226 nfs_sb_active(dentry->d_sb);
1227 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 p->dir = parent;
1229 p->owner = sp;
1230 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001231 p->o_arg.open_flags = flags;
1232 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001233 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1234 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001235 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1236 * will return permission denied for all bits until close */
1237 if (!(flags & O_EXCL)) {
1238 /* ask server to check for all possible rights as results
1239 * are cached */
1240 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1241 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001242 }
David Howells7539bba2006-08-22 20:06:09 -04001243 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001244 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1245 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001246 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001248 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001249 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001250 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001251 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1252 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001253 case NFS4_OPEN_CLAIM_NULL:
1254 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1255 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1256 p->o_arg.fh = NFS_FH(dir);
1257 break;
1258 case NFS4_OPEN_CLAIM_PREVIOUS:
1259 case NFS4_OPEN_CLAIM_FH:
1260 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1261 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001262 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001263 }
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001264 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001266 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001267
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001268 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001269 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001271 p->c_arg.fh = &p->o_res.fh;
1272 p->c_arg.stateid = &p->o_res.stateid;
1273 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001274 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001277
1278err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001279 nfs4_label_free(p->a_label);
1280err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001281 nfs4_label_free(p->f_label);
1282err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 kfree(p);
1284err:
1285 dput(parent);
1286 return NULL;
1287}
1288
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001291 struct nfs4_opendata *p = container_of(kref,
1292 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001293 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001294
1295 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001296 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001297 if (p->state != NULL)
1298 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001299 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001300
Kinglong Meea49c2692015-07-27 15:31:38 +08001301 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001302 nfs4_label_free(p->f_label);
1303
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001305 dput(p->dentry);
1306 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001307 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001308 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 kfree(p);
1310}
1311
1312static void nfs4_opendata_put(struct nfs4_opendata *p)
1313{
1314 if (p != NULL)
1315 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316}
1317
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1319{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001320 int ret;
1321
Trond Myklebust06f814a2006-01-03 09:55:07 +01001322 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001323 return ret;
1324}
1325
Trond Myklebust24311f82015-09-20 10:50:17 -04001326static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1327 fmode_t fmode)
1328{
1329 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1330 case FMODE_READ|FMODE_WRITE:
1331 return state->n_rdwr != 0;
1332 case FMODE_WRITE:
1333 return state->n_wronly != 0;
1334 case FMODE_READ:
1335 return state->n_rdonly != 0;
1336 }
1337 WARN_ON_ONCE(1);
1338 return false;
1339}
1340
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001342{
1343 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344
Trond Myklebust536e43d2012-01-17 22:04:26 -05001345 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 goto out;
1347 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001348 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001349 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1350 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001351 break;
1352 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001353 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1354 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001355 break;
1356 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001357 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1358 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 return ret;
1362}
1363
Trond Myklebust2a606182015-08-19 22:30:00 -05001364static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1365 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001367 if (delegation == NULL)
1368 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001369 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370 return 0;
Trond 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;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001629 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001630 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001631 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001632 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001633 nfs4_stateid stateid;
1634 int ret = -EAGAIN;
1635
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001637 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001638 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001639 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001640 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001641 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001642 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001643 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001644 rcu_read_lock();
Trond Myklebust7fe8d152019-10-31 18:40:32 -04001645 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001646 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001647 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001648 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001649 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001651 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001652 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001653 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001654 if (!opendata->is_recover) {
1655 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1656 if (ret != 0)
1657 goto out;
1658 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001659 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001660
1661 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001662 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001663 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001664 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001665out:
1666 return ERR_PTR(ret);
1667out_return_state:
1668 atomic_inc(&state->count);
1669 return state;
1670}
1671
Andy Adamsone23008e2012-10-02 21:07:32 -04001672static void
1673nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1674{
1675 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1676 struct nfs_delegation *delegation;
1677 int delegation_flags = 0;
1678
1679 rcu_read_lock();
1680 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1681 if (delegation)
1682 delegation_flags = delegation->flags;
1683 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001684 switch (data->o_arg.claim) {
1685 default:
1686 break;
1687 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1688 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001689 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1690 "returning a delegation for "
1691 "OPEN(CLAIM_DELEGATE_CUR)\n",
1692 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001693 return;
1694 }
1695 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001696 nfs_inode_set_delegation(state->inode,
1697 data->owner->so_cred,
1698 &data->o_res);
1699 else
1700 nfs_inode_reclaim_delegation(state->inode,
1701 data->owner->so_cred,
1702 &data->o_res);
1703}
1704
1705/*
1706 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1707 * and update the nfs4_state.
1708 */
1709static struct nfs4_state *
1710_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1711{
1712 struct inode *inode = data->state->inode;
1713 struct nfs4_state *state = data->state;
1714 int ret;
1715
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001716 if (!data->rpc_done) {
1717 if (data->rpc_status) {
1718 ret = data->rpc_status;
1719 goto err;
1720 }
1721 /* cached opens have already been processed */
1722 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001723 }
1724
Andy Adamsone23008e2012-10-02 21:07:32 -04001725 ret = nfs_refresh_inode(inode, &data->f_attr);
1726 if (ret)
1727 goto err;
1728
1729 if (data->o_res.delegation_type != 0)
1730 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001731update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001732 update_open_stateid(state, &data->o_res.stateid, NULL,
1733 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001734 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001735
1736 return state;
1737err:
1738 return ERR_PTR(ret);
1739
1740}
1741
1742static struct nfs4_state *
1743_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001744{
1745 struct inode *inode;
1746 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001747 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001748
Trond Myklebustaac00a82007-07-05 19:02:21 -04001749 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001750 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001751 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001752 goto out;
1753 }
1754
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001755 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001756 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001757 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001758 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001759 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001760 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001761 goto err;
1762 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001763 state = nfs4_get_open_state(inode, data->owner);
1764 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001765 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001766 if (data->o_res.delegation_type != 0)
1767 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001768 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001769 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001770 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001771out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001772 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001774err_put_inode:
1775 iput(inode);
1776err:
1777 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778}
1779
Andy Adamsone23008e2012-10-02 21:07:32 -04001780static struct nfs4_state *
1781nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1782{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001783 struct nfs4_state *ret;
1784
Andy Adamsone23008e2012-10-02 21:07:32 -04001785 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001786 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1787 else
1788 ret = _nfs4_opendata_to_nfs4_state(data);
1789 nfs4_sequence_free_slot(&data->o_res.seq_res);
1790 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001791}
1792
Trond Myklebust864472e2006-01-03 09:55:15 +01001793static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1794{
1795 struct nfs_inode *nfsi = NFS_I(state->inode);
1796 struct nfs_open_context *ctx;
1797
1798 spin_lock(&state->inode->i_lock);
1799 list_for_each_entry(ctx, &nfsi->open_files, list) {
1800 if (ctx->state != state)
1801 continue;
1802 get_nfs_open_context(ctx);
1803 spin_unlock(&state->inode->i_lock);
1804 return ctx;
1805 }
1806 spin_unlock(&state->inode->i_lock);
1807 return ERR_PTR(-ENOENT);
1808}
1809
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001810static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1811 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001812{
1813 struct nfs4_opendata *opendata;
1814
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001815 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001816 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001817 if (opendata == NULL)
1818 return ERR_PTR(-ENOMEM);
1819 opendata->state = state;
1820 atomic_inc(&state->count);
1821 return opendata;
1822}
1823
Trond Myklebust24311f82015-09-20 10:50:17 -04001824static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1825 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001826{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001827 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001828 int ret;
1829
Trond Myklebust24311f82015-09-20 10:50:17 -04001830 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001831 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001832 opendata->o_arg.open_flags = 0;
1833 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001834 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1835 NFS_SB(opendata->dentry->d_sb),
1836 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001837 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1838 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1839 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001840 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001841 if (ret != 0)
1842 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001843 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001844 if (IS_ERR(newstate))
1845 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001846 if (newstate != opendata->state)
1847 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001848 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001849 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001850}
1851
1852static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1853{
Trond Myklebust864472e2006-01-03 09:55:15 +01001854 int ret;
1855
Trond Myklebust4f14c192014-02-12 19:15:06 -05001856 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1857 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1858 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1859 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001860 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001861 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001862 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001864 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1865 if (ret != 0)
1866 return ret;
1867 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1868 if (ret != 0)
1869 return ret;
1870 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1871 if (ret != 0)
1872 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001873 /*
1874 * We may have performed cached opens for all three recoveries.
1875 * Check if we need to update the current stateid.
1876 */
1877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001878 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001879 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001880 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001881 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001882 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001883 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001884 return 0;
1885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887/*
1888 * OPEN_RECLAIM:
1889 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001891static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001893 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001894 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001895 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 int status;
1897
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001898 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1899 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001900 if (IS_ERR(opendata))
1901 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001902 rcu_read_lock();
1903 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001904 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001905 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001906 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001907 opendata->o_arg.u.delegation_type = delegation_type;
1908 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001909 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return status;
1911}
1912
Trond Myklebust539cd032007-06-05 11:46:42 -04001913static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 struct nfs_server *server = NFS_SERVER(state->inode);
1916 struct nfs4_exception exception = { };
1917 int err;
1918 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001919 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001920 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001921 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1922 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001923 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001924 break;
1925 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 } while (exception.retry);
1927 return err;
1928}
1929
Trond Myklebust864472e2006-01-03 09:55:15 +01001930static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1931{
1932 struct nfs_open_context *ctx;
1933 int ret;
1934
1935 ctx = nfs4_state_find_open_context(state);
1936 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001937 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001938 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001939 put_nfs_open_context(ctx);
1940 return ret;
1941}
1942
NeilBrown70f254a2017-12-13 09:57:09 +11001943static 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 -07001944{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001945 switch (err) {
1946 default:
1947 printk(KERN_ERR "NFS: %s: unhandled error "
1948 "%d.\n", __func__, err);
1949 case 0:
1950 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001951 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001952 case -ESTALE:
1953 break;
1954 case -NFS4ERR_BADSESSION:
1955 case -NFS4ERR_BADSLOT:
1956 case -NFS4ERR_BAD_HIGH_SLOT:
1957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1958 case -NFS4ERR_DEADSESSION:
1959 set_bit(NFS_DELEGATED_STATE, &state->flags);
1960 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1961 return -EAGAIN;
1962 case -NFS4ERR_STALE_CLIENTID:
1963 case -NFS4ERR_STALE_STATEID:
1964 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001965 /* Don't recall a delegation if it was lost */
1966 nfs4_schedule_lease_recovery(server->nfs_client);
1967 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001968 case -NFS4ERR_MOVED:
1969 nfs4_schedule_migration_recovery(server);
1970 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001971 case -NFS4ERR_LEASE_MOVED:
1972 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1973 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001974 case -NFS4ERR_DELEG_REVOKED:
1975 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001976 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001977 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001978 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001979 nfs_inode_find_state_and_recover(state->inode,
1980 stateid);
1981 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001982 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001983 case -NFS4ERR_DELAY:
1984 case -NFS4ERR_GRACE:
1985 set_bit(NFS_DELEGATED_STATE, &state->flags);
1986 ssleep(1);
1987 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001988 case -ENOMEM:
1989 case -NFS4ERR_DENIED:
NeilBrown70f254a2017-12-13 09:57:09 +11001990 if (fl) {
1991 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
1992 if (lsp)
1993 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1994 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001995 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return err;
1998}
1999
Trond Myklebust24311f82015-09-20 10:50:17 -04002000int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2001 struct nfs4_state *state, const nfs4_stateid *stateid,
2002 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002003{
2004 struct nfs_server *server = NFS_SERVER(state->inode);
2005 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002006 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002007
2008 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2009 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2010 if (IS_ERR(opendata))
2011 return PTR_ERR(opendata);
2012 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002013 write_seqlock(&state->seqlock);
2014 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2015 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002016 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2017 switch (type & (FMODE_READ|FMODE_WRITE)) {
2018 case FMODE_READ|FMODE_WRITE:
2019 case FMODE_WRITE:
2020 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2021 if (err)
2022 break;
2023 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2024 if (err)
2025 break;
2026 case FMODE_READ:
2027 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2028 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002029 nfs4_opendata_put(opendata);
NeilBrown70f254a2017-12-13 09:57:09 +11002030 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002031}
2032
Chuck Leverbe05c862013-08-09 12:49:47 -04002033static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2034{
2035 struct nfs4_opendata *data = calldata;
2036
Peng Taocb04ad22014-06-11 05:24:15 +08002037 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2038 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002039}
2040
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002041static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2042{
2043 struct nfs4_opendata *data = calldata;
2044
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002045 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002046
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002047 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002048 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002049 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002050 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002051 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002052 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002053 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002054}
2055
2056static void nfs4_open_confirm_release(void *calldata)
2057{
2058 struct nfs4_opendata *data = calldata;
2059 struct nfs4_state *state = NULL;
2060
2061 /* If this request hasn't been cancelled, do nothing */
2062 if (data->cancelled == 0)
2063 goto out_free;
2064 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002065 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002066 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002067 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002068 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002069 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002070out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002071 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002072}
2073
2074static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002075 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002076 .rpc_call_done = nfs4_open_confirm_done,
2077 .rpc_release = nfs4_open_confirm_release,
2078};
2079
2080/*
2081 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2082 */
2083static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2084{
David Howells2b0143b2015-03-17 22:25:59 +00002085 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002086 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002087 struct rpc_message msg = {
2088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2089 .rpc_argp = &data->c_arg,
2090 .rpc_resp = &data->c_res,
2091 .rpc_cred = data->owner->so_cred,
2092 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002093 struct rpc_task_setup task_setup_data = {
2094 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002095 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002096 .callback_ops = &nfs4_open_confirm_ops,
2097 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002098 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002099 .flags = RPC_TASK_ASYNC,
2100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 int status;
2102
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002103 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002104 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002105 data->rpc_done = 0;
2106 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002107 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002108 if (data->is_recover)
2109 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002110 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002111 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002112 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002113 status = nfs4_wait_for_completion_rpc_task(task);
2114 if (status != 0) {
2115 data->cancelled = 1;
2116 smp_wmb();
2117 } else
2118 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002119 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return status;
2121}
2122
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002123static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002125 struct nfs4_opendata *data = calldata;
2126 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002127 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002128 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002129
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002131 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002132 /*
2133 * Check if we still need to send an OPEN call, or if we can use
2134 * a delegation instead.
2135 */
2136 if (data->state != NULL) {
2137 struct nfs_delegation *delegation;
2138
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002139 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002140 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002141 rcu_read_lock();
Trond Myklebust7fe8d152019-10-31 18:40:32 -04002142 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002143 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002144 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002145 rcu_read_unlock();
2146 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002147 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002148 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002149 switch (claim) {
2150 default:
2151 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002152 case NFS4_OPEN_CLAIM_PREVIOUS:
2153 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2154 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002155 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002156 case NFS4_OPEN_CLAIM_FH:
2157 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002158 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2159 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002160 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002161 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002162 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002163 &data->o_res.seq_res,
2164 task) != 0)
2165 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002166
2167 /* Set the create mode (note dependency on the session type) */
2168 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2169 if (data->o_arg.open_flags & O_EXCL) {
2170 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2171 if (nfs4_has_persistent_session(clp))
2172 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2173 else if (clp->cl_mvops->minor_version > 0)
2174 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2175 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002176 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002177unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002178 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002179 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002180out_no_action:
2181 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002182out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002183 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002184}
2185
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002186static void nfs4_open_done(struct rpc_task *task, void *calldata)
2187{
2188 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002190 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002191
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002192 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002193 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002194
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002195 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002196 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2197 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002198 case S_IFREG:
2199 break;
2200 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002202 break;
2203 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002205 break;
2206 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002208 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002209 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002210 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002211 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2212 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002213 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002214 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002215}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002216
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217static void nfs4_open_release(void *calldata)
2218{
2219 struct nfs4_opendata *data = calldata;
2220 struct nfs4_state *state = NULL;
2221
2222 /* If this request hasn't been cancelled, do nothing */
2223 if (data->cancelled == 0)
2224 goto out_free;
2225 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002226 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002227 goto out_free;
2228 /* In case we need an open_confirm, no cleanup! */
2229 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2230 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002231 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002232 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002233 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002234out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002235 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002236}
2237
2238static const struct rpc_call_ops nfs4_open_ops = {
2239 .rpc_call_prepare = nfs4_open_prepare,
2240 .rpc_call_done = nfs4_open_done,
2241 .rpc_release = nfs4_open_release,
2242};
2243
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002244static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002245{
David Howells2b0143b2015-03-17 22:25:59 +00002246 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002247 struct nfs_server *server = NFS_SERVER(dir);
2248 struct nfs_openargs *o_arg = &data->o_arg;
2249 struct nfs_openres *o_res = &data->o_res;
2250 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002251 struct rpc_message msg = {
2252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2253 .rpc_argp = o_arg,
2254 .rpc_resp = o_res,
2255 .rpc_cred = data->owner->so_cred,
2256 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002257 struct rpc_task_setup task_setup_data = {
2258 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002259 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002260 .callback_ops = &nfs4_open_ops,
2261 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002262 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 .flags = RPC_TASK_ASYNC,
2264 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002265 int status;
2266
Chuck Levera9c92d62013-08-09 12:48:18 -04002267 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002268 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002269 data->rpc_done = 0;
2270 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002271 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002272 data->is_recover = 0;
2273 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002274 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002275 data->is_recover = 1;
2276 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002277 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002278 if (IS_ERR(task))
2279 return PTR_ERR(task);
2280 status = nfs4_wait_for_completion_rpc_task(task);
2281 if (status != 0) {
2282 data->cancelled = 1;
2283 smp_wmb();
2284 } else
2285 status = data->rpc_status;
2286 rpc_put_task(task);
2287
2288 return status;
2289}
2290
2291static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2292{
David Howells2b0143b2015-03-17 22:25:59 +00002293 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002294 struct nfs_openres *o_res = &data->o_res;
2295 int status;
2296
2297 status = nfs4_run_open_task(data, 1);
2298 if (status != 0 || !data->rpc_done)
2299 return status;
2300
Trond Myklebust6926afd2012-01-07 13:22:46 -05002301 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2302
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002303 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2304 status = _nfs4_proc_open_confirm(data);
2305 if (status != 0)
2306 return status;
2307 }
2308
2309 return status;
2310}
2311
Trond Myklebustf3792d62014-07-10 08:54:32 -04002312/*
2313 * Additional permission checks in order to distinguish between an
2314 * open for read, and an open for execute. This works around the
2315 * fact that NFSv4 OPEN treats read and execute permissions as being
2316 * the same.
2317 * Note that in the non-execute case, we want to turn off permission
2318 * checking if we just created a new file (POSIX open() semantics).
2319 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002320static int nfs4_opendata_access(struct rpc_cred *cred,
2321 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002322 struct nfs4_state *state, fmode_t fmode,
2323 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002324{
2325 struct nfs_access_entry cache;
2326 u32 mask;
2327
2328 /* access call failed or for some reason the server doesn't
2329 * support any access modes -- defer access call until later */
2330 if (opendata->o_res.access_supported == 0)
2331 return 0;
2332
2333 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002334 /*
2335 * Use openflags to check for exec, because fmode won't
2336 * always have FMODE_EXEC set when file open for exec.
2337 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002338 if (openflags & __FMODE_EXEC) {
2339 /* ONLY check for exec rights */
2340 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002341 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002342 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002343
2344 cache.cred = cred;
2345 cache.jiffies = jiffies;
2346 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2347 nfs_access_add_cache(state->inode, &cache);
2348
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002349 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002350 return 0;
2351
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002352 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002353}
2354
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002355/*
2356 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2357 */
2358static int _nfs4_proc_open(struct nfs4_opendata *data)
2359{
David Howells2b0143b2015-03-17 22:25:59 +00002360 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002361 struct nfs_server *server = NFS_SERVER(dir);
2362 struct nfs_openargs *o_arg = &data->o_arg;
2363 struct nfs_openres *o_res = &data->o_res;
2364 int status;
2365
2366 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002367 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002368 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002369 if (status != 0) {
2370 if (status == -NFS4ERR_BADNAME &&
2371 !(o_arg->open_flags & O_CREAT))
2372 return -ENOENT;
2373 return status;
2374 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002375
Trond Myklebust6926afd2012-01-07 13:22:46 -05002376 nfs_fattr_map_and_free_names(server, &data->f_attr);
2377
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002378 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002379 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002380 if (o_arg->open_flags & O_EXCL)
2381 data->file_created = 1;
2382 else if (o_res->cinfo.before != o_res->cinfo.after)
2383 data->file_created = 1;
2384 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002385 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2386 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002388 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002390 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002392 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2393 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002394 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002395 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
Andy Adamsond83217c2011-03-01 01:34:17 +00002399static int nfs4_recover_expired_lease(struct nfs_server *server)
2400{
2401 return nfs4_client_recover_expired_lease(server->nfs_client);
2402}
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404/*
2405 * OPEN_EXPIRED:
2406 * reclaim state on the server after a network partition.
2407 * Assumes caller holds the appropriate lock
2408 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002409static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002411 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002412 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002414 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002415 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002416 if (IS_ERR(opendata))
2417 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002418 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002419 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002420 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002421 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002422 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002425static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002426{
Trond Myklebust539cd032007-06-05 11:46:42 -04002427 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002428 struct nfs4_exception exception = { };
2429 int err;
2430
2431 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002432 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002433 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002434 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2435 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002436 switch (err) {
2437 default:
2438 goto out;
2439 case -NFS4ERR_GRACE:
2440 case -NFS4ERR_DELAY:
2441 nfs4_handle_exception(server, err, &exception);
2442 err = 0;
2443 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002444 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002445out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002446 return err;
2447}
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002452 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Trond Myklebust864472e2006-01-03 09:55:15 +01002454 ctx = nfs4_state_find_open_context(state);
2455 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002456 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002457 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002458 put_nfs_open_context(ctx);
2459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Trond Myklebust41020b62016-09-22 13:38:58 -04002462static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2463 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002464{
Trond Myklebust41020b62016-09-22 13:38:58 -04002465 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002466 write_seqlock(&state->seqlock);
2467 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2468 write_sequnlock(&state->seqlock);
2469 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2470}
2471
2472static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2473{
2474 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002475 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002476}
2477
2478static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2479{
2480 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2481 nfs40_clear_delegation_stateid(state);
2482 return nfs4_open_expired(sp, state);
2483}
2484
Trond Myklebust45870d62016-09-22 13:38:59 -04002485static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2486 nfs4_stateid *stateid,
2487 struct rpc_cred *cred)
2488{
2489 return -NFS4ERR_BAD_STATEID;
2490}
2491
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002492#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002493static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2494 nfs4_stateid *stateid,
2495 struct rpc_cred *cred)
2496{
2497 int status;
2498
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002499 switch (stateid->type) {
2500 default:
2501 break;
2502 case NFS4_INVALID_STATEID_TYPE:
2503 case NFS4_SPECIAL_STATEID_TYPE:
2504 return -NFS4ERR_BAD_STATEID;
2505 case NFS4_REVOKED_STATEID_TYPE:
2506 goto out_free;
2507 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002508
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002509 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002510 switch (status) {
2511 case -NFS4ERR_EXPIRED:
2512 case -NFS4ERR_ADMIN_REVOKED:
2513 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002514 break;
2515 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002516 return status;
2517 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002518out_free:
2519 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002520 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002521 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002522}
2523
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002524static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002525{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002526 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002527 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002528 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002529 struct rpc_cred *cred;
2530 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002531
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002532 /* Get the delegation credential for use by test/free_stateid */
2533 rcu_read_lock();
2534 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002535 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002536 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002537 return;
2538 }
2539
2540 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaf412412018-09-05 14:07:14 -04002541 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002542 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002543 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002544 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002545 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002546
Trond Myklebustaf412412018-09-05 14:07:14 -04002547 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2548 &delegation->flags)) {
2549 rcu_read_unlock();
2550 return;
2551 }
2552
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002553 cred = get_rpccred(delegation->cred);
2554 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002555 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002556 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002557 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002558 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002559
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002560 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002561}
2562
2563/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002564 * nfs41_check_expired_locks - possibly free a lock stateid
2565 *
2566 * @state: NFSv4 state for an inode
2567 *
2568 * Returns NFS_OK if recovery for this stateid is now finished.
2569 * Otherwise a negative NFS4ERR value is returned.
2570 */
2571static int nfs41_check_expired_locks(struct nfs4_state *state)
2572{
2573 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002574 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002575 struct nfs_server *server = NFS_SERVER(state->inode);
2576
2577 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2578 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002579
2580 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002581 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2582 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2583 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2584
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002585 atomic_inc(&lsp->ls_count);
2586 spin_unlock(&state->state_lock);
2587
2588 nfs4_put_lock_state(prev);
2589 prev = lsp;
2590
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002591 status = nfs41_test_and_free_expired_stateid(server,
2592 &lsp->ls_stateid,
2593 cred);
2594 trace_nfs4_test_lock_stateid(state, lsp, status);
2595 if (status == -NFS4ERR_EXPIRED ||
2596 status == -NFS4ERR_BAD_STATEID) {
2597 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002598 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002599 if (!recover_lost_locks)
2600 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2601 } else if (status != NFS_OK) {
2602 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002603 nfs4_put_lock_state(prev);
2604 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002605 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002606 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002607 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002608 }
2609 spin_unlock(&state->state_lock);
2610 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002611out:
2612 return ret;
2613}
2614
2615/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002616 * nfs41_check_open_stateid - possibly free an open stateid
2617 *
2618 * @state: NFSv4 state for an inode
2619 *
2620 * Returns NFS_OK if recovery for this stateid is now finished.
2621 * Otherwise a negative NFS4ERR value is returned.
2622 */
2623static int nfs41_check_open_stateid(struct nfs4_state *state)
2624{
2625 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002626 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002627 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628 int status;
2629
Trond Myklebustb134fc42016-09-22 13:39:16 -04002630 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002631 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2632 if (nfs4_have_delegation(state->inode, state->state))
2633 return NFS_OK;
2634 return -NFS4ERR_OPENMODE;
2635 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002636 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002637 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002638 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002639 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002640 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002641 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2642 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2643 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002644 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002645 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002646 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002647 if (status != NFS_OK)
2648 return status;
2649 if (nfs_open_stateid_recover_openmode(state))
2650 return -NFS4ERR_OPENMODE;
2651 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002652}
2653
2654static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2655{
Chuck Levereb64cf92012-07-11 16:30:05 -04002656 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002657
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002658 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002659 status = nfs41_check_expired_locks(state);
2660 if (status != NFS_OK)
2661 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002662 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002663 if (status != NFS_OK)
2664 status = nfs4_open_expired(sp, state);
2665 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002666}
2667#endif
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002670 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2671 * fields corresponding to attributes that were used to store the verifier.
2672 * Make sure we clobber those fields in the later setattr call
2673 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002674static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2675 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002676{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002677 const u32 *attrset = opendata->o_res.attrset;
2678
2679 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002680 !(sattr->ia_valid & ATTR_ATIME_SET))
2681 sattr->ia_valid |= ATTR_ATIME;
2682
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002683 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002684 !(sattr->ia_valid & ATTR_MTIME_SET))
2685 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002686
2687 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddington56379492017-01-24 11:34:20 -05002688 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2689 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002690 sattr->ia_valid &= ~ATTR_MODE;
2691
2692 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2693 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002694}
2695
Trond Myklebustc21443c2013-02-07 14:26:21 -05002696static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2697 fmode_t fmode,
2698 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002699 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002700{
2701 struct nfs4_state_owner *sp = opendata->owner;
2702 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002703 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002704 struct nfs4_state *state;
2705 unsigned int seq;
2706 int ret;
2707
2708 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2709
2710 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002711 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002712 goto out;
2713
Trond Myklebustb05c4602018-05-22 11:17:16 -04002714 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715 ret = PTR_ERR(state);
2716 if (IS_ERR(state))
2717 goto out;
Trond Myklebust04653392017-02-08 11:29:46 -05002718 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002719 if (server->caps & NFS_CAP_POSIX_LOCK)
2720 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002721 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2722 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002723
Trond Myklebust275bb302013-05-29 13:11:28 -04002724 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002725 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002726 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002727 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002728 alias = d_exact_alias(dentry, state->inode);
2729 if (!alias)
2730 alias = d_splice_alias(igrab(state->inode), dentry);
2731 /* d_splice_alias() can't fail here - it's a non-directory */
2732 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002733 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002734 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002735 }
2736 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002737 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002738 }
2739
Trond Myklebustc21443c2013-02-07 14:26:21 -05002740 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2741 if (ret != 0)
2742 goto out;
2743
David Howells2b0143b2015-03-17 22:25:59 +00002744 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002745 nfs_inode_attach_open_context(ctx);
2746 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2747 nfs4_schedule_stateid_recovery(server, state);
2748 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002749out:
Olga Kornievskaia22a87112019-03-19 12:12:13 -04002750 if (!opendata->cancelled)
2751 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002752 return ret;
2753}
2754
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002755/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002756 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 */
Andy Adamson82be4172012-05-23 05:02:35 -04002758static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002759 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002760 int flags,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002761 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002762 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 struct nfs4_state_owner *sp;
2765 struct nfs4_state *state = NULL;
2766 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002767 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002768 struct dentry *dentry = ctx->dentry;
2769 struct rpc_cred *cred = ctx->cred;
2770 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2771 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002772 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002773 struct iattr *sattr = c->sattr;
2774 struct nfs4_label *label = c->label;
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 Myklebust2d9b39d2017-11-06 15:28:03 -05002793 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2794 c, 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;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002875 struct nfs4_open_createattrs c = {
2876 .label = label,
2877 .sattr = sattr,
2878 .verf = {
2879 [0] = (__u32)jiffies,
2880 [1] = (__u32)current->pid,
2881 },
2882 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 int status;
2884
2885 do {
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002886 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002887 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002888 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (status == 0)
2890 break;
2891 /* NOTE: BAD_SEQID means the server and client disagree about the
2892 * book-keeping w.r.t. state-changing operations
2893 * (OPEN/CLOSE/LOCK/LOCKU...)
2894 * It is actually a sign of a bug on the client or on the server.
2895 *
2896 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002897 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 * have unhashed the old state_owner for us, and that we can
2899 * therefore safely retry using a new one. We should still warn
2900 * the user though...
2901 */
2902 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002903 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002904 " returned a bad sequence-id error!\n",
2905 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 exception.retry = 1;
2907 continue;
2908 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002909 /*
2910 * BAD_STATEID on OPEN means that the server cancelled our
2911 * state before it received the OPEN_CONFIRM.
2912 * Recover by retrying the request as per the discussion
2913 * on Page 181 of RFC3530.
2914 */
2915 if (status == -NFS4ERR_BAD_STATEID) {
2916 exception.retry = 1;
2917 continue;
2918 }
Robert Milkowskid4dc8c82020-01-28 08:37:47 +00002919 if (status == -NFS4ERR_EXPIRED) {
2920 nfs4_schedule_lease_recovery(server->nfs_client);
2921 exception.retry = 1;
2922 continue;
2923 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002924 if (status == -EAGAIN) {
2925 /* We must have found a delegation */
2926 exception.retry = 1;
2927 continue;
2928 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002929 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2930 continue;
2931 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 status, &exception));
2933 } while (exception.retry);
2934 return res;
2935}
2936
Trond Myklebust8487c472016-06-26 08:44:35 -04002937static int _nfs4_do_setattr(struct inode *inode,
2938 struct nfs_setattrargs *arg,
2939 struct nfs_setattrres *res,
2940 struct rpc_cred *cred,
2941 struct nfs4_state *state)
2942{
2943 struct nfs_server *server = NFS_SERVER(inode);
2944 struct rpc_message msg = {
2945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2946 .rpc_argp = arg,
2947 .rpc_resp = res,
2948 .rpc_cred = cred,
2949 };
2950 struct rpc_cred *delegation_cred = NULL;
2951 unsigned long timestamp = jiffies;
2952 fmode_t fmode;
2953 bool truncate;
2954 int status;
2955
2956 nfs_fattr_init(res->fattr);
2957
2958 /* Servers should only apply open mode checks for file size changes */
2959 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2960 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2961
2962 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2963 /* Use that stateid */
2964 } else if (truncate && state != NULL) {
2965 struct nfs_lockowner lockowner = {
2966 .l_owner = current->files,
2967 .l_pid = current->tgid,
2968 };
2969 if (!nfs4_valid_open_stateid(state))
2970 return -EBADF;
2971 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2972 &arg->stateid, &delegation_cred) == -EIO)
2973 return -EBADF;
2974 } else
2975 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2976 if (delegation_cred)
2977 msg.rpc_cred = delegation_cred;
2978
2979 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2980
2981 put_rpccred(delegation_cred);
2982 if (status == 0 && state != NULL)
2983 renew_lease(server, timestamp);
2984 trace_nfs4_setattr(inode, &arg->stateid, status);
2985 return status;
2986}
2987
2988static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2989 struct nfs_fattr *fattr, struct iattr *sattr,
2990 struct nfs4_state *state, struct nfs4_label *ilabel,
2991 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002993 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002995 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 .iap = sattr,
2997 .server = server,
2998 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002999 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 };
3001 struct nfs_setattrres res = {
3002 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003003 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 .server = server,
3005 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003006 struct nfs4_exception exception = {
3007 .state = state,
3008 .inode = inode,
3009 .stateid = &arg.stateid,
3010 };
3011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
David Quigleyaa9c2662013-05-22 12:50:44 -04003013 arg.bitmask = nfs4_bitmask(server, ilabel);
3014 if (ilabel)
3015 arg.bitmask = nfs4_bitmask(server, olabel);
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003018 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003019 switch (err) {
3020 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003021 if (!(sattr->ia_valid & ATTR_SIZE)) {
3022 pr_warn_once("NFSv4: server %s is incorrectly "
3023 "applying open mode checks to "
3024 "a SETATTR that is not "
3025 "changing file size.\n",
3026 server->nfs_client->cl_hostname);
3027 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003028 if (state && !(state->state & FMODE_WRITE)) {
3029 err = -EBADF;
3030 if (sattr->ia_valid & ATTR_OPEN)
3031 err = -EACCES;
3032 goto out;
3033 }
3034 }
3035 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003037out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return err;
3039}
3040
Peng Tao500d7012015-09-22 11:35:22 +08003041static bool
3042nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3043{
3044 if (inode == NULL || !nfs_have_layout(inode))
3045 return false;
3046
3047 return pnfs_wait_on_layoutreturn(inode, task);
3048}
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050struct nfs4_closedata {
3051 struct inode *inode;
3052 struct nfs4_state *state;
3053 struct nfs_closeargs arg;
3054 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003055 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003056 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003057 bool roc;
3058 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059};
3060
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003061static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003062{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003063 struct nfs4_closedata *calldata = data;
3064 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003065 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003066
Fred Isamanf7e89172011-01-06 11:36:32 +00003067 if (calldata->roc)
3068 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003069 nfs4_put_open_state(calldata->state);
3070 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003071 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003072 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003073 kfree(calldata);
3074}
3075
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003076static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003078 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003081 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Chuck Levera3ca5652012-03-01 17:00:40 -05003083 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003084 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3085 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003086 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 /* hmm. we are done with the inode, and in the process of freeing
3088 * the state_owner. we keep this around to process errors
3089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 switch (task->tk_status) {
3091 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003092 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003093 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003094 pnfs_roc_set_barrier(state->inode,
3095 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003096 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003097 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003098 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003100 case -NFS4ERR_EXPIRED:
3101 nfs4_free_revoked_stateid(server,
3102 &calldata->arg.stateid,
3103 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003104 case -NFS4ERR_OLD_STATEID:
3105 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003106 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003107 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003108 rpc_restart_call_prepare(task);
3109 goto out_release;
3110 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003111 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003114 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003115 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003116 goto out_release;
3117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003119 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3120 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003121out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003122 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003123 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003124 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003127static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003129 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003130 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003131 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003132 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003133 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003134
Chuck Levera3ca5652012-03-01 17:00:40 -05003135 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003136 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003137 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003138
Trond Myklebust88069f72009-12-08 08:33:16 -05003139 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003140 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003141 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3142 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3143 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003144 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003145 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003146 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003147 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003148 if (state->n_rdonly == 0)
3149 call_close |= is_rdonly;
3150 else if (is_rdonly)
3151 calldata->arg.fmode |= FMODE_READ;
3152 if (state->n_wronly == 0)
3153 call_close |= is_wronly;
3154 else if (is_wronly)
3155 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003156 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3157 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003158 } else if (is_rdwr)
3159 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3160
Trond Myklebust5cc78612016-11-14 11:19:56 -05003161 if (!nfs4_valid_open_stateid(state) ||
3162 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003163 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003164 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003165
3166 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003167 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003168 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003169 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003170
Peng Tao500d7012015-09-22 11:35:22 +08003171 if (nfs4_wait_on_layoutreturn(inode, task)) {
3172 nfs_release_seqid(calldata->arg.seqid);
3173 goto out_wait;
3174 }
3175
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003176 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003177 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003178 if (calldata->roc)
3179 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3180
Trond Myklebust6ae37332015-01-30 14:21:14 -05003181 calldata->arg.share_access =
3182 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3183 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003184
Trond Myklebust516a6af2005-10-27 22:12:41 -04003185 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003186 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003187 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003188 &calldata->arg.seq_args,
3189 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003190 task) != 0)
3191 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003192 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003193 return;
3194out_no_action:
3195 task->tk_action = NULL;
3196out_wait:
3197 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003200static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003201 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003202 .rpc_call_done = nfs4_close_done,
3203 .rpc_release = nfs4_free_closedata,
3204};
3205
Peng Taofe08c542014-07-03 13:05:01 +08003206static bool nfs4_roc(struct inode *inode)
3207{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003208 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003209 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003210 return pnfs_roc(inode);
3211}
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213/*
3214 * It is possible for data to be read/written from a mem-mapped file
3215 * after the sys_close call (which hits the vfs layer as a flush).
3216 * This means that we can't safely call nfsv4 close on a file until
3217 * the inode is cleared. This in turn means that we are not good
3218 * NFSv4 citizens - we do not indicate to the server to update the file's
3219 * share state even when we are done with one of the three share
3220 * stateid's in the inode.
3221 *
3222 * NOTE: Caller must be holding the sp->so_owner semaphore!
3223 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003224int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003226 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003227 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003229 struct nfs4_state_owner *sp = state->owner;
3230 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3233 .rpc_cred = state->owner->so_cred,
3234 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003235 struct rpc_task_setup task_setup_data = {
3236 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003237 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003238 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003239 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003240 .flags = RPC_TASK_ASYNC,
3241 };
Trond Myklebust95121352005-10-18 14:20:12 -07003242 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003244 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3245 &task_setup_data.rpc_client, &msg);
3246
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003247 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003249 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003250 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003251 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003253 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003255 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3256 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003257 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003258 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003259 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003260 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003261 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003262 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003263 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003264 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003265 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003266
Trond Myklebust1174dd12010-12-21 10:52:24 -05003267 msg.rpc_argp = &calldata->arg;
3268 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003269 task_setup_data.callback_data = calldata;
3270 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003271 if (IS_ERR(task))
3272 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003273 status = 0;
3274 if (wait)
3275 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003276 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003277 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003278out_free_calldata:
3279 kfree(calldata);
3280out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003281 nfs4_put_open_state(state);
3282 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003283 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
Trond Myklebust2b484292010-09-17 10:56:51 -04003286static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003287nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3288 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003291 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3292
3293 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Trond Myklebust565277f2007-10-15 18:17:53 -04003295 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003296 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003297
3298 nfs4_label_release_security(label);
3299
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003300 if (IS_ERR(state))
3301 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003302 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Trond Myklebust1185a552009-12-03 15:54:02 -05003305static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003306{
3307 if (ctx->state == NULL)
3308 return;
3309 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003310 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003311 else
Al Viro643168c2011-06-22 18:20:23 -04003312 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003313}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Trond Myklebustb944dba2013-11-04 15:20:20 -05003315#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3316#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003317#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3320{
Kinglong Mee8c612822015-08-26 21:12:58 +08003321 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003322 struct nfs4_server_caps_arg args = {
3323 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003324 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003325 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 struct nfs4_server_caps_res res = {};
3327 struct rpc_message msg = {
3328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003329 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 .rpc_resp = &res,
3331 };
3332 int status;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003333 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Kinglong Mee8c612822015-08-26 21:12:58 +08003335 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3336 FATTR4_WORD0_FH_EXPIRE_TYPE |
3337 FATTR4_WORD0_LINK_SUPPORT |
3338 FATTR4_WORD0_SYMLINK_SUPPORT |
3339 FATTR4_WORD0_ACLSUPPORT;
3340 if (minorversion)
3341 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3342
Bryan Schumaker7c513052011-03-24 17:12:24 +00003343 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003345 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003346 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003347 case 0:
3348 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3349 res.attr_bitmask[2] = 0;
3350 break;
3351 case 1:
3352 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3353 break;
3354 case 2:
3355 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003358 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3359 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3360 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3361 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003362 NFS_CAP_CTIME|NFS_CAP_MTIME|
3363 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003364 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3365 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 server->caps |= NFS_CAP_ACLS;
3367 if (res.has_links != 0)
3368 server->caps |= NFS_CAP_HARDLINKS;
3369 if (res.has_symlinks != 0)
3370 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003371 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3372 server->caps |= NFS_CAP_FILEID;
3373 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3374 server->caps |= NFS_CAP_MODE;
3375 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3376 server->caps |= NFS_CAP_NLINK;
3377 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3378 server->caps |= NFS_CAP_OWNER;
3379 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3380 server->caps |= NFS_CAP_OWNER_GROUP;
3381 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3382 server->caps |= NFS_CAP_ATIME;
3383 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3384 server->caps |= NFS_CAP_CTIME;
3385 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3386 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003387#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3388 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3389 server->caps |= NFS_CAP_SECURITY_LABEL;
3390#endif
3391 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3392 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003393 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003394
Trond Myklebusta65318b2009-03-11 14:10:28 -04003395 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3396 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3397 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003398 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003399
3400 /* Avoid a regression due to buggy server */
3401 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3402 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003403 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3404 sizeof(server->exclcreat_bitmask));
Trond Myklebustfca22d52017-05-09 15:47:15 -04003405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003407 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return status;
3411}
3412
Trond Myklebust55a97592006-06-09 09:34:19 -04003413int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
3415 struct nfs4_exception exception = { };
3416 int err;
3417 do {
3418 err = nfs4_handle_exception(server,
3419 _nfs4_server_capabilities(server, fhandle),
3420 &exception);
3421 } while (exception.retry);
3422 return err;
3423}
3424
3425static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3426 struct nfs_fsinfo *info)
3427{
David Quigleyaa9c2662013-05-22 12:50:44 -04003428 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003430 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 };
3432 struct nfs4_lookup_res res = {
3433 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003434 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 .fh = fhandle,
3436 };
3437 struct rpc_message msg = {
3438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3439 .rpc_argp = &args,
3440 .rpc_resp = &res,
3441 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003442
David Quigleyaa9c2662013-05-22 12:50:44 -04003443 bitmask[0] = nfs4_fattr_bitmap[0];
3444 bitmask[1] = nfs4_fattr_bitmap[1];
3445 /*
3446 * Process the label in the upcoming getfattr
3447 */
3448 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3449
Trond Myklebust0e574af2005-10-27 22:12:38 -04003450 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003451 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
3454static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3455 struct nfs_fsinfo *info)
3456{
3457 struct nfs4_exception exception = { };
3458 int err;
3459 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003460 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003461 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003462 switch (err) {
3463 case 0:
3464 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003465 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003466 default:
3467 err = nfs4_handle_exception(server, err, &exception);
3468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003470out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return err;
3472}
3473
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003474static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3475 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3476{
Trond Myklebustc2190662013-08-26 19:23:04 -04003477 struct rpc_auth_create_args auth_args = {
3478 .pseudoflavor = flavor,
3479 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003480 struct rpc_auth *auth;
3481 int ret;
3482
Trond Myklebustc2190662013-08-26 19:23:04 -04003483 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003484 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003485 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003486 goto out;
3487 }
3488 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003489out:
3490 return ret;
3491}
3492
Chuck Lever9a744ba2013-03-16 15:56:02 -04003493/*
3494 * Retry pseudoroot lookup with various security flavors. We do this when:
3495 *
3496 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3497 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3498 *
3499 * Returns zero on success, or a negative NFS4ERR value, or a
3500 * negative errno value.
3501 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003502static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003503 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003505 /* Per 3530bis 15.33.5 */
3506 static const rpc_authflavor_t flav_array[] = {
3507 RPC_AUTH_GSS_KRB5P,
3508 RPC_AUTH_GSS_KRB5I,
3509 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003510 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003511 RPC_AUTH_NULL,
3512 };
3513 int status = -EPERM;
3514 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003516 if (server->auth_info.flavor_len > 0) {
3517 /* try each flavor specified by user */
3518 for (i = 0; i < server->auth_info.flavor_len; i++) {
3519 status = nfs4_lookup_root_sec(server, fhandle, info,
3520 server->auth_info.flavors[i]);
3521 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3522 continue;
3523 break;
3524 }
3525 } else {
3526 /* no flavors specified by user, try default list */
3527 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3528 status = nfs4_lookup_root_sec(server, fhandle, info,
3529 flav_array[i]);
3530 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3531 continue;
3532 break;
3533 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003534 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003535
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003536 /*
3537 * -EACCESS could mean that the user doesn't have correct permissions
3538 * to access the mount. It could also mean that we tried to mount
3539 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3540 * existing mount programs don't handle -EACCES very well so it should
3541 * be mapped to -EPERM instead.
3542 */
3543 if (status == -EACCES)
3544 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003545 return status;
3546}
3547
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003548/**
3549 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3550 * @server: initialized nfs_server handle
3551 * @fhandle: we fill in the pseudo-fs root file handle
3552 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003553 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003554 *
3555 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003556 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003557int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003558 struct nfs_fsinfo *info,
3559 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003560{
Andre Przywarac7757072015-04-23 17:17:40 +01003561 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003562
Andre Przywarac7757072015-04-23 17:17:40 +01003563 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003564 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003565
3566 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003567 status = server->nfs_client->cl_mvops->find_root_sec(server,
3568 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (status == 0)
3571 status = nfs4_server_capabilities(server, fhandle);
3572 if (status == 0)
3573 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003574
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003575 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Bryan Schumakerbae36242012-05-10 15:07:31 -04003578static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3579 struct nfs_fsinfo *info)
3580{
3581 int error;
3582 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003583 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003584
3585 error = nfs4_server_capabilities(server, mntfh);
3586 if (error < 0) {
3587 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3588 return error;
3589 }
3590
David Quigley14c43f72013-05-22 12:50:43 -04003591 label = nfs4_label_alloc(server, GFP_KERNEL);
3592 if (IS_ERR(label))
3593 return PTR_ERR(label);
3594
David Quigley1775fd32013-05-22 12:50:42 -04003595 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003596 if (error < 0) {
3597 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003598 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003599 }
3600
3601 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3602 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3603 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3604
David Quigley14c43f72013-05-22 12:50:43 -04003605err_free_label:
3606 nfs4_label_free(label);
3607
Bryan Schumakerbae36242012-05-10 15:07:31 -04003608 return error;
3609}
3610
Manoj Naik6b97fd32006-06-09 09:34:29 -04003611/*
3612 * Get locations and (maybe) other attributes of a referral.
3613 * Note that we'll actually follow the referral later when
3614 * we detect fsid mismatch in inode revalidation
3615 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003616static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3617 const struct qstr *name, struct nfs_fattr *fattr,
3618 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003619{
3620 int status = -ENOMEM;
3621 struct page *page = NULL;
3622 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003623
3624 page = alloc_page(GFP_KERNEL);
3625 if (page == NULL)
3626 goto out;
3627 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3628 if (locations == NULL)
3629 goto out;
3630
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003631 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003632 if (status != 0)
3633 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003634
3635 /*
3636 * If the fsid didn't change, this is a migration event, not a
3637 * referral. Cause us to drop into the exception handler, which
3638 * will kick off migration recovery.
3639 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003640 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003641 dprintk("%s: server did not return a different fsid for"
3642 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003643 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003644 goto out;
3645 }
Andy Adamson533eb462011-06-13 18:25:56 -04003646 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3647 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003648
Andy Adamson533eb462011-06-13 18:25:56 -04003649 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003650 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003651 memset(fhandle, 0, sizeof(struct nfs_fh));
3652out:
3653 if (page)
3654 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003655 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003656 return status;
3657}
3658
David Quigley1775fd32013-05-22 12:50:42 -04003659static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3660 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
3662 struct nfs4_getattr_arg args = {
3663 .fh = fhandle,
3664 .bitmask = server->attr_bitmask,
3665 };
3666 struct nfs4_getattr_res res = {
3667 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003668 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 .server = server,
3670 };
3671 struct rpc_message msg = {
3672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3673 .rpc_argp = &args,
3674 .rpc_resp = &res,
3675 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003676
3677 args.bitmask = nfs4_bitmask(server, label);
3678
Trond Myklebust0e574af2005-10-27 22:12:38 -04003679 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003680 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
David Quigley1775fd32013-05-22 12:50:42 -04003683static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3684 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
3686 struct nfs4_exception exception = { };
3687 int err;
3688 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003689 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3690 trace_nfs4_getattr(server, fhandle, fattr, err);
3691 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 &exception);
3693 } while (exception.retry);
3694 return err;
3695}
3696
3697/*
3698 * The file is not closed if it is opened due to the a request to change
3699 * the size of the file. The open call will not be needed once the
3700 * VFS layer lookup-intents are implemented.
3701 *
3702 * Close is called when the inode is destroyed.
3703 * If we haven't opened the file for O_WRONLY, we
3704 * need to in the size_change case to obtain a stateid.
3705 *
3706 * Got race?
3707 * Because OPEN is always done by name in nfsv4, it is
3708 * possible that we opened a different file by the same
3709 * name. We can recognize this race condition, but we
3710 * can't do anything about it besides returning an error.
3711 *
3712 * This will be fixed with VFS changes (lookup-intent).
3713 */
3714static int
3715nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3716 struct iattr *sattr)
3717{
David Howells2b0143b2015-03-17 22:25:59 +00003718 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003719 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003720 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003721 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 int status;
3723
Peng Tao88ac8152014-09-12 11:04:10 +08003724 if (pnfs_ld_layoutret_on_setattr(inode) &&
3725 sattr->ia_valid & ATTR_SIZE &&
3726 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003727 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003728
Trond Myklebust0e574af2005-10-27 22:12:38 -04003729 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Andy Adamson26699402012-05-30 16:12:24 -04003731 /* Deal with open(O_TRUNC) */
3732 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003733 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003734
3735 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003736 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003737 return 0;
3738
Trond Myklebustd5308382005-11-04 15:33:38 -05003739 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003740 if (sattr->ia_valid & ATTR_FILE) {
3741 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003742
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003743 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003744 if (ctx) {
3745 cred = ctx->cred;
3746 state = ctx->state;
3747 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003748 }
3749
David Quigley14c43f72013-05-22 12:50:43 -04003750 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3751 if (IS_ERR(label))
3752 return PTR_ERR(label);
3753
3754 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003755 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003756 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003757 nfs_setsecurity(inode, fattr, label);
3758 }
David Quigley14c43f72013-05-22 12:50:43 -04003759 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return status;
3761}
3762
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003763static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3764 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003765 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003766{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003767 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003768 int status;
3769 struct nfs4_lookup_arg args = {
3770 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003771 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003772 .name = name,
3773 };
3774 struct nfs4_lookup_res res = {
3775 .server = server,
3776 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003777 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003778 .fh = fhandle,
3779 };
3780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3782 .rpc_argp = &args,
3783 .rpc_resp = &res,
3784 };
3785
David Quigleyaa9c2662013-05-22 12:50:44 -04003786 args.bitmask = nfs4_bitmask(server, label);
3787
David Howells2b3de442006-08-22 20:06:09 -04003788 nfs_fattr_init(fattr);
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003791 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 dprintk("NFS reply lookup: %d\n", status);
3793 return status;
3794}
3795
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003796static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003797{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003798 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003799 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003800 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3801 fattr->nlink = 2;
3802}
3803
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003804static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003805 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003806 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003807{
3808 struct nfs4_exception exception = { };
3809 struct rpc_clnt *client = *clnt;
3810 int err;
3811 do {
David Quigley1775fd32013-05-22 12:50:42 -04003812 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003813 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003814 switch (err) {
3815 case -NFS4ERR_BADNAME:
3816 err = -ENOENT;
3817 goto out;
3818 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003819 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003820 if (err == -NFS4ERR_MOVED)
3821 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003822 goto out;
3823 case -NFS4ERR_WRONGSEC:
3824 err = -EPERM;
3825 if (client != *clnt)
3826 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003827 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003828 if (IS_ERR(client))
3829 return PTR_ERR(client);
3830
3831 exception.retry = 1;
3832 break;
3833 default:
3834 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3835 }
3836 } while (exception.retry);
3837
3838out:
3839 if (err == 0)
3840 *clnt = client;
3841 else if (client != *clnt)
3842 rpc_shutdown_client(client);
3843
3844 return err;
3845}
3846
Al Virobeffb8f2016-07-20 16:34:42 -04003847static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003848 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3849 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003851 int status;
3852 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003853
David Quigley1775fd32013-05-22 12:50:42 -04003854 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003855 if (client != NFS_CLIENT(dir)) {
3856 rpc_shutdown_client(client);
3857 nfs_fixup_secinfo_attributes(fattr);
3858 }
3859 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003862struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003863nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003864 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3865{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003866 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003867 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003868
David Quigley1775fd32013-05-22 12:50:42 -04003869 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003870 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003871 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003872 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003873}
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3876{
Trond Myklebust76b32992007-08-10 17:45:11 -04003877 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 struct nfs4_accessargs args = {
3879 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003880 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003882 struct nfs4_accessres res = {
3883 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003884 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 struct rpc_message msg = {
3886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3887 .rpc_argp = &args,
3888 .rpc_resp = &res,
3889 .rpc_cred = entry->cred,
3890 };
3891 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003892 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 /*
3895 * Determine which access bits we want to ask for...
3896 */
3897 if (mode & MAY_READ)
3898 args.access |= NFS4_ACCESS_READ;
3899 if (S_ISDIR(inode->i_mode)) {
3900 if (mode & MAY_WRITE)
3901 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3902 if (mode & MAY_EXEC)
3903 args.access |= NFS4_ACCESS_LOOKUP;
3904 } else {
3905 if (mode & MAY_WRITE)
3906 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3907 if (mode & MAY_EXEC)
3908 args.access |= NFS4_ACCESS_EXECUTE;
3909 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003910
3911 res.fattr = nfs_alloc_fattr();
3912 if (res.fattr == NULL)
3913 return -ENOMEM;
3914
Bryan Schumaker7c513052011-03-24 17:12:24 +00003915 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003917 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003918 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003920 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 return status;
3922}
3923
3924static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3925{
3926 struct nfs4_exception exception = { };
3927 int err;
3928 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003929 err = _nfs4_proc_access(inode, entry);
3930 trace_nfs4_access(inode, err);
3931 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 &exception);
3933 } while (exception.retry);
3934 return err;
3935}
3936
3937/*
3938 * TODO: For the time being, we don't try to get any attributes
3939 * along with any of the zero-copy operations READ, READDIR,
3940 * READLINK, WRITE.
3941 *
3942 * In the case of the first three, we want to put the GETATTR
3943 * after the read-type operation -- this is because it is hard
3944 * to predict the length of a GETATTR response in v4, and thus
3945 * align the READ data correctly. This means that the GETATTR
3946 * may end up partially falling into the page cache, and we should
3947 * shift it into the 'tail' of the xdr_buf before processing.
3948 * To do this efficiently, we need to know the total length
3949 * of data received, which doesn't seem to be available outside
3950 * of the RPC layer.
3951 *
3952 * In the case of WRITE, we also want to put the GETATTR after
3953 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003954 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 *
3956 * Both of these changes to the XDR layer would in fact be quite
3957 * minor, but I decided to leave them for a subsequent patch.
3958 */
3959static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3960 unsigned int pgbase, unsigned int pglen)
3961{
3962 struct nfs4_readlink args = {
3963 .fh = NFS_FH(inode),
3964 .pgbase = pgbase,
3965 .pglen = pglen,
3966 .pages = &page,
3967 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003968 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 struct rpc_message msg = {
3970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3971 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003972 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 };
3974
Bryan Schumaker7c513052011-03-24 17:12:24 +00003975 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 -07003976}
3977
3978static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3979 unsigned int pgbase, unsigned int pglen)
3980{
3981 struct nfs4_exception exception = { };
3982 int err;
3983 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003984 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3985 trace_nfs4_readlink(inode, err);
3986 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 &exception);
3988 } while (exception.retry);
3989 return err;
3990}
3991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003993 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995static int
3996nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003997 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
David Quigleyaa9c2662013-05-22 12:50:44 -04003999 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004000 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 int status = 0;
4003
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004004 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
4005 if (IS_ERR(ctx))
4006 return PTR_ERR(ctx);
4007
David Quigleyaa9c2662013-05-22 12:50:44 -04004008 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4009
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004010 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004011 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 if (IS_ERR(state)) {
4013 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004014 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004017 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004018 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return status;
4020}
4021
Al Virobeffb8f2016-07-20 16:34:42 -04004022static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Trond Myklebust16e42952005-10-27 22:12:44 -04004024 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004025 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004027 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004029 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004030 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4034 .rpc_argp = &args,
4035 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004037 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004038
Bryan Schumaker7c513052011-03-24 17:12:24 +00004039 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004040 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004041 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 return status;
4043}
4044
Al Virobeffb8f2016-07-20 16:34:42 -04004045static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
4047 struct nfs4_exception exception = { };
4048 int err;
4049 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004050 err = _nfs4_proc_remove(dir, name);
4051 trace_nfs4_remove(dir, name, err);
4052 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 &exception);
4054 } while (exception.retry);
4055 return err;
4056}
4057
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004058static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004060 struct nfs_server *server = NFS_SERVER(dir);
4061 struct nfs_removeargs *args = msg->rpc_argp;
4062 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004064 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004066 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004067
4068 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004071static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4072{
Al Viro884be172016-04-28 23:56:31 -04004073 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004074 &data->args.seq_args,
4075 &data->res.seq_res,
4076 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004079static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004081 struct nfs_unlinkdata *data = task->tk_calldata;
4082 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004083
Trond Myklebust14516c32010-07-31 14:29:06 -04004084 if (!nfs4_sequence_done(task, &res->seq_res))
4085 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004086 if (nfs4_async_handle_error(task, res->server, NULL,
4087 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004088 return 0;
4089 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004090 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
Jeff Laytond3d41522010-09-17 17:31:57 -04004093static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4094{
4095 struct nfs_server *server = NFS_SERVER(dir);
4096 struct nfs_renameargs *arg = msg->rpc_argp;
4097 struct nfs_renameres *res = msg->rpc_resp;
4098
4099 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004100 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004101 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004102}
4103
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004104static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4105{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004106 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4107 &data->args.seq_args,
4108 &data->res.seq_res,
4109 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004110}
4111
4112static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4113 struct inode *new_dir)
4114{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004115 struct nfs_renamedata *data = task->tk_calldata;
4116 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004117
4118 if (!nfs4_sequence_done(task, &res->seq_res))
4119 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004120 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004121 return 0;
4122
4123 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004124 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004125 return 1;
4126}
4127
Al Virobeffb8f2016-07-20 16:34:42 -04004128static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004130 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 struct nfs4_link_arg arg = {
4132 .fh = NFS_FH(inode),
4133 .dir_fh = NFS_FH(dir),
4134 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004135 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004137 struct nfs4_link_res res = {
4138 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004139 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004140 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 struct rpc_message msg = {
4142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4143 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004144 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004146 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Trond Myklebust136f2622010-04-16 16:22:49 -04004148 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004149 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004150 goto out;
4151
David Quigley14c43f72013-05-22 12:50:43 -04004152 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4153 if (IS_ERR(res.label)) {
4154 status = PTR_ERR(res.label);
4155 goto out;
4156 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004157 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004158
Bryan Schumaker7c513052011-03-24 17:12:24 +00004159 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004160 if (!status) {
4161 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004162 status = nfs_post_op_update_inode(inode, res.fattr);
4163 if (!status)
4164 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004165 }
David Quigley14c43f72013-05-22 12:50:43 -04004166
4167
4168 nfs4_label_free(res.label);
4169
Trond Myklebust136f2622010-04-16 16:22:49 -04004170out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004171 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 return status;
4173}
4174
Al Virobeffb8f2016-07-20 16:34:42 -04004175static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
4177 struct nfs4_exception exception = { };
4178 int err;
4179 do {
4180 err = nfs4_handle_exception(NFS_SERVER(inode),
4181 _nfs4_proc_link(inode, dir, name),
4182 &exception);
4183 } while (exception.retry);
4184 return err;
4185}
4186
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004187struct nfs4_createdata {
4188 struct rpc_message msg;
4189 struct nfs4_create_arg arg;
4190 struct nfs4_create_res res;
4191 struct nfs_fh fh;
4192 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004193 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004194};
4195
4196static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004197 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004198{
4199 struct nfs4_createdata *data;
4200
4201 data = kzalloc(sizeof(*data), GFP_KERNEL);
4202 if (data != NULL) {
4203 struct nfs_server *server = NFS_SERVER(dir);
4204
David Quigley14c43f72013-05-22 12:50:43 -04004205 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4206 if (IS_ERR(data->label))
4207 goto out_free;
4208
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004209 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4210 data->msg.rpc_argp = &data->arg;
4211 data->msg.rpc_resp = &data->res;
4212 data->arg.dir_fh = NFS_FH(dir);
4213 data->arg.server = server;
4214 data->arg.name = name;
4215 data->arg.attrs = sattr;
4216 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004217 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004218 data->res.server = server;
4219 data->res.fh = &data->fh;
4220 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004221 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004222 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004223 }
4224 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004225out_free:
4226 kfree(data);
4227 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004228}
4229
4230static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4231{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004232 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004233 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004234 if (status == 0) {
4235 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004236 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 }
4238 return status;
4239}
4240
4241static void nfs4_free_createdata(struct nfs4_createdata *data)
4242{
David Quigley14c43f72013-05-22 12:50:43 -04004243 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004244 kfree(data);
4245}
4246
Chuck Lever4f390c12006-08-22 20:06:22 -04004247static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004248 struct page *page, unsigned int len, struct iattr *sattr,
4249 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004251 struct nfs4_createdata *data;
4252 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Chuck Lever94a6d752006-08-22 20:06:23 -04004254 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004255 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004256
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004257 status = -ENOMEM;
4258 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4259 if (data == NULL)
4260 goto out;
4261
4262 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4263 data->arg.u.symlink.pages = &page;
4264 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004265 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004267 status = nfs4_do_create(dir, dentry, data);
4268
4269 nfs4_free_createdata(data);
4270out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return status;
4272}
4273
Chuck Lever4f390c12006-08-22 20:06:22 -04004274static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004275 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
4277 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004278 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004280
4281 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004284 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4285 trace_nfs4_symlink(dir, &dentry->d_name, err);
4286 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 &exception);
4288 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004289
4290 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 return err;
4292}
4293
4294static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004295 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004297 struct nfs4_createdata *data;
4298 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004300 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4301 if (data == NULL)
4302 goto out;
4303
David Quigley1775fd32013-05-22 12:50:42 -04004304 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004305 status = nfs4_do_create(dir, dentry, data);
4306
4307 nfs4_free_createdata(data);
4308out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return status;
4310}
4311
4312static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4313 struct iattr *sattr)
4314{
4315 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004316 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004318
David Quigleyaa9c2662013-05-22 12:50:44 -04004319 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4320
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004321 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004323 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4324 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4325 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 &exception);
4327 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004328 nfs4_label_release_security(label);
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return err;
4331}
4332
4333static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004334 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
David Howells2b0143b2015-03-17 22:25:59 +00004336 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 struct nfs4_readdir_arg args = {
4338 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004339 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 .pgbase = 0,
4341 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004342 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004343 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 };
4345 struct nfs4_readdir_res res;
4346 struct rpc_message msg = {
4347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4348 .rpc_argp = &args,
4349 .rpc_resp = &res,
4350 .rpc_cred = cred,
4351 };
4352 int status;
4353
Al Viro6de14722013-09-16 10:53:17 -04004354 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4355 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004356 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004357 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004359 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 -05004360 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004361 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004362 status += args.pgbase;
4363 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004364
4365 nfs_invalidate_atime(dir);
4366
Harvey Harrison3110ff82008-05-02 13:42:44 -07004367 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 return status;
4369}
4370
4371static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004372 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373{
4374 struct nfs4_exception exception = { };
4375 int err;
4376 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004377 err = _nfs4_proc_readdir(dentry, cred, cookie,
4378 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004379 trace_nfs4_readdir(d_inode(dentry), err);
4380 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 &exception);
4382 } while (exception.retry);
4383 return err;
4384}
4385
4386static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004387 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004389 struct nfs4_createdata *data;
4390 int mode = sattr->ia_mode;
4391 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004393 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4394 if (data == NULL)
4395 goto out;
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004398 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004400 data->arg.ftype = NF4BLK;
4401 data->arg.u.device.specdata1 = MAJOR(rdev);
4402 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 }
4404 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004405 data->arg.ftype = NF4CHR;
4406 data->arg.u.device.specdata1 = MAJOR(rdev);
4407 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004408 } else if (!S_ISSOCK(mode)) {
4409 status = -EINVAL;
4410 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 }
David Quigley1775fd32013-05-22 12:50:42 -04004412
David Quigleyaa9c2662013-05-22 12:50:44 -04004413 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004415out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004416 nfs4_free_createdata(data);
4417out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 return status;
4419}
4420
4421static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4422 struct iattr *sattr, dev_t rdev)
4423{
4424 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004425 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004427
David Quigleyaa9c2662013-05-22 12:50:44 -04004428 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4429
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004430 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004432 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4433 trace_nfs4_mknod(dir, &dentry->d_name, err);
4434 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 &exception);
4436 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004437
4438 nfs4_label_release_security(label);
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return err;
4441}
4442
4443static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4444 struct nfs_fsstat *fsstat)
4445{
4446 struct nfs4_statfs_arg args = {
4447 .fh = fhandle,
4448 .bitmask = server->attr_bitmask,
4449 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004450 struct nfs4_statfs_res res = {
4451 .fsstat = fsstat,
4452 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 struct rpc_message msg = {
4454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4455 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004456 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 };
4458
Trond Myklebust0e574af2005-10-27 22:12:38 -04004459 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004460 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
4463static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4464{
4465 struct nfs4_exception exception = { };
4466 int err;
4467 do {
4468 err = nfs4_handle_exception(server,
4469 _nfs4_proc_statfs(server, fhandle, fsstat),
4470 &exception);
4471 } while (exception.retry);
4472 return err;
4473}
4474
4475static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4476 struct nfs_fsinfo *fsinfo)
4477{
4478 struct nfs4_fsinfo_arg args = {
4479 .fh = fhandle,
4480 .bitmask = server->attr_bitmask,
4481 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004482 struct nfs4_fsinfo_res res = {
4483 .fsinfo = fsinfo,
4484 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4487 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004488 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 };
4490
Bryan Schumaker7c513052011-03-24 17:12:24 +00004491 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492}
4493
4494static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4495{
4496 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004497 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 int err;
4499
4500 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004501 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004502 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004503 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004504 nfs4_set_lease_period(server->nfs_client,
4505 fsinfo->lease_time * HZ,
4506 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004507 break;
4508 }
4509 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 } while (exception.retry);
4511 return err;
4512}
4513
4514static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4515{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004516 int error;
4517
Trond Myklebust0e574af2005-10-27 22:12:38 -04004518 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004519 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004520 if (error == 0) {
4521 /* block layout checks this! */
4522 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004523 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004524 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004525
4526 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
4529static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4530 struct nfs_pathconf *pathconf)
4531{
4532 struct nfs4_pathconf_arg args = {
4533 .fh = fhandle,
4534 .bitmask = server->attr_bitmask,
4535 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004536 struct nfs4_pathconf_res res = {
4537 .pathconf = pathconf,
4538 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 struct rpc_message msg = {
4540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4541 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004542 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 };
4544
4545 /* None of the pathconf attributes are mandatory to implement */
4546 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4547 memset(pathconf, 0, sizeof(*pathconf));
4548 return 0;
4549 }
4550
Trond Myklebust0e574af2005-10-27 22:12:38 -04004551 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004552 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553}
4554
4555static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4556 struct nfs_pathconf *pathconf)
4557{
4558 struct nfs4_exception exception = { };
4559 int err;
4560
4561 do {
4562 err = nfs4_handle_exception(server,
4563 _nfs4_proc_pathconf(server, fhandle, pathconf),
4564 &exception);
4565 } while (exception.retry);
4566 return err;
4567}
4568
Trond Myklebust5521abf2013-03-16 20:54:34 -04004569int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004570 const struct nfs_open_context *ctx,
4571 const struct nfs_lock_context *l_ctx,
4572 fmode_t fmode)
4573{
4574 const struct nfs_lockowner *lockowner = NULL;
4575
4576 if (l_ctx != NULL)
4577 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004578 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004579}
4580EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4581
Trond Myklebust5521abf2013-03-16 20:54:34 -04004582static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4583 const struct nfs_open_context *ctx,
4584 const struct nfs_lock_context *l_ctx,
4585 fmode_t fmode)
4586{
4587 nfs4_stateid current_stateid;
4588
Trond Myklebuste1253be2014-03-05 08:44:23 -05004589 /* If the current stateid represents a lost lock, then exit */
4590 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4591 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004592 return nfs4_stateid_match(stateid, &current_stateid);
4593}
4594
4595static bool nfs4_error_stateid_expired(int err)
4596{
4597 switch (err) {
4598 case -NFS4ERR_DELEG_REVOKED:
4599 case -NFS4ERR_ADMIN_REVOKED:
4600 case -NFS4ERR_BAD_STATEID:
4601 case -NFS4ERR_STALE_STATEID:
4602 case -NFS4ERR_OLD_STATEID:
4603 case -NFS4ERR_OPENMODE:
4604 case -NFS4ERR_EXPIRED:
4605 return true;
4606 }
4607 return false;
4608}
4609
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004614 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004615 if (task->tk_status < 0) {
4616 struct nfs4_exception exception = {
4617 .inode = hdr->inode,
4618 .state = hdr->args.context->state,
4619 .stateid = &hdr->args.stateid,
4620 };
4621 task->tk_status = nfs4_async_handle_exception(task,
4622 server, task->tk_status, &exception);
4623 if (exception.retry) {
4624 rpc_restart_call_prepare(task);
4625 return -EAGAIN;
4626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
Trond Myklebust5521abf2013-03-16 20:54:34 -04004634static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004635 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004636{
4637
4638 if (!nfs4_error_stateid_expired(task->tk_status) ||
4639 nfs4_stateid_is_current(&args->stateid,
4640 args->context,
4641 args->lock_context,
4642 FMODE_READ))
4643 return false;
4644 rpc_restart_call_prepare(task);
4645 return true;
4646}
4647
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004648static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004649{
4650
4651 dprintk("--> %s\n", __func__);
4652
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004654 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004655 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004656 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004657 if (task->tk_status > 0)
4658 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004659 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4660 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004661}
4662
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4664 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004666 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004667 if (!hdr->pgio_done_cb)
4668 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004669 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004670 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671}
4672
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4674 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004675{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004676 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4677 &hdr->args.seq_args,
4678 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004679 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004680 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4682 hdr->args.lock_context,
4683 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004684 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004686 return -EIO;
4687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688}
4689
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690static int nfs4_write_done_cb(struct rpc_task *task,
4691 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004694
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004695 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004696 if (task->tk_status < 0) {
4697 struct nfs4_exception exception = {
4698 .inode = hdr->inode,
4699 .state = hdr->args.context->state,
4700 .stateid = &hdr->args.stateid,
4701 };
4702 task->tk_status = nfs4_async_handle_exception(task,
4703 NFS_SERVER(inode), task->tk_status,
4704 &exception);
4705 if (exception.retry) {
4706 rpc_restart_call_prepare(task);
4707 return -EAGAIN;
4708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004710 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004711 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004712 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004713 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
Trond Myklebust5521abf2013-03-16 20:54:34 -04004717static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004718 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004719{
4720
4721 if (!nfs4_error_stateid_expired(task->tk_status) ||
4722 nfs4_stateid_is_current(&args->stateid,
4723 args->context,
4724 args->lock_context,
4725 FMODE_WRITE))
4726 return false;
4727 rpc_restart_call_prepare(task);
4728 return true;
4729}
4730
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004731static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004732{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004733 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004734 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004735 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004736 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004737 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4738 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004739}
4740
Trond Myklebust5a37f852012-04-28 14:55:16 -04004741static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004742bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004743{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004744 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004745 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004746 return false;
4747 /* Otherwise, request attributes if and only if we don't hold
4748 * a delegation
4749 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004750 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004751}
Fred Isamana69aef12011-03-03 15:13:47 +00004752
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004753static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4754 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004756 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004757
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004758 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4759 hdr->args.bitmask = NULL;
4760 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004761 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004763
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 if (!hdr->pgio_done_cb)
4765 hdr->pgio_done_cb = nfs4_write_done_cb;
4766 hdr->res.server = server;
4767 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004769 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004770 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
4772
Fred Isaman0b7c0152012-04-20 14:47:39 -04004773static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4774{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004775 nfs4_setup_sequence(NFS_SERVER(data->inode),
4776 &data->args.seq_args,
4777 &data->res.seq_res,
4778 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
4780
Fred Isaman0b7c0152012-04-20 14:47:39 -04004781static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004784
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004785 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004786 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4787 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004788 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004789 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792}
4793
Fred Isaman0b7c0152012-04-20 14:47:39 -04004794static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004795{
4796 if (!nfs4_sequence_done(task, &data->res.seq_res))
4797 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004798 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004799}
4800
Fred Isaman0b7c0152012-04-20 14:47:39 -04004801static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004803 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004804
Fred Isaman0b7c0152012-04-20 14:47:39 -04004805 if (data->commit_done_cb == NULL)
4806 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004807 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004808 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004809 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004812struct nfs4_renewdata {
4813 struct nfs_client *client;
4814 unsigned long timestamp;
4815};
4816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817/*
4818 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4819 * standalone procedure for queueing an asynchronous RENEW.
4820 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004821static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004822{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004823 struct nfs4_renewdata *data = calldata;
4824 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004825
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004826 if (atomic_read(&clp->cl_count) > 1)
4827 nfs4_schedule_state_renewal(clp);
4828 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004829 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004830}
4831
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004832static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004834 struct nfs4_renewdata *data = calldata;
4835 struct nfs_client *clp = data->client;
4836 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004838 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004839 switch (task->tk_status) {
4840 case 0:
4841 break;
4842 case -NFS4ERR_LEASE_MOVED:
4843 nfs4_schedule_lease_moved_recovery(clp);
4844 break;
4845 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004846 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004847 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4848 return;
4849 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004850 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004851 return;
4852 }
4853 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004855 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856}
4857
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004858static const struct rpc_call_ops nfs4_renew_ops = {
4859 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004860 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004861};
4862
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004863static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
4865 struct rpc_message msg = {
4866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4867 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004868 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004870 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004872 if (renew_flags == 0)
4873 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004874 if (!atomic_inc_not_zero(&clp->cl_count))
4875 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004876 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004877 if (data == NULL)
4878 return -ENOMEM;
4879 data->client = clp;
4880 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004881 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004882 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883}
4884
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004885static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886{
4887 struct rpc_message msg = {
4888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4889 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004890 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 };
4892 unsigned long now = jiffies;
4893 int status;
4894
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004895 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 if (status < 0)
4897 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004898 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 return 0;
4900}
4901
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004902static inline int nfs4_server_supports_acls(struct nfs_server *server)
4903{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004904 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004905}
4906
Trond Myklebust21f498c2012-08-24 10:59:25 -04004907/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4908 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004909 * the stack.
4910 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004911#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004912
Neil Hormane9e3d722011-03-04 19:26:03 -05004913static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004914 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004915{
4916 struct page *newpage, **spages;
4917 int rc = 0;
4918 size_t len;
4919 spages = pages;
4920
4921 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004922 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004923 newpage = alloc_page(GFP_KERNEL);
4924
4925 if (newpage == NULL)
4926 goto unwind;
4927 memcpy(page_address(newpage), buf, len);
4928 buf += len;
4929 buflen -= len;
4930 *pages++ = newpage;
4931 rc++;
4932 } while (buflen != 0);
4933
4934 return rc;
4935
4936unwind:
4937 for(; rc > 0; rc--)
4938 __free_page(spages[rc-1]);
4939 return -ENOMEM;
4940}
4941
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004942struct nfs4_cached_acl {
4943 int cached;
4944 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004945 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004946};
4947
4948static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004949{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004950 struct nfs_inode *nfsi = NFS_I(inode);
4951
4952 spin_lock(&inode->i_lock);
4953 kfree(nfsi->nfs4_acl);
4954 nfsi->nfs4_acl = acl;
4955 spin_unlock(&inode->i_lock);
4956}
4957
4958static void nfs4_zap_acl_attr(struct inode *inode)
4959{
4960 nfs4_set_cached_acl(inode, NULL);
4961}
4962
4963static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4964{
4965 struct nfs_inode *nfsi = NFS_I(inode);
4966 struct nfs4_cached_acl *acl;
4967 int ret = -ENOENT;
4968
4969 spin_lock(&inode->i_lock);
4970 acl = nfsi->nfs4_acl;
4971 if (acl == NULL)
4972 goto out;
4973 if (buf == NULL) /* user is just asking for length */
4974 goto out_len;
4975 if (acl->cached == 0)
4976 goto out;
4977 ret = -ERANGE; /* see getxattr(2) man page */
4978 if (acl->len > buflen)
4979 goto out;
4980 memcpy(buf, acl->data, acl->len);
4981out_len:
4982 ret = acl->len;
4983out:
4984 spin_unlock(&inode->i_lock);
4985 return ret;
4986}
4987
Sachin Prabhu5794d212012-04-17 14:36:40 +01004988static 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 +00004989{
4990 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004991 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004992
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004993 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004994 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004995 if (acl == NULL)
4996 goto out;
4997 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004998 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004999 } else {
5000 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5001 if (acl == NULL)
5002 goto out;
5003 acl->cached = 0;
5004 }
5005 acl->len = acl_len;
5006out:
5007 nfs4_set_cached_acl(inode, acl);
5008}
5009
Andy Adamsonbf118a32011-12-07 11:55:27 -05005010/*
5011 * The getxattr API returns the required buffer length when called with a
5012 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5013 * the required buf. On a NULL buf, we send a page of data to the server
5014 * guessing that the ACL request can be serviced by a page. If so, we cache
5015 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5016 * the cache. If not so, we throw away the page, and cache the required
5017 * length. The next getxattr call will then produce another round trip to
5018 * the server, this time with the input buf of the required size.
5019 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005020static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005021{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005022 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005023 struct nfs_getaclargs args = {
5024 .fh = NFS_FH(inode),
5025 .acl_pages = pages,
5026 .acl_len = buflen,
5027 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005028 struct nfs_getaclres res = {
5029 .acl_len = buflen,
5030 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005031 struct rpc_message msg = {
5032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5033 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005034 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005035 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005036 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005037 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005038
Trond Myklebust21f498c2012-08-24 10:59:25 -04005039 if (npages > ARRAY_SIZE(pages))
5040 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005041
Andy Adamsonbf118a32011-12-07 11:55:27 -05005042 for (i = 0; i < npages; i++) {
5043 pages[i] = alloc_page(GFP_KERNEL);
5044 if (!pages[i])
5045 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005046 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005047
5048 /* for decoding across pages */
5049 res.acl_scratch = alloc_page(GFP_KERNEL);
5050 if (!res.acl_scratch)
5051 goto out_free;
5052
Andy Adamsonbf118a32011-12-07 11:55:27 -05005053 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005054
Peng Taode040be2012-01-10 22:42:47 +08005055 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005056 __func__, buf, buflen, npages, args.acl_len);
5057 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5058 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005059 if (ret)
5060 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005061
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005062 /* Handle the case where the passed-in buffer is too short */
5063 if (res.acl_flags & NFS4_ACL_TRUNC) {
5064 /* Did the user only issue a request for the acl length? */
5065 if (buf == NULL)
5066 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005067 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005068 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005069 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005070 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005071 if (buf) {
5072 if (res.acl_len > buflen) {
5073 ret = -ERANGE;
5074 goto out_free;
5075 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005076 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005077 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005078out_ok:
5079 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005080out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005081 for (i = 0; i < npages; i++)
5082 if (pages[i])
5083 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005084 if (res.acl_scratch)
5085 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005086 return ret;
5087}
5088
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005089static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5090{
5091 struct nfs4_exception exception = { };
5092 ssize_t ret;
5093 do {
5094 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005095 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005096 if (ret >= 0)
5097 break;
5098 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5099 } while (exception.retry);
5100 return ret;
5101}
5102
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005103static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5104{
5105 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005106 int ret;
5107
5108 if (!nfs4_server_supports_acls(server))
5109 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005110 ret = nfs_revalidate_inode(server, inode);
5111 if (ret < 0)
5112 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005113 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5114 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005115 ret = nfs4_read_cached_acl(inode, buf, buflen);
5116 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005117 /* -ENOENT is returned if there is no ACL or if there is an ACL
5118 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005119 return ret;
5120 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005121}
5122
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005123static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005124{
5125 struct nfs_server *server = NFS_SERVER(inode);
5126 struct page *pages[NFS4ACL_MAXPAGES];
5127 struct nfs_setaclargs arg = {
5128 .fh = NFS_FH(inode),
5129 .acl_pages = pages,
5130 .acl_len = buflen,
5131 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005132 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005133 struct rpc_message msg = {
5134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5135 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005136 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005137 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005138 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005139 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005140
5141 if (!nfs4_server_supports_acls(server))
5142 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005143 if (npages > ARRAY_SIZE(pages))
5144 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005145 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005146 if (i < 0)
5147 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005148 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005149 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005150
5151 /*
5152 * Free each page after tx, so the only ref left is
5153 * held by the network stack
5154 */
5155 for (; i > 0; i--)
5156 put_page(pages[i-1]);
5157
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005158 /*
5159 * Acl update can result in inode attribute update.
5160 * so mark the attribute cache invalid.
5161 */
5162 spin_lock(&inode->i_lock);
5163 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5164 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005165 nfs_access_zap_cache(inode);
5166 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005167 return ret;
5168}
5169
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005170static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5171{
5172 struct nfs4_exception exception = { };
5173 int err;
5174 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005175 err = __nfs4_proc_set_acl(inode, buf, buflen);
5176 trace_nfs4_set_acl(inode, err);
5177 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005178 &exception);
5179 } while (exception.retry);
5180 return err;
5181}
5182
David Quigleyaa9c2662013-05-22 12:50:44 -04005183#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5184static int _nfs4_get_security_label(struct inode *inode, void *buf,
5185 size_t buflen)
5186{
5187 struct nfs_server *server = NFS_SERVER(inode);
5188 struct nfs_fattr fattr;
5189 struct nfs4_label label = {0, 0, buflen, buf};
5190
5191 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005192 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005193 .fh = NFS_FH(inode),
5194 .bitmask = bitmask,
5195 };
5196 struct nfs4_getattr_res res = {
5197 .fattr = &fattr,
5198 .label = &label,
5199 .server = server,
5200 };
5201 struct rpc_message msg = {
5202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005203 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005204 .rpc_resp = &res,
5205 };
5206 int ret;
5207
5208 nfs_fattr_init(&fattr);
5209
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005210 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005211 if (ret)
5212 return ret;
5213 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5214 return -ENOENT;
5215 if (buflen < label.len)
5216 return -ERANGE;
5217 return 0;
5218}
5219
5220static int nfs4_get_security_label(struct inode *inode, void *buf,
5221 size_t buflen)
5222{
5223 struct nfs4_exception exception = { };
5224 int err;
5225
5226 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5227 return -EOPNOTSUPP;
5228
5229 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005230 err = _nfs4_get_security_label(inode, buf, buflen);
5231 trace_nfs4_get_security_label(inode, err);
5232 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005233 &exception);
5234 } while (exception.retry);
5235 return err;
5236}
5237
5238static int _nfs4_do_set_security_label(struct inode *inode,
5239 struct nfs4_label *ilabel,
5240 struct nfs_fattr *fattr,
5241 struct nfs4_label *olabel)
5242{
5243
5244 struct iattr sattr = {0};
5245 struct nfs_server *server = NFS_SERVER(inode);
5246 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005247 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005248 .fh = NFS_FH(inode),
5249 .iap = &sattr,
5250 .server = server,
5251 .bitmask = bitmask,
5252 .label = ilabel,
5253 };
5254 struct nfs_setattrres res = {
5255 .fattr = fattr,
5256 .label = olabel,
5257 .server = server,
5258 };
5259 struct rpc_message msg = {
5260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005261 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005262 .rpc_resp = &res,
5263 };
5264 int status;
5265
Jeff Layton12207f62013-11-01 10:49:32 -04005266 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005267
Jeff Layton12207f62013-11-01 10:49:32 -04005268 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005269 if (status)
5270 dprintk("%s failed: %d\n", __func__, status);
5271
5272 return status;
5273}
5274
5275static int nfs4_do_set_security_label(struct inode *inode,
5276 struct nfs4_label *ilabel,
5277 struct nfs_fattr *fattr,
5278 struct nfs4_label *olabel)
5279{
5280 struct nfs4_exception exception = { };
5281 int err;
5282
5283 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005284 err = _nfs4_do_set_security_label(inode, ilabel,
5285 fattr, olabel);
5286 trace_nfs4_set_security_label(inode, err);
5287 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005288 &exception);
5289 } while (exception.retry);
5290 return err;
5291}
5292
5293static int
Al Viro59301222016-05-27 10:19:30 -04005294nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005295{
5296 struct nfs4_label ilabel, *olabel = NULL;
5297 struct nfs_fattr fattr;
5298 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005299 int status;
5300
5301 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5302 return -EOPNOTSUPP;
5303
5304 nfs_fattr_init(&fattr);
5305
5306 ilabel.pi = 0;
5307 ilabel.lfs = 0;
5308 ilabel.label = (char *)buf;
5309 ilabel.len = buflen;
5310
5311 cred = rpc_lookup_cred();
5312 if (IS_ERR(cred))
5313 return PTR_ERR(cred);
5314
5315 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5316 if (IS_ERR(olabel)) {
5317 status = -PTR_ERR(olabel);
5318 goto out;
5319 }
5320
5321 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5322 if (status == 0)
5323 nfs_setsecurity(inode, &fattr, olabel);
5324
5325 nfs4_label_free(olabel);
5326out:
5327 put_rpccred(cred);
5328 return status;
5329}
5330#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5331
5332
Chuck Leverf0920752012-05-21 22:45:41 -04005333static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5334 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005335{
5336 __be32 verf[2];
5337
Chuck Lever2c820d92012-05-21 22:45:33 -04005338 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5339 /* An impossible timestamp guarantees this value
5340 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005341 verf[0] = cpu_to_be32(U32_MAX);
5342 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005343 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005344 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005345 u64 ns = ktime_to_ns(nn->boot_time);
5346
5347 verf[0] = cpu_to_be32(ns >> 32);
5348 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005349 }
Chuck Levercd937102012-03-02 17:14:31 -05005350 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5351}
5352
Jeff Laytona3192682015-06-09 19:43:59 -04005353static int
5354nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005355{
Jeff Laytona3192682015-06-09 19:43:59 -04005356 size_t len;
5357 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005358
Trond Myklebustceb3a162015-01-03 15:16:04 -05005359 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005360 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005361
Jeff Laytona3192682015-06-09 19:43:59 -04005362 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005363 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005364 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5365 1 +
5366 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5367 1;
5368 rcu_read_unlock();
5369
5370 if (len > NFS4_OPAQUE_LIMIT + 1)
5371 return -EINVAL;
5372
5373 /*
5374 * Since this string is allocated at mount time, and held until the
5375 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5376 * about a memory-reclaim deadlock.
5377 */
5378 str = kmalloc(len, GFP_KERNEL);
5379 if (!str)
5380 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005381
Chuck Levere984a552012-09-14 17:24:21 -04005382 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005383 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005384 clp->cl_ipaddr,
5385 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5386 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005387 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005388
Jeff Laytona3192682015-06-09 19:43:59 -04005389 clp->cl_owner_id = str;
5390 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005391}
5392
Jeff Layton873e3852015-06-09 19:44:00 -04005393static int
5394nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005395{
Jeff Layton873e3852015-06-09 19:44:00 -04005396 size_t len;
5397 char *str;
5398
5399 len = 10 + 10 + 1 + 10 + 1 +
5400 strlen(nfs4_client_id_uniquifier) + 1 +
5401 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5402
5403 if (len > NFS4_OPAQUE_LIMIT + 1)
5404 return -EINVAL;
5405
5406 /*
5407 * Since this string is allocated at mount time, and held until the
5408 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5409 * about a memory-reclaim deadlock.
5410 */
5411 str = kmalloc(len, GFP_KERNEL);
5412 if (!str)
5413 return -ENOMEM;
5414
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005415 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005416 clp->rpc_ops->version, clp->cl_minorversion,
5417 nfs4_client_id_uniquifier,
5418 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005419 clp->cl_owner_id = str;
5420 return 0;
5421}
5422
5423static int
5424nfs4_init_uniform_client_string(struct nfs_client *clp)
5425{
Jeff Layton873e3852015-06-09 19:44:00 -04005426 size_t len;
5427 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005428
5429 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005430 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005431
5432 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005433 return nfs4_init_uniquifier_client_string(clp);
5434
5435 len = 10 + 10 + 1 + 10 + 1 +
5436 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5437
5438 if (len > NFS4_OPAQUE_LIMIT + 1)
5439 return -EINVAL;
5440
5441 /*
5442 * Since this string is allocated at mount time, and held until the
5443 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5444 * about a memory-reclaim deadlock.
5445 */
5446 str = kmalloc(len, GFP_KERNEL);
5447 if (!str)
5448 return -ENOMEM;
5449
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005450 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005451 clp->rpc_ops->version, clp->cl_minorversion,
5452 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005453 clp->cl_owner_id = str;
5454 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005455}
5456
Chuck Lever706cb8d2014-03-12 12:51:47 -04005457/*
5458 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5459 * services. Advertise one based on the address family of the
5460 * clientaddr.
5461 */
5462static unsigned int
5463nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5464{
5465 if (strchr(clp->cl_ipaddr, ':') != NULL)
5466 return scnprintf(buf, len, "tcp6");
5467 else
5468 return scnprintf(buf, len, "tcp");
5469}
5470
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005471static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5472{
5473 struct nfs4_setclientid *sc = calldata;
5474
5475 if (task->tk_status == 0)
5476 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5477}
5478
5479static const struct rpc_call_ops nfs4_setclientid_ops = {
5480 .rpc_call_done = nfs4_setclientid_done,
5481};
5482
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005483/**
5484 * nfs4_proc_setclientid - Negotiate client ID
5485 * @clp: state data structure
5486 * @program: RPC program for NFSv4 callback service
5487 * @port: IP port number for NFS4 callback service
5488 * @cred: RPC credential to use for this call
5489 * @res: where to place the result
5490 *
5491 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5492 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005493int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5494 unsigned short port, struct rpc_cred *cred,
5495 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
5497 nfs4_verifier sc_verifier;
5498 struct nfs4_setclientid setclientid = {
5499 .sc_verifier = &sc_verifier,
5500 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005501 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 };
5503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5505 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005506 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005507 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005509 struct rpc_task *task;
5510 struct rpc_task_setup task_setup_data = {
5511 .rpc_client = clp->cl_rpcclient,
5512 .rpc_message = &msg,
5513 .callback_ops = &nfs4_setclientid_ops,
5514 .callback_data = &setclientid,
5515 .flags = RPC_TASK_TIMEOUT,
5516 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005517 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Chuck Leverde734832012-07-11 16:30:50 -04005519 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005520 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005521
5522 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5523 status = nfs4_init_uniform_client_string(clp);
5524 else
Jeff Laytona3192682015-06-09 19:43:59 -04005525 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005526
5527 if (status)
5528 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005529
Chuck Leverde734832012-07-11 16:30:50 -04005530 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005531 setclientid.sc_netid_len =
5532 nfs4_init_callback_netid(clp,
5533 setclientid.sc_netid,
5534 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005535 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005536 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 clp->cl_ipaddr, port >> 8, port & 255);
5538
Jeff Layton3a6bb732015-06-09 19:43:57 -04005539 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005540 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005541 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005542 task = rpc_run_task(&task_setup_data);
5543 if (IS_ERR(task)) {
5544 status = PTR_ERR(task);
5545 goto out;
5546 }
5547 status = task->tk_status;
5548 if (setclientid.sc_cred) {
Chuck Lever05f0b032019-10-04 09:58:54 -04005549 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005550 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5551 put_rpccred(setclientid.sc_cred);
5552 }
5553 rpc_put_task(task);
5554out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005555 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005556 dprintk("NFS reply setclientid: %d\n", status);
5557 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558}
5559
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005560/**
5561 * nfs4_proc_setclientid_confirm - Confirm client ID
5562 * @clp: state data structure
5563 * @res: result of a previous SETCLIENTID
5564 * @cred: RPC credential to use for this call
5565 *
5566 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5567 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005568int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005569 struct nfs4_setclientid_res *arg,
5570 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 struct rpc_message msg = {
5573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005574 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005575 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 int status;
5578
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005579 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5580 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5581 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005582 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005583 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005584 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 return status;
5586}
5587
Trond Myklebustfe650402006-01-03 09:55:18 +01005588struct nfs4_delegreturndata {
5589 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005590 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005591 struct nfs_fh fh;
5592 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005593 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005594 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005595 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005596 struct inode *inode;
5597 bool roc;
5598 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005599};
5600
Trond Myklebustfe650402006-01-03 09:55:18 +01005601static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5602{
5603 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005604
Trond Myklebust14516c32010-07-31 14:29:06 -04005605 if (!nfs4_sequence_done(task, &data->res.seq_res))
5606 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005607
Trond Myklebustca8acf82013-08-13 10:36:56 -04005608 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005609 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005610 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005611 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005612 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005613 case -NFS4ERR_ADMIN_REVOKED:
5614 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005615 case -NFS4ERR_EXPIRED:
5616 nfs4_free_revoked_stateid(data->res.server,
5617 data->args.stateid,
5618 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005619 case -NFS4ERR_BAD_STATEID:
5620 case -NFS4ERR_OLD_STATEID:
5621 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005622 task->tk_status = 0;
5623 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005624 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005625 if (nfs4_async_handle_error(task, data->res.server,
5626 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005627 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005628 return;
5629 }
5630 }
5631 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005632 if (data->roc && data->rpc_status == 0)
5633 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005634}
5635
5636static void nfs4_delegreturn_release(void *calldata)
5637{
Peng Tao039b7562014-07-03 13:05:02 +08005638 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005639 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005640
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005641 if (inode) {
5642 if (data->roc)
5643 pnfs_roc_release(inode);
5644 nfs_iput_and_deactive(inode);
5645 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005646 kfree(calldata);
5647}
5648
Andy Adamson938e1012009-04-01 09:22:28 -04005649static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5650{
5651 struct nfs4_delegreturndata *d_data;
5652
5653 d_data = (struct nfs4_delegreturndata *)data;
5654
Peng Tao500d7012015-09-22 11:35:22 +08005655 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5656 return;
5657
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005658 if (d_data->roc)
5659 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005660
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005661 nfs4_setup_sequence(d_data->res.server,
5662 &d_data->args.seq_args,
5663 &d_data->res.seq_res,
5664 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005665}
Andy Adamson938e1012009-04-01 09:22:28 -04005666
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005667static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005668 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005669 .rpc_call_done = nfs4_delegreturn_done,
5670 .rpc_release = nfs4_delegreturn_release,
5671};
5672
Trond Myklebuste6f81072008-01-24 18:14:34 -05005673static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005674{
5675 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005676 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005677 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005678 struct rpc_message msg = {
5679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5680 .rpc_cred = cred,
5681 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005682 struct rpc_task_setup task_setup_data = {
5683 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005684 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005685 .callback_ops = &nfs4_delegreturn_ops,
5686 .flags = RPC_TASK_ASYNC,
5687 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005688 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005689
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005690 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005691 if (data == NULL)
5692 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005693 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005694
5695 nfs4_state_protect(server->nfs_client,
5696 NFS_SP4_MACH_CRED_CLEANUP,
5697 &task_setup_data.rpc_client, &msg);
5698
Trond Myklebustfe650402006-01-03 09:55:18 +01005699 data->args.fhandle = &data->fh;
5700 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005701 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005702 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005703 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005704 data->res.fattr = &data->fattr;
5705 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005706 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005707 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005708 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005709 data->inode = nfs_igrab_and_active(inode);
5710 if (data->inode)
5711 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005712
Trond Myklebustc970aa82007-07-14 15:39:59 -04005713 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005714 msg.rpc_argp = &data->args;
5715 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005716 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005717 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005718 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005719 if (!issync)
5720 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005721 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005722 if (status != 0)
5723 goto out;
5724 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005725 if (status == 0)
5726 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5727 else
5728 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005729out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005730 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005731 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732}
5733
Trond Myklebuste6f81072008-01-24 18:14:34 -05005734int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
5736 struct nfs_server *server = NFS_SERVER(inode);
5737 struct nfs4_exception exception = { };
5738 int err;
5739 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005740 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005741 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 switch (err) {
5743 case -NFS4ERR_STALE_STATEID:
5744 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 case 0:
5746 return 0;
5747 }
5748 err = nfs4_handle_exception(server, err, &exception);
5749 } while (exception.retry);
5750 return err;
5751}
5752
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5754{
5755 struct inode *inode = state->inode;
5756 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005757 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005758 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005760 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005762 struct nfs_lockt_res res = {
5763 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 };
5765 struct rpc_message msg = {
5766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5767 .rpc_argp = &arg,
5768 .rpc_resp = &res,
5769 .rpc_cred = state->owner->so_cred,
5770 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 struct nfs4_lock_state *lsp;
5772 int status;
5773
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005774 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005775 status = nfs4_set_lock_state(state, request);
5776 if (status != 0)
5777 goto out;
5778 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005779 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005780 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005781 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005782 switch (status) {
5783 case 0:
5784 request->fl_type = F_UNLCK;
5785 break;
5786 case -NFS4ERR_DENIED:
5787 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005789 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005790 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005791out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 return status;
5793}
5794
5795static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5796{
5797 struct nfs4_exception exception = { };
5798 int err;
5799
5800 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005801 err = _nfs4_proc_getlk(state, cmd, request);
5802 trace_nfs4_get_lock(request, state, cmd, err);
5803 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 &exception);
5805 } while (exception.retry);
5806 return err;
5807}
5808
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005809struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005810 struct nfs_locku_args arg;
5811 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005812 struct nfs4_lock_state *lsp;
5813 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005814 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005815 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005816 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005817};
5818
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005819static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5820 struct nfs_open_context *ctx,
5821 struct nfs4_lock_state *lsp,
5822 struct nfs_seqid *seqid)
5823{
5824 struct nfs4_unlockdata *p;
5825 struct inode *inode = lsp->ls_state->inode;
5826
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005827 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005828 if (p == NULL)
5829 return NULL;
5830 p->arg.fh = NFS_FH(inode);
5831 p->arg.fl = &p->fl;
5832 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005833 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005834 p->lsp = lsp;
5835 atomic_inc(&lsp->ls_count);
5836 /* Ensure we don't close file until we're done freeing locks! */
5837 p->ctx = get_nfs_open_context(ctx);
5838 memcpy(&p->fl, fl, sizeof(p->fl));
5839 p->server = NFS_SERVER(inode);
5840 return p;
5841}
5842
Trond Myklebust06f814a2006-01-03 09:55:07 +01005843static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005844{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005845 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005846 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005847 nfs4_put_lock_state(calldata->lsp);
5848 put_nfs_open_context(calldata->ctx);
5849 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005850}
5851
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005852static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005853{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005854 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005855
Trond Myklebust14516c32010-07-31 14:29:06 -04005856 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5857 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005858 switch (task->tk_status) {
5859 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005860 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005861 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005862 if (nfs4_update_lock_stateid(calldata->lsp,
5863 &calldata->res.stateid))
5864 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005865 case -NFS4ERR_ADMIN_REVOKED:
5866 case -NFS4ERR_EXPIRED:
5867 nfs4_free_revoked_stateid(calldata->server,
5868 &calldata->arg.stateid,
5869 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005870 case -NFS4ERR_BAD_STATEID:
5871 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005872 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005873 if (!nfs4_stateid_match(&calldata->arg.stateid,
5874 &calldata->lsp->ls_stateid))
5875 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005876 break;
5877 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005878 if (nfs4_async_handle_error(task, calldata->server,
5879 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005880 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005881 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005882 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005883}
5884
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005885static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005886{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005887 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005889 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005890 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005891 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005892 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005893 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005894 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005895 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005896 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005897 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005898 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005899 &calldata->res.seq_res,
5900 task) != 0)
5901 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005902 return;
5903out_no_action:
5904 task->tk_action = NULL;
5905out_wait:
5906 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907}
5908
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005909static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005910 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005911 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005912 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005913};
5914
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005915static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5916 struct nfs_open_context *ctx,
5917 struct nfs4_lock_state *lsp,
5918 struct nfs_seqid *seqid)
5919{
5920 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005921 struct rpc_message msg = {
5922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5923 .rpc_cred = ctx->cred,
5924 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005925 struct rpc_task_setup task_setup_data = {
5926 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005927 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005928 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005929 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005930 .flags = RPC_TASK_ASYNC,
5931 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005932
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005933 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5934 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5935
Frank Filz137d6ac2007-07-09 15:32:29 -07005936 /* Ensure this is an unlock - when canceling a lock, the
5937 * canceled lock is passed in, and it won't be an unlock.
5938 */
5939 fl->fl_type = F_UNLCK;
5940
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005941 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5942 if (data == NULL) {
5943 nfs_free_seqid(seqid);
5944 return ERR_PTR(-ENOMEM);
5945 }
5946
Chuck Levera9c92d62013-08-09 12:48:18 -04005947 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005948 msg.rpc_argp = &data->arg;
5949 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005950 task_setup_data.callback_data = data;
5951 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005952}
5953
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5955{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005956 struct inode *inode = state->inode;
5957 struct nfs4_state_owner *sp = state->owner;
5958 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005959 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005960 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005961 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005962 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005963 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005964 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
Trond Myklebust9b073572006-06-29 16:38:34 -04005966 status = nfs4_set_lock_state(state, request);
5967 /* Unlock _before_ we do the RPC call */
5968 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005969 /* Exclude nfs_delegation_claim_locks() */
5970 mutex_lock(&sp->so_delegreturn_mutex);
5971 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005972 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005973 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005974 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005975 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005976 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005977 }
5978 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005979 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005980 if (status != 0)
5981 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005982 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005983 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005984 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5985 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005986 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5987 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005988 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005989 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005990 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005991 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005992 status = PTR_ERR(task);
5993 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005994 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005996 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005997out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005998 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005999 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006000 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001}
6002
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003struct nfs4_lockdata {
6004 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006005 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006006 struct nfs4_lock_state *lsp;
6007 struct nfs_open_context *ctx;
6008 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006009 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010 int rpc_status;
6011 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006012 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006013};
6014
6015static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006016 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6017 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006018{
6019 struct nfs4_lockdata *p;
6020 struct inode *inode = lsp->ls_state->inode;
6021 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006022 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006023
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006024 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006025 if (p == NULL)
6026 return NULL;
6027
6028 p->arg.fh = NFS_FH(inode);
6029 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006030 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006031 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006032 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006033 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6034 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006035 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006036 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006037 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006038 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006039 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006040 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006041 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006042 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006043 atomic_inc(&lsp->ls_count);
6044 p->ctx = get_nfs_open_context(ctx);
6045 memcpy(&p->fl, fl, sizeof(p->fl));
6046 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006047out_free_seqid:
6048 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006049out_free:
6050 kfree(p);
6051 return NULL;
6052}
6053
6054static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6055{
6056 struct nfs4_lockdata *data = calldata;
6057 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
Harvey Harrison3110ff82008-05-02 13:42:44 -07006059 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006060 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006061 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006062 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006063 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006064 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006065 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006066 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006067 nfs4_stateid_copy(&data->arg.open_stateid,
6068 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006069 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006070 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006071 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006072 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006073 nfs4_stateid_copy(&data->arg.lock_stateid,
6074 &data->lsp->ls_stateid);
6075 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006076 if (!nfs4_valid_open_stateid(state)) {
6077 data->rpc_status = -EBADF;
6078 task->tk_action = NULL;
6079 goto out_release_open_seqid;
6080 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006081 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006082 if (nfs4_setup_sequence(data->server,
6083 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006084 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006085 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006086 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006087out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006088 nfs_release_seqid(data->arg.open_seqid);
6089out_release_lock_seqid:
6090 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006091out_wait:
6092 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006093 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006094}
6095
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006096static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6097{
6098 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006099 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006100
Harvey Harrison3110ff82008-05-02 13:42:44 -07006101 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006102
Trond Myklebust14516c32010-07-31 14:29:06 -04006103 if (!nfs4_sequence_done(task, &data->res.seq_res))
6104 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006105
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006106 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006107 switch (task->tk_status) {
6108 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006109 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006110 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006111 if (data->arg.new_lock) {
6112 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006113 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006114 rpc_restart_call_prepare(task);
6115 break;
6116 }
6117 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006118 if (data->arg.new_lock_owner != 0) {
6119 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6120 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6121 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6122 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6123 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006124 break;
6125 case -NFS4ERR_BAD_STATEID:
6126 case -NFS4ERR_OLD_STATEID:
6127 case -NFS4ERR_STALE_STATEID:
6128 case -NFS4ERR_EXPIRED:
6129 if (data->arg.new_lock_owner != 0) {
6130 if (!nfs4_stateid_match(&data->arg.open_stateid,
6131 &lsp->ls_state->open_stateid))
6132 rpc_restart_call_prepare(task);
6133 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6134 &lsp->ls_stateid))
6135 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006136 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006137 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138}
6139
6140static void nfs4_lock_release(void *calldata)
6141{
6142 struct nfs4_lockdata *data = calldata;
6143
Harvey Harrison3110ff82008-05-02 13:42:44 -07006144 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006145 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006146 if (data->cancelled != 0) {
6147 struct rpc_task *task;
6148 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6149 data->arg.lock_seqid);
6150 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006151 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006152 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153 } else
6154 nfs_free_seqid(data->arg.lock_seqid);
6155 nfs4_put_lock_state(data->lsp);
6156 put_nfs_open_context(data->ctx);
6157 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006158 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006159}
6160
6161static const struct rpc_call_ops nfs4_lock_ops = {
6162 .rpc_call_prepare = nfs4_lock_prepare,
6163 .rpc_call_done = nfs4_lock_done,
6164 .rpc_release = nfs4_lock_release,
6165};
6166
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006167static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6168{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006169 switch (error) {
6170 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006171 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006172 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006173 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006174 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006175 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006176 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006177 break;
6178 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006179 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006180 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006181 };
6182}
6183
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006184static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185{
6186 struct nfs4_lockdata *data;
6187 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006188 struct rpc_message msg = {
6189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6190 .rpc_cred = state->owner->so_cred,
6191 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006192 struct rpc_task_setup task_setup_data = {
6193 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006194 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006195 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006196 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006197 .flags = RPC_TASK_ASYNC,
6198 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006199 int ret;
6200
Harvey Harrison3110ff82008-05-02 13:42:44 -07006201 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006202 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006203 fl->fl_u.nfs4_fl.owner,
6204 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205 if (data == NULL)
6206 return -ENOMEM;
6207 if (IS_SETLKW(cmd))
6208 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006209 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006210 msg.rpc_argp = &data->arg;
6211 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006212 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006213 if (recovery_type > NFS_LOCK_NEW) {
6214 if (recovery_type == NFS_LOCK_RECLAIM)
6215 data->arg.reclaim = NFS_LOCK_RECLAIM;
6216 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006217 } else
6218 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006219 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006220 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006221 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 ret = nfs4_wait_for_completion_rpc_task(task);
6223 if (ret == 0) {
6224 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006225 if (ret)
6226 nfs4_handle_setlk_error(data->server, data->lsp,
6227 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006228 } else
6229 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006230 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006231 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006232 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006233 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
6236static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6237{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006238 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006239 struct nfs4_exception exception = {
6240 .inode = state->inode,
6241 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006242 int err;
6243
6244 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006245 /* Cache the lock if possible... */
6246 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6247 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006248 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006249 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006250 break;
6251 nfs4_handle_exception(server, err, &exception);
6252 } while (exception.retry);
6253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254}
6255
6256static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6257{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006258 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006259 struct nfs4_exception exception = {
6260 .inode = state->inode,
6261 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006262 int err;
6263
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006264 err = nfs4_set_lock_state(state, request);
6265 if (err != 0)
6266 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006267 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006268 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6269 return 0;
6270 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006271 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006272 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6273 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006274 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006275 switch (err) {
6276 default:
6277 goto out;
6278 case -NFS4ERR_GRACE:
6279 case -NFS4ERR_DELAY:
6280 nfs4_handle_exception(server, err, &exception);
6281 err = 0;
6282 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006283 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006284out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006285 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286}
6287
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006288#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006289static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6290{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006291 struct nfs4_lock_state *lsp;
6292 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006293
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006294 status = nfs4_set_lock_state(state, request);
6295 if (status != 0)
6296 return status;
6297 lsp = request->fl_u.nfs4_fl.owner;
6298 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6299 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6300 return 0;
6301 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006302 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006303}
6304#endif
6305
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6307{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006308 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006309 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006310 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006311 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
Trond Myklebust01c3b862006-06-29 16:38:39 -04006313 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006314 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006315 if (status < 0)
6316 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006317 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006318 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006319 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006320 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006321 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006322 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006323 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006324 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006325 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006326 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006327 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006328 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006329 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006330 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006331out:
6332 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 return status;
6334}
6335
6336static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6337{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006338 struct nfs4_exception exception = {
6339 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006340 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006341 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 int err;
6343
6344 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006345 err = _nfs4_proc_setlk(state, cmd, request);
6346 if (err == -NFS4ERR_DENIED)
6347 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006349 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 } while (exception.retry);
6351 return err;
6352}
6353
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006354#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6355#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6356
6357static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006358nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6359 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006360{
6361 int status = -ERESTARTSYS;
6362 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6363
6364 while(!signalled()) {
6365 status = nfs4_proc_setlk(state, cmd, request);
6366 if ((status != -EAGAIN) || IS_SETLK(cmd))
6367 break;
6368 freezable_schedule_timeout_interruptible(timeout);
6369 timeout *= 2;
6370 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6371 status = -ERESTARTSYS;
6372 }
6373 return status;
6374}
6375
Jeff Laytona1d617d82016-09-17 18:17:39 -04006376#ifdef CONFIG_NFS_V4_1
6377struct nfs4_lock_waiter {
6378 struct task_struct *task;
6379 struct inode *inode;
6380 struct nfs_lowner *owner;
6381 bool notified;
6382};
6383
6384static int
6385nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6386{
6387 int ret;
6388 struct cb_notify_lock_args *cbnl = key;
6389 struct nfs4_lock_waiter *waiter = wait->private;
6390 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6391 *wowner = waiter->owner;
6392
6393 /* Only wake if the callback was for the same owner */
6394 if (lowner->clientid != wowner->clientid ||
6395 lowner->id != wowner->id ||
6396 lowner->s_dev != wowner->s_dev)
6397 return 0;
6398
6399 /* Make sure it's for the right inode */
6400 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6401 return 0;
6402
6403 waiter->notified = true;
6404
6405 /* override "private" so we can use default_wake_function */
6406 wait->private = waiter->task;
6407 ret = autoremove_wake_function(wait, mode, flags, key);
6408 wait->private = waiter;
6409 return ret;
6410}
6411
6412static int
6413nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6414{
6415 int status = -ERESTARTSYS;
6416 unsigned long flags;
6417 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6418 struct nfs_server *server = NFS_SERVER(state->inode);
6419 struct nfs_client *clp = server->nfs_client;
6420 wait_queue_head_t *q = &clp->cl_lock_waitq;
6421 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6422 .id = lsp->ls_seqid.owner_id,
6423 .s_dev = server->s_dev };
6424 struct nfs4_lock_waiter waiter = { .task = current,
6425 .inode = state->inode,
6426 .owner = &owner,
6427 .notified = false };
6428 wait_queue_t wait;
6429
6430 /* Don't bother with waitqueue if we don't expect a callback */
6431 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6432 return nfs4_retry_setlk_simple(state, cmd, request);
6433
6434 init_wait(&wait);
6435 wait.private = &waiter;
6436 wait.func = nfs4_wake_lock_waiter;
6437 add_wait_queue(q, &wait);
6438
6439 while(!signalled()) {
6440 status = nfs4_proc_setlk(state, cmd, request);
6441 if ((status != -EAGAIN) || IS_SETLK(cmd))
6442 break;
6443
6444 status = -ERESTARTSYS;
6445 spin_lock_irqsave(&q->lock, flags);
6446 if (waiter.notified) {
6447 spin_unlock_irqrestore(&q->lock, flags);
6448 continue;
6449 }
6450 set_current_state(TASK_INTERRUPTIBLE);
6451 spin_unlock_irqrestore(&q->lock, flags);
6452
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006453 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006454 }
6455
6456 finish_wait(q, &wait);
6457 return status;
6458}
6459#else /* !CONFIG_NFS_V4_1 */
6460static inline int
6461nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6462{
6463 return nfs4_retry_setlk_simple(state, cmd, request);
6464}
6465#endif
6466
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467static int
6468nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6469{
6470 struct nfs_open_context *ctx;
6471 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 int status;
6473
6474 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006475 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 state = ctx->state;
6477
6478 if (request->fl_start < 0 || request->fl_end < 0)
6479 return -EINVAL;
6480
Trond Myklebustd9531262009-07-21 19:22:38 -04006481 if (IS_GETLK(cmd)) {
6482 if (state != NULL)
6483 return nfs4_proc_getlk(state, F_GETLK, request);
6484 return 0;
6485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486
6487 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6488 return -EINVAL;
6489
Trond Myklebustd9531262009-07-21 19:22:38 -04006490 if (request->fl_type == F_UNLCK) {
6491 if (state != NULL)
6492 return nfs4_proc_unlck(state, cmd, request);
6493 return 0;
6494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495
Trond Myklebustd9531262009-07-21 19:22:38 -04006496 if (state == NULL)
6497 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006498
6499 if ((request->fl_flags & FL_POSIX) &&
6500 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6501 return -ENOLCK;
6502
Trond Myklebust55725512012-04-18 12:48:35 -04006503 /*
6504 * Don't rely on the VFS having checked the file open mode,
6505 * since it won't do this for flock() locks.
6506 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006507 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006508 case F_RDLCK:
6509 if (!(filp->f_mode & FMODE_READ))
6510 return -EBADF;
6511 break;
6512 case F_WRLCK:
6513 if (!(filp->f_mode & FMODE_WRITE))
6514 return -EBADF;
6515 }
6516
Jeff Layton1ea67db2016-09-17 18:17:37 -04006517 status = nfs4_set_lock_state(state, request);
6518 if (status != 0)
6519 return status;
6520
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006521 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522}
6523
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006524int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006525{
6526 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006527 int err;
6528
6529 err = nfs4_set_lock_state(state, fl);
6530 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006531 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006532 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrown70f254a2017-12-13 09:57:09 +11006533 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006534}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006535
Trond Myklebustcf470c32012-03-07 13:49:12 -05006536struct nfs_release_lockowner_data {
6537 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006538 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006539 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006540 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006541 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006542};
6543
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006544static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6545{
6546 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006547 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006548 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6549 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006550 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006551 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006552}
6553
6554static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6555{
6556 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006557 struct nfs_server *server = data->server;
6558
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006559 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006560
6561 switch (task->tk_status) {
6562 case 0:
6563 renew_lease(server, data->timestamp);
6564 break;
6565 case -NFS4ERR_STALE_CLIENTID:
6566 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006567 nfs4_schedule_lease_recovery(server->nfs_client);
6568 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006569 case -NFS4ERR_LEASE_MOVED:
6570 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006571 if (nfs4_async_handle_error(task, server,
6572 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006573 rpc_restart_call_prepare(task);
6574 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006575}
6576
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006577static void nfs4_release_lockowner_release(void *calldata)
6578{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006579 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006580 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006581 kfree(calldata);
6582}
6583
Trond Myklebust17280172012-03-11 13:11:00 -04006584static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006585 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6586 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006587 .rpc_release = nfs4_release_lockowner_release,
6588};
6589
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006590static void
6591nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006592{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006593 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006594 struct rpc_message msg = {
6595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6596 };
6597
6598 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006599 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006600
Trond Myklebustcf470c32012-03-07 13:49:12 -05006601 data = kmalloc(sizeof(*data), GFP_NOFS);
6602 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006603 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006604 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006605 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006606 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6607 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6608 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006609
Trond Myklebustcf470c32012-03-07 13:49:12 -05006610 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006611 msg.rpc_resp = &data->res;
6612 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006613 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006614}
6615
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006616#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6617
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006618static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006619 struct dentry *unused, struct inode *inode,
6620 const char *key, const void *buf,
6621 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006622{
Al Viro59301222016-05-27 10:19:30 -04006623 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006624}
6625
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006626static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006627 struct dentry *unused, struct inode *inode,
6628 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006629{
Al Virob2968212016-04-10 20:48:24 -04006630 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006631}
6632
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006633static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006634{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006635 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006636}
6637
David Quigleyc9bccef2013-05-22 12:50:45 -04006638#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006639
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006640static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006641 struct dentry *unused, struct inode *inode,
6642 const char *key, const void *buf,
6643 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006644{
6645 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006646 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006647
6648 return -EOPNOTSUPP;
6649}
6650
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006651static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006652 struct dentry *unused, struct inode *inode,
6653 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006654{
6655 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006656 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006657 return -EOPNOTSUPP;
6658}
6659
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006660static ssize_t
6661nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006662{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006663 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006664
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006665 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6666 len = security_inode_listsecurity(inode, list, list_len);
6667 if (list_len && len > list_len)
6668 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006669 }
6670 return len;
6671}
6672
6673static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6674 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006675 .get = nfs4_xattr_get_nfs4_label,
6676 .set = nfs4_xattr_set_nfs4_label,
6677};
David Quigleyc9bccef2013-05-22 12:50:45 -04006678
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006679#else
6680
6681static ssize_t
6682nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6683{
6684 return 0;
6685}
6686
6687#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006688
Andy Adamson533eb462011-06-13 18:25:56 -04006689/*
6690 * nfs_fhget will use either the mounted_on_fileid or the fileid
6691 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006692static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6693{
Andy Adamson533eb462011-06-13 18:25:56 -04006694 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6695 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6696 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006697 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006698 return;
6699
6700 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006701 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006702 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6703 fattr->nlink = 2;
6704}
6705
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006706static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6707 const struct qstr *name,
6708 struct nfs4_fs_locations *fs_locations,
6709 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006710{
6711 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006712 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006713 struct nfs4_fs_locations_arg args = {
6714 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006715 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006716 .page = page,
6717 .bitmask = bitmask,
6718 };
Benny Halevy22958462009-04-01 09:22:02 -04006719 struct nfs4_fs_locations_res res = {
6720 .fs_locations = fs_locations,
6721 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006722 struct rpc_message msg = {
6723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6724 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006725 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006726 };
6727 int status;
6728
Harvey Harrison3110ff82008-05-02 13:42:44 -07006729 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006730
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006731 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6732 bitmask[1] = nfs4_fattr_bitmap[1];
6733
Andy Adamson533eb462011-06-13 18:25:56 -04006734 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6735 * is not supported */
6736 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006737 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006738 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006739 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006740
Trond Myklebustc228fd32007-01-13 02:28:11 -05006741 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006742 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006743 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006744 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006745 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006746 return status;
6747}
6748
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006749int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6750 const struct qstr *name,
6751 struct nfs4_fs_locations *fs_locations,
6752 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006753{
6754 struct nfs4_exception exception = { };
6755 int err;
6756 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006757 err = _nfs4_proc_fs_locations(client, dir, name,
6758 fs_locations, page);
6759 trace_nfs4_get_fs_locations(dir, name, err);
6760 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006761 &exception);
6762 } while (exception.retry);
6763 return err;
6764}
6765
Chuck Leverb03d7352013-10-17 14:12:50 -04006766/*
6767 * This operation also signals the server that this client is
6768 * performing migration recovery. The server can stop returning
6769 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6770 * appended to this compound to identify the client ID which is
6771 * performing recovery.
6772 */
6773static int _nfs40_proc_get_locations(struct inode *inode,
6774 struct nfs4_fs_locations *locations,
6775 struct page *page, struct rpc_cred *cred)
6776{
6777 struct nfs_server *server = NFS_SERVER(inode);
6778 struct rpc_clnt *clnt = server->client;
6779 u32 bitmask[2] = {
6780 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6781 };
6782 struct nfs4_fs_locations_arg args = {
6783 .clientid = server->nfs_client->cl_clientid,
6784 .fh = NFS_FH(inode),
6785 .page = page,
6786 .bitmask = bitmask,
6787 .migration = 1, /* skip LOOKUP */
6788 .renew = 1, /* append RENEW */
6789 };
6790 struct nfs4_fs_locations_res res = {
6791 .fs_locations = locations,
6792 .migration = 1,
6793 .renew = 1,
6794 };
6795 struct rpc_message msg = {
6796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6797 .rpc_argp = &args,
6798 .rpc_resp = &res,
6799 .rpc_cred = cred,
6800 };
6801 unsigned long now = jiffies;
6802 int status;
6803
6804 nfs_fattr_init(&locations->fattr);
6805 locations->server = server;
6806 locations->nlocations = 0;
6807
6808 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6809 nfs4_set_sequence_privileged(&args.seq_args);
6810 status = nfs4_call_sync_sequence(clnt, server, &msg,
6811 &args.seq_args, &res.seq_res);
6812 if (status)
6813 return status;
6814
6815 renew_lease(server, now);
6816 return 0;
6817}
6818
6819#ifdef CONFIG_NFS_V4_1
6820
6821/*
6822 * This operation also signals the server that this client is
6823 * performing migration recovery. The server can stop asserting
6824 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6825 * performing this operation is identified in the SEQUENCE
6826 * operation in this compound.
6827 *
6828 * When the client supports GETATTR(fs_locations_info), it can
6829 * be plumbed in here.
6830 */
6831static int _nfs41_proc_get_locations(struct inode *inode,
6832 struct nfs4_fs_locations *locations,
6833 struct page *page, struct rpc_cred *cred)
6834{
6835 struct nfs_server *server = NFS_SERVER(inode);
6836 struct rpc_clnt *clnt = server->client;
6837 u32 bitmask[2] = {
6838 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6839 };
6840 struct nfs4_fs_locations_arg args = {
6841 .fh = NFS_FH(inode),
6842 .page = page,
6843 .bitmask = bitmask,
6844 .migration = 1, /* skip LOOKUP */
6845 };
6846 struct nfs4_fs_locations_res res = {
6847 .fs_locations = locations,
6848 .migration = 1,
6849 };
6850 struct rpc_message msg = {
6851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6852 .rpc_argp = &args,
6853 .rpc_resp = &res,
6854 .rpc_cred = cred,
6855 };
6856 int status;
6857
6858 nfs_fattr_init(&locations->fattr);
6859 locations->server = server;
6860 locations->nlocations = 0;
6861
6862 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6863 nfs4_set_sequence_privileged(&args.seq_args);
6864 status = nfs4_call_sync_sequence(clnt, server, &msg,
6865 &args.seq_args, &res.seq_res);
6866 if (status == NFS4_OK &&
6867 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6868 status = -NFS4ERR_LEASE_MOVED;
6869 return status;
6870}
6871
6872#endif /* CONFIG_NFS_V4_1 */
6873
6874/**
6875 * nfs4_proc_get_locations - discover locations for a migrated FSID
6876 * @inode: inode on FSID that is migrating
6877 * @locations: result of query
6878 * @page: buffer
6879 * @cred: credential to use for this operation
6880 *
6881 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6882 * operation failed, or a negative errno if a local error occurred.
6883 *
6884 * On success, "locations" is filled in, but if the server has
6885 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6886 * asserted.
6887 *
6888 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6889 * from this client that require migration recovery.
6890 */
6891int nfs4_proc_get_locations(struct inode *inode,
6892 struct nfs4_fs_locations *locations,
6893 struct page *page, struct rpc_cred *cred)
6894{
6895 struct nfs_server *server = NFS_SERVER(inode);
6896 struct nfs_client *clp = server->nfs_client;
6897 const struct nfs4_mig_recovery_ops *ops =
6898 clp->cl_mvops->mig_recovery_ops;
6899 struct nfs4_exception exception = { };
6900 int status;
6901
6902 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6903 (unsigned long long)server->fsid.major,
6904 (unsigned long long)server->fsid.minor,
6905 clp->cl_hostname);
6906 nfs_display_fhandle(NFS_FH(inode), __func__);
6907
6908 do {
6909 status = ops->get_locations(inode, locations, page, cred);
6910 if (status != -NFS4ERR_DELAY)
6911 break;
6912 nfs4_handle_exception(server, status, &exception);
6913 } while (exception.retry);
6914 return status;
6915}
6916
Chuck Lever44c99932013-10-17 14:13:30 -04006917/*
6918 * This operation also signals the server that this client is
6919 * performing "lease moved" recovery. The server can stop
6920 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6921 * is appended to this compound to identify the client ID which is
6922 * performing recovery.
6923 */
6924static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6925{
6926 struct nfs_server *server = NFS_SERVER(inode);
6927 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6928 struct rpc_clnt *clnt = server->client;
6929 struct nfs4_fsid_present_arg args = {
6930 .fh = NFS_FH(inode),
6931 .clientid = clp->cl_clientid,
6932 .renew = 1, /* append RENEW */
6933 };
6934 struct nfs4_fsid_present_res res = {
6935 .renew = 1,
6936 };
6937 struct rpc_message msg = {
6938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6939 .rpc_argp = &args,
6940 .rpc_resp = &res,
6941 .rpc_cred = cred,
6942 };
6943 unsigned long now = jiffies;
6944 int status;
6945
6946 res.fh = nfs_alloc_fhandle();
6947 if (res.fh == NULL)
6948 return -ENOMEM;
6949
6950 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6951 nfs4_set_sequence_privileged(&args.seq_args);
6952 status = nfs4_call_sync_sequence(clnt, server, &msg,
6953 &args.seq_args, &res.seq_res);
6954 nfs_free_fhandle(res.fh);
6955 if (status)
6956 return status;
6957
6958 do_renew_lease(clp, now);
6959 return 0;
6960}
6961
6962#ifdef CONFIG_NFS_V4_1
6963
6964/*
6965 * This operation also signals the server that this client is
6966 * performing "lease moved" recovery. The server can stop asserting
6967 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6968 * this operation is identified in the SEQUENCE operation in this
6969 * compound.
6970 */
6971static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6972{
6973 struct nfs_server *server = NFS_SERVER(inode);
6974 struct rpc_clnt *clnt = server->client;
6975 struct nfs4_fsid_present_arg args = {
6976 .fh = NFS_FH(inode),
6977 };
6978 struct nfs4_fsid_present_res res = {
6979 };
6980 struct rpc_message msg = {
6981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6982 .rpc_argp = &args,
6983 .rpc_resp = &res,
6984 .rpc_cred = cred,
6985 };
6986 int status;
6987
6988 res.fh = nfs_alloc_fhandle();
6989 if (res.fh == NULL)
6990 return -ENOMEM;
6991
6992 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6993 nfs4_set_sequence_privileged(&args.seq_args);
6994 status = nfs4_call_sync_sequence(clnt, server, &msg,
6995 &args.seq_args, &res.seq_res);
6996 nfs_free_fhandle(res.fh);
6997 if (status == NFS4_OK &&
6998 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6999 status = -NFS4ERR_LEASE_MOVED;
7000 return status;
7001}
7002
7003#endif /* CONFIG_NFS_V4_1 */
7004
7005/**
7006 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7007 * @inode: inode on FSID to check
7008 * @cred: credential to use for this operation
7009 *
7010 * Server indicates whether the FSID is present, moved, or not
7011 * recognized. This operation is necessary to clear a LEASE_MOVED
7012 * condition for this client ID.
7013 *
7014 * Returns NFS4_OK if the FSID is present on this server,
7015 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7016 * NFS4ERR code if some error occurred on the server, or a
7017 * negative errno if a local failure occurred.
7018 */
7019int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7020{
7021 struct nfs_server *server = NFS_SERVER(inode);
7022 struct nfs_client *clp = server->nfs_client;
7023 const struct nfs4_mig_recovery_ops *ops =
7024 clp->cl_mvops->mig_recovery_ops;
7025 struct nfs4_exception exception = { };
7026 int status;
7027
7028 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7029 (unsigned long long)server->fsid.major,
7030 (unsigned long long)server->fsid.minor,
7031 clp->cl_hostname);
7032 nfs_display_fhandle(NFS_FH(inode), __func__);
7033
7034 do {
7035 status = ops->fsid_present(inode, cred);
7036 if (status != -NFS4ERR_DELAY)
7037 break;
7038 nfs4_handle_exception(server, status, &exception);
7039 } while (exception.retry);
7040 return status;
7041}
7042
Andy Adamson5ec16a82013-08-08 10:57:55 -04007043/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007044 * If 'use_integrity' is true and the state managment nfs_client
7045 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7046 * and the machine credential as per RFC3530bis and RFC5661 Security
7047 * Considerations sections. Otherwise, just use the user cred with the
7048 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007049 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007050static 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 +00007051{
7052 int status;
7053 struct nfs4_secinfo_arg args = {
7054 .dir_fh = NFS_FH(dir),
7055 .name = name,
7056 };
7057 struct nfs4_secinfo_res res = {
7058 .flavors = flavors,
7059 };
7060 struct rpc_message msg = {
7061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7062 .rpc_argp = &args,
7063 .rpc_resp = &res,
7064 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007065 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007066 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007067
7068 if (use_integrity) {
7069 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007070 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7071 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007072 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007073
7074 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007075
7076 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7077 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7078
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007079 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7080 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007081 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007082
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007083 if (cred)
7084 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007085
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007086 return status;
7087}
7088
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007089int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7090 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007091{
7092 struct nfs4_exception exception = { };
7093 int err;
7094 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007095 err = -NFS4ERR_WRONGSEC;
7096
7097 /* try to use integrity protection with machine cred */
7098 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7099 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7100
7101 /*
7102 * if unable to use integrity protection, or SECINFO with
7103 * integrity protection returns NFS4ERR_WRONGSEC (which is
7104 * disallowed by spec, but exists in deployed servers) use
7105 * the current filesystem's rpc_client and the user cred.
7106 */
7107 if (err == -NFS4ERR_WRONGSEC)
7108 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7109
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007110 trace_nfs4_secinfo(dir, name, err);
7111 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007112 &exception);
7113 } while (exception.retry);
7114 return err;
7115}
7116
Andy Adamson557134a2009-04-01 09:21:53 -04007117#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007118/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007119 * Check the exchange flags returned by the server for invalid flags, having
7120 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7121 * DS flags set.
7122 */
7123static int nfs4_check_cl_exchange_flags(u32 flags)
7124{
7125 if (flags & ~EXCHGID4_FLAG_MASK_R)
7126 goto out_inval;
7127 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7128 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7129 goto out_inval;
7130 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7131 goto out_inval;
7132 return NFS_OK;
7133out_inval:
7134 return -NFS4ERR_INVAL;
7135}
7136
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007137static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007138nfs41_same_server_scope(struct nfs41_server_scope *a,
7139 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007140{
7141 if (a->server_scope_sz == b->server_scope_sz &&
7142 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7143 return true;
7144
7145 return false;
7146}
7147
Andy Adamson02a95de2016-02-05 16:08:37 -05007148static void
7149nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7150{
7151}
7152
7153static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7154 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7155};
7156
Andy Adamson357f54d2010-12-14 10:11:57 -05007157/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007158 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007159 *
7160 * The 4.1 client currently uses the same TCP connection for the
7161 * fore and backchannel.
7162 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007163static
7164int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7165 struct rpc_xprt *xprt,
7166 struct nfs_client *clp,
7167 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007168{
7169 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007170 struct nfs41_bind_conn_to_session_args args = {
7171 .client = clp,
7172 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7173 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007174 struct nfs41_bind_conn_to_session_res res;
7175 struct rpc_message msg = {
7176 .rpc_proc =
7177 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007178 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007179 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007180 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007181 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007182 struct rpc_task_setup task_setup_data = {
7183 .rpc_client = clnt,
7184 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007185 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007186 .rpc_message = &msg,
7187 .flags = RPC_TASK_TIMEOUT,
7188 };
7189 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007190
7191 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007192
Trond Myklebust71a097c2015-02-18 09:27:18 -08007193 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7194 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7195 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007196
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007197 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7198 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7199 args.dir = NFS4_CDFC4_FORE;
7200
7201 task = rpc_run_task(&task_setup_data);
7202 if (!IS_ERR(task)) {
7203 status = task->tk_status;
7204 rpc_put_task(task);
7205 } else
7206 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007207 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007208 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007209 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007210 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7211 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7212 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007213 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007214 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007215 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007216 dprintk("NFS: %s: Unexpected direction from server\n",
7217 __func__);
7218 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007219 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007220 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007221 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007222 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7223 __func__);
7224 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007225 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007226 }
7227 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007228out:
7229 dprintk("<-- %s status= %d\n", __func__, status);
7230 return status;
7231}
7232
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007233struct rpc_bind_conn_calldata {
7234 struct nfs_client *clp;
7235 struct rpc_cred *cred;
7236};
7237
7238static int
7239nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7240 struct rpc_xprt *xprt,
7241 void *calldata)
7242{
7243 struct rpc_bind_conn_calldata *p = calldata;
7244
7245 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7246}
7247
7248int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7249{
7250 struct rpc_bind_conn_calldata data = {
7251 .clp = clp,
7252 .cred = cred,
7253 };
7254 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7255 nfs4_proc_bind_conn_to_session_callback, &data);
7256}
7257
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007258/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007259 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7260 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007261 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007262static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7263 .how = SP4_MACH_CRED,
7264 .enforce.u.words = {
7265 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7266 1 << (OP_EXCHANGE_ID - 32) |
7267 1 << (OP_CREATE_SESSION - 32) |
7268 1 << (OP_DESTROY_SESSION - 32) |
7269 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007270 },
7271 .allow.u.words = {
7272 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007273 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007274 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007275 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007276 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007277 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007278 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007279 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007280 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007281 1 << (OP_FREE_STATEID - 32) |
7282 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007283 }
7284};
7285
7286/*
7287 * Select the state protection mode for client `clp' given the server results
7288 * from exchange_id in `sp'.
7289 *
7290 * Returns 0 on success, negative errno otherwise.
7291 */
7292static int nfs4_sp4_select_mode(struct nfs_client *clp,
7293 struct nfs41_state_protection *sp)
7294{
7295 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7296 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7297 1 << (OP_EXCHANGE_ID - 32) |
7298 1 << (OP_CREATE_SESSION - 32) |
7299 1 << (OP_DESTROY_SESSION - 32) |
7300 1 << (OP_DESTROY_CLIENTID - 32)
7301 };
7302 unsigned int i;
7303
7304 if (sp->how == SP4_MACH_CRED) {
7305 /* Print state protect result */
7306 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7307 for (i = 0; i <= LAST_NFS4_OP; i++) {
7308 if (test_bit(i, sp->enforce.u.longs))
7309 dfprintk(MOUNT, " enforce op %d\n", i);
7310 if (test_bit(i, sp->allow.u.longs))
7311 dfprintk(MOUNT, " allow op %d\n", i);
7312 }
7313
7314 /* make sure nothing is on enforce list that isn't supported */
7315 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7316 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7317 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7318 return -EINVAL;
7319 }
7320 }
7321
7322 /*
7323 * Minimal mode - state operations are allowed to use machine
7324 * credential. Note this already happens by default, so the
7325 * client doesn't have to do anything more than the negotiation.
7326 *
7327 * NOTE: we don't care if EXCHANGE_ID is in the list -
7328 * we're already using the machine cred for exchange_id
7329 * and will never use a different cred.
7330 */
7331 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7332 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7333 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7334 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7335 dfprintk(MOUNT, "sp4_mach_cred:\n");
7336 dfprintk(MOUNT, " minimal mode enabled\n");
7337 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7338 } else {
7339 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7340 return -EINVAL;
7341 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007342
7343 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007344 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7345 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007346 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7347 dfprintk(MOUNT, " cleanup mode enabled\n");
7348 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7349 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007350
Andrew Elble99ade3c2015-12-02 09:39:51 -05007351 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7352 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7353 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7354 &clp->cl_sp4_flags);
7355 }
7356
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007357 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7358 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7359 dfprintk(MOUNT, " secinfo mode enabled\n");
7360 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7361 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007362
7363 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7364 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7365 dfprintk(MOUNT, " stateid mode enabled\n");
7366 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7367 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007368
7369 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7370 dfprintk(MOUNT, " write mode enabled\n");
7371 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7372 }
7373
7374 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7375 dfprintk(MOUNT, " commit mode enabled\n");
7376 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7377 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007378 }
7379
7380 return 0;
7381}
7382
Andy Adamson8d89bd72016-09-09 09:22:18 -04007383struct nfs41_exchange_id_data {
7384 struct nfs41_exchange_id_res res;
7385 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007386 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007387 int rpc_status;
7388};
7389
7390static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007391{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007392 struct nfs41_exchange_id_data *cdata =
7393 (struct nfs41_exchange_id_data *)data;
7394 struct nfs_client *clp = cdata->args.client;
7395 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007396
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007397 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007398
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007399 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007400 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007401
7402 if (cdata->xprt && status == 0) {
7403 status = nfs4_detect_session_trunking(clp, &cdata->res,
7404 cdata->xprt);
7405 goto out;
7406 }
7407
Andy Adamson8d89bd72016-09-09 09:22:18 -04007408 if (status == 0)
7409 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007410
Chuck Lever177313f2012-05-21 22:44:58 -04007411 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412 clp->cl_clientid = cdata->res.clientid;
7413 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007414 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007415 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007416 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007417 &clp->cl_session->session_state);
7418 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007419 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007420
Chuck Leveracdeb692012-05-21 22:46:16 -04007421 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007422 clp->cl_serverowner = cdata->res.server_owner;
7423 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007424
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007425 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007426 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007427 clp->cl_implid = cdata->res.impl_id;
7428 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007429
Chuck Lever177313f2012-05-21 22:44:58 -04007430 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007431 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007432 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007433 dprintk("%s: server_scope mismatch detected\n",
7434 __func__);
7435 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007436 kfree(clp->cl_serverscope);
7437 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007438 }
7439
Chuck Lever177313f2012-05-21 22:44:58 -04007440 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007441 clp->cl_serverscope = cdata->res.server_scope;
7442 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007443 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007444 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007445 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007446 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007447 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007448out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007449 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007450 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451}
7452
7453static void nfs4_exchange_id_release(void *data)
7454{
7455 struct nfs41_exchange_id_data *cdata =
7456 (struct nfs41_exchange_id_data *)data;
7457
Andy Adamsonad0849a2016-09-09 09:22:28 -04007458 if (cdata->xprt) {
7459 xprt_put(cdata->xprt);
7460 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7461 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007462 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 kfree(cdata->res.impl_id);
7464 kfree(cdata->res.server_scope);
7465 kfree(cdata->res.server_owner);
7466 kfree(cdata);
7467}
7468
7469static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7470 .rpc_call_done = nfs4_exchange_id_done,
7471 .rpc_release = nfs4_exchange_id_release,
7472};
7473
Benny Halevy99fe60d2009-04-01 09:22:29 -04007474/*
7475 * _nfs4_proc_exchange_id()
7476 *
7477 * Wrapper for EXCHANGE_ID operation.
7478 */
7479static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007480 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007481{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007482 struct rpc_message msg = {
7483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484 .rpc_cred = cred,
7485 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007486 struct rpc_task_setup task_setup_data = {
7487 .rpc_client = clp->cl_rpcclient,
7488 .callback_ops = &nfs4_exchange_id_call_ops,
7489 .rpc_message = &msg,
7490 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7491 };
7492 struct nfs41_exchange_id_data *calldata;
7493 struct rpc_task *task;
7494 int status = -EIO;
7495
7496 if (!atomic_inc_not_zero(&clp->cl_count))
7497 goto out;
7498
7499 status = -ENOMEM;
7500 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7501 if (!calldata)
7502 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007503
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007504 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007505
7506 status = nfs4_init_uniform_client_string(clp);
7507 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007508 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007509
7510 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7511 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7512 clp->cl_owner_id);
7513
Andy Adamson8d89bd72016-09-09 09:22:18 -04007514 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7515 GFP_NOFS);
7516 status = -ENOMEM;
7517 if (unlikely(calldata->res.server_owner == NULL))
7518 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007519
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007521 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007522 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007523 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007524
Andy Adamson8d89bd72016-09-09 09:22:18 -04007525 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7526 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007527 goto out_server_scope;
7528
7529 switch (sp4_how) {
7530 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007531 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007532 break;
7533
7534 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007535 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007536 break;
7537
7538 default:
7539 /* unsupported! */
7540 WARN_ON_ONCE(1);
7541 status = -EINVAL;
7542 goto out_impl_id;
7543 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007544 if (xprt) {
7545 calldata->xprt = xprt;
7546 task_setup_data.rpc_xprt = xprt;
7547 task_setup_data.flags =
7548 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007549 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7550 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007551 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007552 calldata->args.client = clp;
7553#ifdef CONFIG_NFS_V4_1_MIGRATION
7554 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7555 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7556 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7557#else
7558 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7559 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7560#endif
7561 msg.rpc_argp = &calldata->args;
7562 msg.rpc_resp = &calldata->res;
7563 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007564
Andy Adamson8d89bd72016-09-09 09:22:18 -04007565 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007566 if (IS_ERR(task))
7567 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007568
Andy Adamsonad0849a2016-09-09 09:22:28 -04007569 if (!xprt) {
7570 status = rpc_wait_for_completion_task(task);
7571 if (!status)
7572 status = calldata->rpc_status;
7573 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007574 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007575
Andy Adamson8d89bd72016-09-09 09:22:18 -04007576 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007577out:
Chuck Lever177313f2012-05-21 22:44:58 -04007578 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007579 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007580 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007581 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007582 clp->cl_implid->date.seconds,
7583 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007584 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007585 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007586
7587out_impl_id:
7588 kfree(calldata->res.impl_id);
7589out_server_scope:
7590 kfree(calldata->res.server_scope);
7591out_server_owner:
7592 kfree(calldata->res.server_owner);
7593out_calldata:
7594 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007595 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007596 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007597}
7598
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007599/*
7600 * nfs4_proc_exchange_id()
7601 *
7602 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7603 *
7604 * Since the clientid has expired, all compounds using sessions
7605 * associated with the stale clientid will be returning
7606 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7607 * be in some phase of session reset.
7608 *
7609 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7610 */
7611int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7612{
7613 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7614 int status;
7615
7616 /* try SP4_MACH_CRED if krb5i/p */
7617 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7618 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007619 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007620 if (!status)
7621 return 0;
7622 }
7623
7624 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007625 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007626}
7627
Andy Adamson04fa2c62016-09-09 09:22:29 -04007628/**
7629 * nfs4_test_session_trunk
7630 *
7631 * This is an add_xprt_test() test function called from
7632 * rpc_clnt_setup_test_and_add_xprt.
7633 *
7634 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7635 * and is dereferrenced in nfs4_exchange_id_release
7636 *
7637 * Upon success, add the new transport to the rpc_clnt
7638 *
7639 * @clnt: struct rpc_clnt to get new transport
7640 * @xprt: the rpc_xprt to test
7641 * @data: call data for _nfs4_proc_exchange_id.
7642 */
7643int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7644 void *data)
7645{
7646 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7647 u32 sp4_how;
7648
7649 dprintk("--> %s try %s\n", __func__,
7650 xprt->address_strings[RPC_DISPLAY_ADDR]);
7651
7652 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7653
7654 /* Test connection for session trunking. Async exchange_id call */
7655 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7656}
7657EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7658
Trond Myklebust66245532012-05-25 17:18:09 -04007659static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7660 struct rpc_cred *cred)
7661{
7662 struct rpc_message msg = {
7663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7664 .rpc_argp = clp,
7665 .rpc_cred = cred,
7666 };
7667 int status;
7668
7669 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007670 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007671 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007672 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007673 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7674 return status;
7675}
7676
7677static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7678 struct rpc_cred *cred)
7679{
7680 unsigned int loop;
7681 int ret;
7682
7683 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7684 ret = _nfs4_proc_destroy_clientid(clp, cred);
7685 switch (ret) {
7686 case -NFS4ERR_DELAY:
7687 case -NFS4ERR_CLIENTID_BUSY:
7688 ssleep(1);
7689 break;
7690 default:
7691 return ret;
7692 }
7693 }
7694 return 0;
7695}
7696
7697int nfs4_destroy_clientid(struct nfs_client *clp)
7698{
7699 struct rpc_cred *cred;
7700 int ret = 0;
7701
7702 if (clp->cl_mvops->minor_version < 1)
7703 goto out;
7704 if (clp->cl_exchange_flags == 0)
7705 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007706 if (clp->cl_preserve_clid)
7707 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007708 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007709 ret = nfs4_proc_destroy_clientid(clp, cred);
7710 if (cred)
7711 put_rpccred(cred);
7712 switch (ret) {
7713 case 0:
7714 case -NFS4ERR_STALE_CLIENTID:
7715 clp->cl_exchange_flags = 0;
7716 }
7717out:
7718 return ret;
7719}
7720
Andy Adamson2050f0c2009-04-01 09:22:30 -04007721struct nfs4_get_lease_time_data {
7722 struct nfs4_get_lease_time_args *args;
7723 struct nfs4_get_lease_time_res *res;
7724 struct nfs_client *clp;
7725};
7726
7727static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7728 void *calldata)
7729{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007730 struct nfs4_get_lease_time_data *data =
7731 (struct nfs4_get_lease_time_data *)calldata;
7732
7733 dprintk("--> %s\n", __func__);
7734 /* just setup sequence, do not trigger session recovery
7735 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007736 nfs41_setup_sequence(data->clp->cl_session,
7737 &data->args->la_seq_args,
7738 &data->res->lr_seq_res,
7739 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007740 dprintk("<-- %s\n", __func__);
7741}
7742
7743/*
7744 * Called from nfs4_state_manager thread for session setup, so don't recover
7745 * from sequence operation or clientid errors.
7746 */
7747static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7748{
7749 struct nfs4_get_lease_time_data *data =
7750 (struct nfs4_get_lease_time_data *)calldata;
7751
7752 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007753 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7754 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007755 switch (task->tk_status) {
7756 case -NFS4ERR_DELAY:
7757 case -NFS4ERR_GRACE:
7758 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7759 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7760 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007761 /* fall through */
7762 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007763 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007764 return;
7765 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007766 dprintk("<-- %s\n", __func__);
7767}
7768
Trond Myklebust17280172012-03-11 13:11:00 -04007769static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007770 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7771 .rpc_call_done = nfs4_get_lease_time_done,
7772};
7773
7774int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7775{
7776 struct rpc_task *task;
7777 struct nfs4_get_lease_time_args args;
7778 struct nfs4_get_lease_time_res res = {
7779 .lr_fsinfo = fsinfo,
7780 };
7781 struct nfs4_get_lease_time_data data = {
7782 .args = &args,
7783 .res = &res,
7784 .clp = clp,
7785 };
7786 struct rpc_message msg = {
7787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7788 .rpc_argp = &args,
7789 .rpc_resp = &res,
7790 };
7791 struct rpc_task_setup task_setup = {
7792 .rpc_client = clp->cl_rpcclient,
7793 .rpc_message = &msg,
7794 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007795 .callback_data = &data,
7796 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007797 };
7798 int status;
7799
Chuck Levera9c92d62013-08-09 12:48:18 -04007800 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007801 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007802 dprintk("--> %s\n", __func__);
7803 task = rpc_run_task(&task_setup);
7804
7805 if (IS_ERR(task))
7806 status = PTR_ERR(task);
7807 else {
7808 status = task->tk_status;
7809 rpc_put_task(task);
7810 }
7811 dprintk("<-- %s return %d\n", __func__, status);
7812
7813 return status;
7814}
7815
Andy Adamsonfc931582009-04-01 09:22:31 -04007816/*
7817 * Initialize the values to be used by the client in CREATE_SESSION
7818 * If nfs4_init_session set the fore channel request and response sizes,
7819 * use them.
7820 *
7821 * Set the back channel max_resp_sz_cached to zero to force the client to
7822 * always set csa_cachethis to FALSE because the current implementation
7823 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7824 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007825static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7826 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007827{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007828 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007829 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007830
Andy Adamson18aad3d2013-06-26 12:21:49 -04007831 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7832 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7833
Andy Adamsonfc931582009-04-01 09:22:31 -04007834 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007835 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7836 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007837 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007838 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007839
7840 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007841 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007842 __func__,
7843 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007844 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007845
7846 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007847 args->bc_attrs.max_rqst_sz = max_bc_payload;
7848 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007849 args->bc_attrs.max_resp_sz_cached = 0;
7850 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007851 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007852
7853 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7854 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7855 __func__,
7856 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7857 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7858 args->bc_attrs.max_reqs);
7859}
7860
Trond Myklebust79969dd2015-02-18 11:30:18 -08007861static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7862 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007863{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007864 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007865 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007866
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007867 if (rcvd->max_resp_sz > sent->max_resp_sz)
7868 return -EINVAL;
7869 /*
7870 * Our requested max_ops is the minimum we need; we're not
7871 * prepared to break up compounds into smaller pieces than that.
7872 * So, no point even trying to continue if the server won't
7873 * cooperate:
7874 */
7875 if (rcvd->max_ops < sent->max_ops)
7876 return -EINVAL;
7877 if (rcvd->max_reqs == 0)
7878 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007879 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7880 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007881 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007882}
7883
Trond Myklebust79969dd2015-02-18 11:30:18 -08007884static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7885 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007886{
7887 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007888 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007889
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007890 if (!(res->flags & SESSION4_BACK_CHAN))
7891 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007892 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7893 return -EINVAL;
7894 if (rcvd->max_resp_sz < sent->max_resp_sz)
7895 return -EINVAL;
7896 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7897 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007898 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007899 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007900 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007901 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007902out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007903 return 0;
7904}
Andy Adamson8d353012009-04-01 09:22:32 -04007905
Andy Adamson8d353012009-04-01 09:22:32 -04007906static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007907 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007908{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007909 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007910
Trond Myklebust79969dd2015-02-18 11:30:18 -08007911 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007912 if (ret)
7913 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007914 return nfs4_verify_back_channel_attrs(args, res);
7915}
7916
7917static void nfs4_update_session(struct nfs4_session *session,
7918 struct nfs41_create_session_res *res)
7919{
7920 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007921 /* Mark client id and session as being confirmed */
7922 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7923 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007924 session->flags = res->flags;
7925 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007926 if (res->flags & SESSION4_BACK_CHAN)
7927 memcpy(&session->bc_attrs, &res->bc_attrs,
7928 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007929}
7930
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007931static int _nfs4_proc_create_session(struct nfs_client *clp,
7932 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007933{
7934 struct nfs4_session *session = clp->cl_session;
7935 struct nfs41_create_session_args args = {
7936 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007937 .clientid = clp->cl_clientid,
7938 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007939 .cb_program = NFS4_CALLBACK,
7940 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007941 struct nfs41_create_session_res res;
7942
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 struct rpc_message msg = {
7944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7945 .rpc_argp = &args,
7946 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007947 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007948 };
7949 int status;
7950
Chuck Lever6b26cc82016-05-02 14:40:40 -04007951 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007952 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007953
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007954 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007955 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007956
Trond Myklebustb519d402016-09-11 14:50:01 -04007957 switch (status) {
7958 case -NFS4ERR_STALE_CLIENTID:
7959 case -NFS4ERR_DELAY:
7960 case -ETIMEDOUT:
7961 case -EACCES:
7962 case -EAGAIN:
7963 goto out;
7964 };
7965
7966 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007967 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007968 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007969 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007970 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007971 if (status)
7972 goto out;
7973 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007974 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007975out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007976 return status;
7977}
7978
7979/*
7980 * Issues a CREATE_SESSION operation to the server.
7981 * It is the responsibility of the caller to verify the session is
7982 * expired before calling this routine.
7983 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007984int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007985{
7986 int status;
7987 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007988 struct nfs4_session *session = clp->cl_session;
7989
7990 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7991
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007992 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007993 if (status)
7994 goto out;
7995
Andy Adamsonaacd5532011-11-09 13:58:21 -05007996 /* Init or reset the session slot tables */
7997 status = nfs4_setup_session_slot_tables(session);
7998 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007999 if (status)
8000 goto out;
8001
8002 ptr = (unsigned *)&session->sess_id.data[0];
8003 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8004 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008005out:
8006 dprintk("<-- %s\n", __func__);
8007 return status;
8008}
8009
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008010/*
8011 * Issue the over-the-wire RPC DESTROY_SESSION.
8012 * The caller must serialize access to this routine.
8013 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008014int nfs4_proc_destroy_session(struct nfs4_session *session,
8015 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008016{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008017 struct rpc_message msg = {
8018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8019 .rpc_argp = session,
8020 .rpc_cred = cred,
8021 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008022 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008023
8024 dprintk("--> nfs4_proc_destroy_session\n");
8025
8026 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008027 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8028 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008029
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008030 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008031 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008032
8033 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008034 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008035 "Session has been destroyed regardless...\n", status);
8036
8037 dprintk("<-- nfs4_proc_destroy_session\n");
8038 return status;
8039}
8040
Trond Myklebust7b38c362012-05-23 13:23:31 -04008041/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008042 * Renew the cl_session lease.
8043 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008044struct nfs4_sequence_data {
8045 struct nfs_client *clp;
8046 struct nfs4_sequence_args args;
8047 struct nfs4_sequence_res res;
8048};
8049
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008050static void nfs41_sequence_release(void *data)
8051{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008052 struct nfs4_sequence_data *calldata = data;
8053 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008054
Alexandros Batsakis71358402010-02-05 03:45:05 -08008055 if (atomic_read(&clp->cl_count) > 1)
8056 nfs4_schedule_state_renewal(clp);
8057 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008058 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008059}
8060
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008061static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8062{
8063 switch(task->tk_status) {
8064 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008065 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8066 return -EAGAIN;
8067 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008068 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008069 }
8070 return 0;
8071}
8072
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008073static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008074{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008075 struct nfs4_sequence_data *calldata = data;
8076 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077
Trond Myklebust14516c32010-07-31 14:29:06 -04008078 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8079 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008080
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008081 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008082 if (task->tk_status < 0) {
8083 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008084 if (atomic_read(&clp->cl_count) == 1)
8085 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008086
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008087 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8088 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089 return;
8090 }
8091 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008092 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008093out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008094 dprintk("<-- %s\n", __func__);
8095}
8096
8097static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8098{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008099 struct nfs4_sequence_data *calldata = data;
8100 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008101 struct nfs4_sequence_args *args;
8102 struct nfs4_sequence_res *res;
8103
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008104 args = task->tk_msg.rpc_argp;
8105 res = task->tk_msg.rpc_resp;
8106
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008107 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008108}
8109
8110static const struct rpc_call_ops nfs41_sequence_ops = {
8111 .rpc_call_done = nfs41_sequence_call_done,
8112 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008113 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008114};
8115
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008116static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8117 struct rpc_cred *cred,
8118 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008119{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008120 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008121 struct rpc_message msg = {
8122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8123 .rpc_cred = cred,
8124 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008125 struct rpc_task_setup task_setup_data = {
8126 .rpc_client = clp->cl_rpcclient,
8127 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008128 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008129 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008130 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008131
Alexandros Batsakis71358402010-02-05 03:45:05 -08008132 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008133 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008134 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008135 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008136 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008137 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008138 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008139 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008140 if (is_privileged)
8141 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008142 msg.rpc_argp = &calldata->args;
8143 msg.rpc_resp = &calldata->res;
8144 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008145 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008146
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008147 return rpc_run_task(&task_setup_data);
8148}
8149
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008150static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008151{
8152 struct rpc_task *task;
8153 int ret = 0;
8154
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008155 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008156 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008157 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008158 if (IS_ERR(task))
8159 ret = PTR_ERR(task);
8160 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008161 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008162 dprintk("<-- %s status=%d\n", __func__, ret);
8163 return ret;
8164}
8165
8166static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8167{
8168 struct rpc_task *task;
8169 int ret;
8170
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008171 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008172 if (IS_ERR(task)) {
8173 ret = PTR_ERR(task);
8174 goto out;
8175 }
8176 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008177 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008178 ret = task->tk_status;
8179 rpc_put_task(task);
8180out:
8181 dprintk("<-- %s status=%d\n", __func__, ret);
8182 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008183}
8184
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008185struct nfs4_reclaim_complete_data {
8186 struct nfs_client *clp;
8187 struct nfs41_reclaim_complete_args arg;
8188 struct nfs41_reclaim_complete_res res;
8189};
8190
8191static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8192{
8193 struct nfs4_reclaim_complete_data *calldata = data;
8194
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008195 nfs41_setup_sequence(calldata->clp->cl_session,
8196 &calldata->arg.seq_args,
8197 &calldata->res.seq_res,
8198 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008199}
8200
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008201static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8202{
8203 switch(task->tk_status) {
8204 case 0:
8205 case -NFS4ERR_COMPLETE_ALREADY:
8206 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8207 break;
8208 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008209 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008210 /* fall through */
8211 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008212 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008213 case -NFS4ERR_BADSESSION:
8214 case -NFS4ERR_DEADSESSION:
8215 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8216 nfs4_schedule_session_recovery(clp->cl_session,
8217 task->tk_status);
8218 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008219 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008220 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008221 }
8222 return 0;
8223}
8224
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008225static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8226{
8227 struct nfs4_reclaim_complete_data *calldata = data;
8228 struct nfs_client *clp = calldata->clp;
8229 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8230
8231 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008232 if (!nfs41_sequence_done(task, res))
8233 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008234
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008235 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008236 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8237 rpc_restart_call_prepare(task);
8238 return;
8239 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008240 dprintk("<-- %s\n", __func__);
8241}
8242
8243static void nfs4_free_reclaim_complete_data(void *data)
8244{
8245 struct nfs4_reclaim_complete_data *calldata = data;
8246
8247 kfree(calldata);
8248}
8249
8250static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8251 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8252 .rpc_call_done = nfs4_reclaim_complete_done,
8253 .rpc_release = nfs4_free_reclaim_complete_data,
8254};
8255
8256/*
8257 * Issue a global reclaim complete.
8258 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008259static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8260 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008261{
8262 struct nfs4_reclaim_complete_data *calldata;
8263 struct rpc_task *task;
8264 struct rpc_message msg = {
8265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008266 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008267 };
8268 struct rpc_task_setup task_setup_data = {
8269 .rpc_client = clp->cl_rpcclient,
8270 .rpc_message = &msg,
8271 .callback_ops = &nfs4_reclaim_complete_call_ops,
8272 .flags = RPC_TASK_ASYNC,
8273 };
8274 int status = -ENOMEM;
8275
8276 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008277 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008278 if (calldata == NULL)
8279 goto out;
8280 calldata->clp = clp;
8281 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282
Chuck Levera9c92d62013-08-09 12:48:18 -04008283 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008284 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008285 msg.rpc_argp = &calldata->arg;
8286 msg.rpc_resp = &calldata->res;
8287 task_setup_data.callback_data = calldata;
8288 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008289 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008290 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008291 goto out;
8292 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008293 status = nfs4_wait_for_completion_rpc_task(task);
8294 if (status == 0)
8295 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008296 rpc_put_task(task);
8297out:
8298 dprintk("<-- %s status=%d\n", __func__, status);
8299 return status;
8300}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008301
8302static void
8303nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8304{
8305 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008306 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008307 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008308
8309 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008310 nfs41_setup_sequence(session, &lgp->args.seq_args,
8311 &lgp->res.seq_res, task);
8312 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008313}
8314
8315static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8316{
8317 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008318
8319 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008320 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008321 dprintk("<-- %s\n", __func__);
8322}
8323
8324static int
8325nfs4_layoutget_handle_exception(struct rpc_task *task,
8326 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8327{
Trond Myklebustee314c22012-10-01 17:25:48 -07008328 struct inode *inode = lgp->args.inode;
8329 struct nfs_server *server = NFS_SERVER(inode);
8330 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008331 int nfs4err = task->tk_status;
8332 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008333 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008334
Boaz Harroshed7e5422014-01-22 20:34:54 +02008335 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008336
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008337 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008338 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008339 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008340
8341 /*
8342 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8343 * on the file. set tk_status to -ENODATA to tell upper layer to
8344 * retry go inband.
8345 */
8346 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008347 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008348 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008349 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008350 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8351 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8352 */
8353 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008354 status = -EOVERFLOW;
8355 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008356 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008357 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008358 * (or clients) writing to the same RAID stripe except when
8359 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008360 *
8361 * Treat it like we would RECALLCONFLICT -- we retry for a little
8362 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008363 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008364 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008365 if (lgp->args.minlength == 0) {
8366 status = -EOVERFLOW;
8367 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008368 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008369 status = -EBUSY;
8370 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008371 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008372 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008373 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008374 case -NFS4ERR_DELEG_REVOKED:
8375 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008376 case -NFS4ERR_EXPIRED:
8377 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008378 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008379 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008380 lo = NFS_I(inode)->layout;
8381 /* If the open stateid was bad, then recover it. */
8382 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8383 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008384 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008385 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008386 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008387 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008388 break;
8389 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008390
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008391 /*
8392 * Mark the bad layout state as invalid, then retry
8393 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008394 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008395 spin_unlock(&inode->i_lock);
8396 pnfs_free_lseg_list(&head);
8397 status = -EAGAIN;
8398 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008399 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008400
Trond Myklebust0a702352017-01-23 22:44:12 -05008401 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008402 err = nfs4_handle_exception(server, nfs4err, exception);
8403 if (!status) {
8404 if (exception->retry)
8405 status = -EAGAIN;
8406 else
8407 status = err;
8408 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008409out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008410 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008411 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008412}
8413
Idan Kedar85541162012-08-02 11:47:10 +03008414static size_t max_response_pages(struct nfs_server *server)
8415{
8416 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8417 return nfs_page_array_len(0, max_resp_sz);
8418}
8419
8420static void nfs4_free_pages(struct page **pages, size_t size)
8421{
8422 int i;
8423
8424 if (!pages)
8425 return;
8426
8427 for (i = 0; i < size; i++) {
8428 if (!pages[i])
8429 break;
8430 __free_page(pages[i]);
8431 }
8432 kfree(pages);
8433}
8434
8435static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8436{
8437 struct page **pages;
8438 int i;
8439
8440 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8441 if (!pages) {
8442 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8443 return NULL;
8444 }
8445
8446 for (i = 0; i < size; i++) {
8447 pages[i] = alloc_page(gfp_flags);
8448 if (!pages[i]) {
8449 dprintk("%s: failed to allocate page\n", __func__);
8450 nfs4_free_pages(pages, size);
8451 return NULL;
8452 }
8453 }
8454
8455 return pages;
8456}
8457
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008458static void nfs4_layoutget_release(void *calldata)
8459{
8460 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008461 struct inode *inode = lgp->args.inode;
8462 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008463 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008464
8465 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008466 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008467 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008468 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008469 put_nfs_open_context(lgp->args.ctx);
8470 kfree(calldata);
8471 dprintk("<-- %s\n", __func__);
8472}
8473
8474static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8475 .rpc_call_prepare = nfs4_layoutget_prepare,
8476 .rpc_call_done = nfs4_layoutget_done,
8477 .rpc_release = nfs4_layoutget_release,
8478};
8479
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008480struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008481nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008482{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008483 struct inode *inode = lgp->args.inode;
8484 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008485 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008486 struct rpc_task *task;
8487 struct rpc_message msg = {
8488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8489 .rpc_argp = &lgp->args,
8490 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008491 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008492 };
8493 struct rpc_task_setup task_setup_data = {
8494 .rpc_client = server->client,
8495 .rpc_message = &msg,
8496 .callback_ops = &nfs4_layoutget_call_ops,
8497 .callback_data = lgp,
8498 .flags = RPC_TASK_ASYNC,
8499 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008500 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008501 struct nfs4_exception exception = {
8502 .inode = inode,
8503 .timeout = *timeout,
8504 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008505 int status = 0;
8506
8507 dprintk("--> %s\n", __func__);
8508
Peng Tao4bd5a982014-11-17 11:05:17 +08008509 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8510 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8511
Idan Kedar85541162012-08-02 11:47:10 +03008512 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8513 if (!lgp->args.layout.pages) {
8514 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008515 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008516 }
8517 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8518
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008519 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008520 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008521 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008522
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008523 task = rpc_run_task(&task_setup_data);
8524 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008525 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008526 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008527 if (status == 0) {
8528 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8529 *timeout = exception.timeout;
8530 }
8531
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008532 trace_nfs4_layoutget(lgp->args.ctx,
8533 &lgp->args.range,
8534 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008535 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008536 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008537
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008538 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8539 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008540 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008541 rpc_put_task(task);
8542 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008543 if (status)
8544 return ERR_PTR(status);
8545 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008546}
8547
Benny Halevycbe82602011-05-22 19:52:37 +03008548static void
8549nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8550{
8551 struct nfs4_layoutreturn *lrp = calldata;
8552
8553 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008554 nfs41_setup_sequence(lrp->clp->cl_session,
8555 &lrp->args.seq_args,
8556 &lrp->res.seq_res,
8557 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008558}
8559
8560static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8561{
8562 struct nfs4_layoutreturn *lrp = calldata;
8563 struct nfs_server *server;
8564
8565 dprintk("--> %s\n", __func__);
8566
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008567 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008568 return;
8569
8570 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008571 switch (task->tk_status) {
8572 default:
8573 task->tk_status = 0;
8574 case 0:
8575 break;
8576 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008577 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008578 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008579 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008580 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008581 return;
8582 }
Benny Halevycbe82602011-05-22 19:52:37 +03008583 dprintk("<-- %s\n", __func__);
8584}
8585
8586static void nfs4_layoutreturn_release(void *calldata)
8587{
8588 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008589 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008590 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008591
8592 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008593 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008594 if (lrp->res.lrs_present) {
8595 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8596 &lrp->args.range,
8597 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008598 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008599 } else
8600 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008601 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008602 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008603 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008604 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008605 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008606 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008607 kfree(calldata);
8608 dprintk("<-- %s\n", __func__);
8609}
8610
8611static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8612 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8613 .rpc_call_done = nfs4_layoutreturn_done,
8614 .rpc_release = nfs4_layoutreturn_release,
8615};
8616
Peng Tao6c166052014-11-17 09:30:40 +08008617int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008618{
8619 struct rpc_task *task;
8620 struct rpc_message msg = {
8621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8622 .rpc_argp = &lrp->args,
8623 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008624 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008625 };
8626 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008627 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008628 .rpc_message = &msg,
8629 .callback_ops = &nfs4_layoutreturn_call_ops,
8630 .callback_data = lrp,
8631 };
Peng Tao6c166052014-11-17 09:30:40 +08008632 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008633
Andrew Elble99ade3c2015-12-02 09:39:51 -05008634 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8635 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8636 &task_setup_data.rpc_client, &msg);
8637
Benny Halevycbe82602011-05-22 19:52:37 +03008638 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008639 if (!sync) {
8640 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8641 if (!lrp->inode) {
8642 nfs4_layoutreturn_release(lrp);
8643 return -EAGAIN;
8644 }
8645 task_setup_data.flags |= RPC_TASK_ASYNC;
8646 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008647 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008648 task = rpc_run_task(&task_setup_data);
8649 if (IS_ERR(task))
8650 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008651 if (sync)
8652 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008653 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008654 dprintk("<-- %s status=%d\n", __func__, status);
8655 rpc_put_task(task);
8656 return status;
8657}
8658
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008659static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008660_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8661 struct pnfs_device *pdev,
8662 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663{
8664 struct nfs4_getdeviceinfo_args args = {
8665 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008666 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8667 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008668 };
8669 struct nfs4_getdeviceinfo_res res = {
8670 .pdev = pdev,
8671 };
8672 struct rpc_message msg = {
8673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8674 .rpc_argp = &args,
8675 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008676 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008677 };
8678 int status;
8679
8680 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008681 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008682 if (res.notification & ~args.notify_types)
8683 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008684 if (res.notification != args.notify_types)
8685 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008686
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008687 dprintk("<-- %s status=%d\n", __func__, status);
8688
8689 return status;
8690}
8691
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008692int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8693 struct pnfs_device *pdev,
8694 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008695{
8696 struct nfs4_exception exception = { };
8697 int err;
8698
8699 do {
8700 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008701 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008702 &exception);
8703 } while (exception.retry);
8704 return err;
8705}
8706EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8707
Andy Adamson863a3c62011-03-23 13:27:54 +00008708static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8709{
8710 struct nfs4_layoutcommit_data *data = calldata;
8711 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008712 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008713
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008714 nfs41_setup_sequence(session,
8715 &data->args.seq_args,
8716 &data->res.seq_res,
8717 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008718}
8719
8720static void
8721nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8722{
8723 struct nfs4_layoutcommit_data *data = calldata;
8724 struct nfs_server *server = NFS_SERVER(data->args.inode);
8725
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008726 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008727 return;
8728
8729 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008730 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8731 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8732 case -NFS4ERR_BADLAYOUT: /* no layout */
8733 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008734 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008735 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008736 break;
8737 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008738 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008739 rpc_restart_call_prepare(task);
8740 return;
8741 }
8742 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008743}
8744
8745static void nfs4_layoutcommit_release(void *calldata)
8746{
8747 struct nfs4_layoutcommit_data *data = calldata;
8748
Andy Adamsondb29c082011-07-30 20:52:38 -04008749 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008750 nfs_post_op_update_inode_force_wcc(data->args.inode,
8751 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008752 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008753 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008754 kfree(data);
8755}
8756
8757static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8758 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8759 .rpc_call_done = nfs4_layoutcommit_done,
8760 .rpc_release = nfs4_layoutcommit_release,
8761};
8762
8763int
Andy Adamsonef311532011-03-12 02:58:10 -05008764nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008765{
8766 struct rpc_message msg = {
8767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8768 .rpc_argp = &data->args,
8769 .rpc_resp = &data->res,
8770 .rpc_cred = data->cred,
8771 };
8772 struct rpc_task_setup task_setup_data = {
8773 .task = &data->task,
8774 .rpc_client = NFS_CLIENT(data->args.inode),
8775 .rpc_message = &msg,
8776 .callback_ops = &nfs4_layoutcommit_ops,
8777 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008778 };
8779 struct rpc_task *task;
8780 int status = 0;
8781
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008782 dprintk("NFS: initiating layoutcommit call. sync %d "
8783 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008784 data->args.lastbytewritten,
8785 data->args.inode->i_ino);
8786
Trond Myklebust472e2592015-02-05 16:50:30 -05008787 if (!sync) {
8788 data->inode = nfs_igrab_and_active(data->args.inode);
8789 if (data->inode == NULL) {
8790 nfs4_layoutcommit_release(data);
8791 return -EAGAIN;
8792 }
8793 task_setup_data.flags = RPC_TASK_ASYNC;
8794 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008795 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008796 task = rpc_run_task(&task_setup_data);
8797 if (IS_ERR(task))
8798 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008799 if (sync)
8800 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008801 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008802 dprintk("%s: status %d\n", __func__, status);
8803 rpc_put_task(task);
8804 return status;
8805}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008806
Andy Adamson97431202013-08-08 10:57:56 -04008807/**
8808 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8809 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8810 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008811static int
8812_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008813 struct nfs_fsinfo *info,
8814 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008815{
8816 struct nfs41_secinfo_no_name_args args = {
8817 .style = SECINFO_STYLE_CURRENT_FH,
8818 };
8819 struct nfs4_secinfo_res res = {
8820 .flavors = flavors,
8821 };
8822 struct rpc_message msg = {
8823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8824 .rpc_argp = &args,
8825 .rpc_resp = &res,
8826 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008827 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008828 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008829 int status;
8830
8831 if (use_integrity) {
8832 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008833 cred = nfs4_get_clid_cred(server->nfs_client);
8834 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008835 }
8836
8837 dprintk("--> %s\n", __func__);
8838 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8839 &res.seq_res, 0);
8840 dprintk("<-- %s status=%d\n", __func__, status);
8841
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008842 if (cred)
8843 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008844
8845 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008846}
8847
8848static int
8849nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8850 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8851{
8852 struct nfs4_exception exception = { };
8853 int err;
8854 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008855 /* first try using integrity protection */
8856 err = -NFS4ERR_WRONGSEC;
8857
8858 /* try to use integrity protection with machine cred */
8859 if (_nfs4_is_integrity_protected(server->nfs_client))
8860 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8861 flavors, true);
8862
8863 /*
8864 * if unable to use integrity protection, or SECINFO with
8865 * integrity protection returns NFS4ERR_WRONGSEC (which is
8866 * disallowed by spec, but exists in deployed servers) use
8867 * the current filesystem's rpc_client and the user cred.
8868 */
8869 if (err == -NFS4ERR_WRONGSEC)
8870 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8871 flavors, false);
8872
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008873 switch (err) {
8874 case 0:
8875 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008876 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008877 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008878 default:
8879 err = nfs4_handle_exception(server, err, &exception);
8880 }
8881 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008882out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008883 return err;
8884}
8885
8886static int
8887nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8888 struct nfs_fsinfo *info)
8889{
8890 int err;
8891 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008892 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008893 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008894 struct nfs4_secinfo4 *secinfo;
8895 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008896
8897 page = alloc_page(GFP_KERNEL);
8898 if (!page) {
8899 err = -ENOMEM;
8900 goto out;
8901 }
8902
8903 flavors = page_address(page);
8904 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8905
8906 /*
8907 * Fall back on "guess and check" method if
8908 * the server doesn't support SECINFO_NO_NAME
8909 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008910 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008911 err = nfs4_find_root_sec(server, fhandle, info);
8912 goto out_freepage;
8913 }
8914 if (err)
8915 goto out_freepage;
8916
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008917 for (i = 0; i < flavors->num_flavors; i++) {
8918 secinfo = &flavors->flavors[i];
8919
8920 switch (secinfo->flavor) {
8921 case RPC_AUTH_NULL:
8922 case RPC_AUTH_UNIX:
8923 case RPC_AUTH_GSS:
8924 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8925 &secinfo->flavor_info);
8926 break;
8927 default:
8928 flavor = RPC_AUTH_MAXFLAVOR;
8929 break;
8930 }
8931
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008932 if (!nfs_auth_info_match(&server->auth_info, flavor))
8933 flavor = RPC_AUTH_MAXFLAVOR;
8934
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008935 if (flavor != RPC_AUTH_MAXFLAVOR) {
8936 err = nfs4_lookup_root_sec(server, fhandle,
8937 info, flavor);
8938 if (!err)
8939 break;
8940 }
8941 }
8942
8943 if (flavor == RPC_AUTH_MAXFLAVOR)
8944 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008945
8946out_freepage:
8947 put_page(page);
8948 if (err == -EACCES)
8949 return -EPERM;
8950out:
8951 return err;
8952}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008953
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008954static int _nfs41_test_stateid(struct nfs_server *server,
8955 nfs4_stateid *stateid,
8956 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008957{
8958 int status;
8959 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008960 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008961 };
8962 struct nfs41_test_stateid_res res;
8963 struct rpc_message msg = {
8964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8965 .rpc_argp = &args,
8966 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008967 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008968 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008969 struct rpc_clnt *rpc_client = server->client;
8970
8971 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8972 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008973
Chuck Lever38527b12012-07-11 16:30:23 -04008974 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008975 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008976 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008977 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008978 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008979 if (status != NFS_OK) {
8980 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008981 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008982 }
8983 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008984 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008985}
8986
Trond Myklebust43912bb2016-09-22 13:38:56 -04008987static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8988 int err, struct nfs4_exception *exception)
8989{
8990 exception->retry = 0;
8991 switch(err) {
8992 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008993 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008994 nfs4_handle_exception(server, err, exception);
8995 break;
8996 case -NFS4ERR_BADSESSION:
8997 case -NFS4ERR_BADSLOT:
8998 case -NFS4ERR_BAD_HIGH_SLOT:
8999 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9000 case -NFS4ERR_DEADSESSION:
9001 nfs4_do_handle_exception(server, err, exception);
9002 }
9003}
9004
Chuck Lever38527b12012-07-11 16:30:23 -04009005/**
9006 * nfs41_test_stateid - perform a TEST_STATEID operation
9007 *
9008 * @server: server / transport on which to perform the operation
9009 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009010 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009011 *
9012 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9013 * Otherwise a negative NFS4ERR value is returned if the operation
9014 * failed or the state ID is not currently valid.
9015 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009016static int nfs41_test_stateid(struct nfs_server *server,
9017 nfs4_stateid *stateid,
9018 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009019{
9020 struct nfs4_exception exception = { };
9021 int err;
9022 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009023 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009024 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009025 } while (exception.retry);
9026 return err;
9027}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009028
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009029struct nfs_free_stateid_data {
9030 struct nfs_server *server;
9031 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009032 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009033};
9034
9035static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9036{
9037 struct nfs_free_stateid_data *data = calldata;
9038 nfs41_setup_sequence(nfs4_get_session(data->server),
9039 &data->args.seq_args,
9040 &data->res.seq_res,
9041 task);
9042}
9043
9044static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9045{
9046 struct nfs_free_stateid_data *data = calldata;
9047
9048 nfs41_sequence_done(task, &data->res.seq_res);
9049
9050 switch (task->tk_status) {
9051 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009052 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009053 rpc_restart_call_prepare(task);
9054 }
9055}
9056
9057static void nfs41_free_stateid_release(void *calldata)
9058{
9059 kfree(calldata);
9060}
9061
Trond Myklebust17f26b12013-08-21 15:48:42 -04009062static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009063 .rpc_call_prepare = nfs41_free_stateid_prepare,
9064 .rpc_call_done = nfs41_free_stateid_done,
9065 .rpc_release = nfs41_free_stateid_release,
9066};
9067
9068static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009069 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009070 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009071 bool privileged)
9072{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009073 struct rpc_message msg = {
9074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009075 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009076 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009077 struct rpc_task_setup task_setup = {
9078 .rpc_client = server->client,
9079 .rpc_message = &msg,
9080 .callback_ops = &nfs41_free_stateid_ops,
9081 .flags = RPC_TASK_ASYNC,
9082 };
9083 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009084
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009085 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9086 &task_setup.rpc_client, &msg);
9087
Chuck Lever38527b12012-07-11 16:30:23 -04009088 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009089 data = kmalloc(sizeof(*data), GFP_NOFS);
9090 if (!data)
9091 return ERR_PTR(-ENOMEM);
9092 data->server = server;
9093 nfs4_stateid_copy(&data->args.stateid, stateid);
9094
9095 task_setup.callback_data = data;
9096
9097 msg.rpc_argp = &data->args;
9098 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009099 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009100 if (privileged)
9101 nfs4_set_sequence_privileged(&data->args.seq_args);
9102
9103 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009104}
9105
Chuck Lever38527b12012-07-11 16:30:23 -04009106/**
9107 * nfs41_free_stateid - perform a FREE_STATEID operation
9108 *
9109 * @server: server / transport on which to perform the operation
9110 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009111 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009112 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009113 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009114 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009115 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009116static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009117 const nfs4_stateid *stateid,
9118 struct rpc_cred *cred,
9119 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009120{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009121 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009122
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009123 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009124 if (IS_ERR(task))
9125 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009126 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009127 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009128}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009129
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009130static void
9131nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009132{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009133 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009134
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009135 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009136 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009137}
9138
Trond Myklebust36281ca2012-03-04 18:13:56 -05009139static bool nfs41_match_stateid(const nfs4_stateid *s1,
9140 const nfs4_stateid *s2)
9141{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009142 if (s1->type != s2->type)
9143 return false;
9144
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009145 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009146 return false;
9147
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009148 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009149 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009150 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009151 return true;
9152
9153 return false;
9154}
9155
Andy Adamson557134a2009-04-01 09:21:53 -04009156#endif /* CONFIG_NFS_V4_1 */
9157
Trond Myklebust36281ca2012-03-04 18:13:56 -05009158static bool nfs4_match_stateid(const nfs4_stateid *s1,
9159 const nfs4_stateid *s2)
9160{
Trond Myklebustf597c532012-03-04 18:13:56 -05009161 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009162}
9163
9164
Trond Myklebust17280172012-03-11 13:11:00 -04009165static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009166 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009167 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009168 .recover_open = nfs4_open_reclaim,
9169 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009170 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009171 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172};
9173
Andy Adamson591d71c2009-04-01 09:22:47 -04009174#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009175static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009176 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9177 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9178 .recover_open = nfs4_open_reclaim,
9179 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009180 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009181 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009182 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009183};
9184#endif /* CONFIG_NFS_V4_1 */
9185
Trond Myklebust17280172012-03-11 13:11:00 -04009186static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009187 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009188 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009189 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009191 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192};
9193
Andy Adamson591d71c2009-04-01 09:22:47 -04009194#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009195static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009196 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9197 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009198 .recover_open = nfs41_open_expired,
9199 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009200 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009201};
9202#endif /* CONFIG_NFS_V4_1 */
9203
Trond Myklebust17280172012-03-11 13:11:00 -04009204static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009205 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009206 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009207 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009208};
9209
9210#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009211static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009212 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009213 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009214 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009215};
9216#endif
9217
Chuck Leverec011fe2013-10-17 14:12:39 -04009218static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009219 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009220 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009221};
9222
9223#if defined(CONFIG_NFS_V4_1)
9224static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009225 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009226 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009227};
9228#endif /* CONFIG_NFS_V4_1 */
9229
Trond Myklebust97dc1352010-06-16 09:52:26 -04009230static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9231 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009232 .init_caps = NFS_CAP_READDIRPLUS
9233 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009234 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009235 .init_client = nfs40_init_client,
9236 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009237 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009238 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009239 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009240 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009241 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009242 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009243 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9244 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9245 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009246 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009247};
9248
9249#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009250static struct nfs_seqid *
9251nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9252{
9253 return NULL;
9254}
9255
Trond Myklebust97dc1352010-06-16 09:52:26 -04009256static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9257 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009258 .init_caps = NFS_CAP_READDIRPLUS
9259 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009260 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009261 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009262 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009263 .init_client = nfs41_init_client,
9264 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009265 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009266 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009267 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009268 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009269 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009270 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009271 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009272 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9273 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9274 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009275 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009276};
9277#endif
9278
Steve Dickson42c2c422013-05-22 12:50:38 -04009279#if defined(CONFIG_NFS_V4_2)
9280static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9281 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009282 .init_caps = NFS_CAP_READDIRPLUS
9283 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009284 | NFS_CAP_POSIX_LOCK
9285 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009286 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009287 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009288 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009289 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009290 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009291 | NFS_CAP_LAYOUTSTATS
9292 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009293 .init_client = nfs41_init_client,
9294 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009295 .match_stateid = nfs41_match_stateid,
9296 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009297 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009298 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009299 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009300 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009301 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009302 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9303 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9304 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009305 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009306};
9307#endif
9308
Trond Myklebust97dc1352010-06-16 09:52:26 -04009309const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9310 [0] = &nfs_v4_0_minor_ops,
9311#if defined(CONFIG_NFS_V4_1)
9312 [1] = &nfs_v4_1_minor_ops,
9313#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009314#if defined(CONFIG_NFS_V4_2)
9315 [2] = &nfs_v4_2_minor_ops,
9316#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009317};
9318
Trond Myklebust13997822016-07-24 17:10:52 -04009319static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009320{
9321 ssize_t error, error2;
9322
9323 error = generic_listxattr(dentry, list, size);
9324 if (error < 0)
9325 return error;
9326 if (list) {
9327 list += error;
9328 size -= error;
9329 }
9330
9331 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9332 if (error2 < 0)
9333 return error2;
9334 return error + error2;
9335}
9336
Trond Myklebust17f26b12013-08-21 15:48:42 -04009337static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009338 .create = nfs_create,
9339 .lookup = nfs_lookup,
9340 .atomic_open = nfs_atomic_open,
9341 .link = nfs_link,
9342 .unlink = nfs_unlink,
9343 .symlink = nfs_symlink,
9344 .mkdir = nfs_mkdir,
9345 .rmdir = nfs_rmdir,
9346 .mknod = nfs_mknod,
9347 .rename = nfs_rename,
9348 .permission = nfs_permission,
9349 .getattr = nfs_getattr,
9350 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009351 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009352};
9353
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009354static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009355 .permission = nfs_permission,
9356 .getattr = nfs_getattr,
9357 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009358 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009359};
9360
David Howells509de812006-08-22 20:06:11 -04009361const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 .version = 4, /* protocol version */
9363 .dentry_ops = &nfs4_dentry_operations,
9364 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009365 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009366 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009368 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009369 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009370 .getattr = nfs4_proc_getattr,
9371 .setattr = nfs4_proc_setattr,
9372 .lookup = nfs4_proc_lookup,
9373 .access = nfs4_proc_access,
9374 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 .create = nfs4_proc_create,
9376 .remove = nfs4_proc_remove,
9377 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009378 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009379 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009380 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009381 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009382 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 .link = nfs4_proc_link,
9384 .symlink = nfs4_proc_symlink,
9385 .mkdir = nfs4_proc_mkdir,
9386 .rmdir = nfs4_proc_remove,
9387 .readdir = nfs4_proc_readdir,
9388 .mknod = nfs4_proc_mknod,
9389 .statfs = nfs4_proc_statfs,
9390 .fsinfo = nfs4_proc_fsinfo,
9391 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009392 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009394 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009396 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009397 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009398 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009400 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009401 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009403 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009404 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009405 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009406 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009407 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009408 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009409 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009410 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009411 .create_server = nfs4_create_server,
9412 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413};
9414
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009415static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009416 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009417 .list = nfs4_xattr_list_nfs4_acl,
9418 .get = nfs4_xattr_get_nfs4_acl,
9419 .set = nfs4_xattr_set_nfs4_acl,
9420};
9421
9422const struct xattr_handler *nfs4_xattr_handlers[] = {
9423 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009424#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9425 &nfs4_xattr_nfs4_label_handler,
9426#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009427 NULL
9428};
9429
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430/*
9431 * Local variables:
9432 * c-basic-offset: 8
9433 * End:
9434 */