blob: ca4249ae644f293389bcea1bd85f11732440555d [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 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002919 if (status == -EAGAIN) {
2920 /* We must have found a delegation */
2921 exception.retry = 1;
2922 continue;
2923 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002924 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2925 continue;
2926 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 status, &exception));
2928 } while (exception.retry);
2929 return res;
2930}
2931
Trond Myklebust8487c472016-06-26 08:44:35 -04002932static int _nfs4_do_setattr(struct inode *inode,
2933 struct nfs_setattrargs *arg,
2934 struct nfs_setattrres *res,
2935 struct rpc_cred *cred,
2936 struct nfs4_state *state)
2937{
2938 struct nfs_server *server = NFS_SERVER(inode);
2939 struct rpc_message msg = {
2940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2941 .rpc_argp = arg,
2942 .rpc_resp = res,
2943 .rpc_cred = cred,
2944 };
2945 struct rpc_cred *delegation_cred = NULL;
2946 unsigned long timestamp = jiffies;
2947 fmode_t fmode;
2948 bool truncate;
2949 int status;
2950
2951 nfs_fattr_init(res->fattr);
2952
2953 /* Servers should only apply open mode checks for file size changes */
2954 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2955 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2956
2957 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2958 /* Use that stateid */
2959 } else if (truncate && state != NULL) {
2960 struct nfs_lockowner lockowner = {
2961 .l_owner = current->files,
2962 .l_pid = current->tgid,
2963 };
2964 if (!nfs4_valid_open_stateid(state))
2965 return -EBADF;
2966 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2967 &arg->stateid, &delegation_cred) == -EIO)
2968 return -EBADF;
2969 } else
2970 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2971 if (delegation_cred)
2972 msg.rpc_cred = delegation_cred;
2973
2974 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2975
2976 put_rpccred(delegation_cred);
2977 if (status == 0 && state != NULL)
2978 renew_lease(server, timestamp);
2979 trace_nfs4_setattr(inode, &arg->stateid, status);
2980 return status;
2981}
2982
2983static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2984 struct nfs_fattr *fattr, struct iattr *sattr,
2985 struct nfs4_state *state, struct nfs4_label *ilabel,
2986 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002988 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002990 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 .iap = sattr,
2992 .server = server,
2993 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002994 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 };
2996 struct nfs_setattrres res = {
2997 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002998 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 .server = server,
3000 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003001 struct nfs4_exception exception = {
3002 .state = state,
3003 .inode = inode,
3004 .stateid = &arg.stateid,
3005 };
3006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
David Quigleyaa9c2662013-05-22 12:50:44 -04003008 arg.bitmask = nfs4_bitmask(server, ilabel);
3009 if (ilabel)
3010 arg.bitmask = nfs4_bitmask(server, olabel);
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003013 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003014 switch (err) {
3015 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003016 if (!(sattr->ia_valid & ATTR_SIZE)) {
3017 pr_warn_once("NFSv4: server %s is incorrectly "
3018 "applying open mode checks to "
3019 "a SETATTR that is not "
3020 "changing file size.\n",
3021 server->nfs_client->cl_hostname);
3022 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003023 if (state && !(state->state & FMODE_WRITE)) {
3024 err = -EBADF;
3025 if (sattr->ia_valid & ATTR_OPEN)
3026 err = -EACCES;
3027 goto out;
3028 }
3029 }
3030 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003032out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return err;
3034}
3035
Peng Tao500d7012015-09-22 11:35:22 +08003036static bool
3037nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3038{
3039 if (inode == NULL || !nfs_have_layout(inode))
3040 return false;
3041
3042 return pnfs_wait_on_layoutreturn(inode, task);
3043}
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045struct nfs4_closedata {
3046 struct inode *inode;
3047 struct nfs4_state *state;
3048 struct nfs_closeargs arg;
3049 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003050 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003051 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003052 bool roc;
3053 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054};
3055
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003056static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003057{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003058 struct nfs4_closedata *calldata = data;
3059 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003060 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003061
Fred Isamanf7e89172011-01-06 11:36:32 +00003062 if (calldata->roc)
3063 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003064 nfs4_put_open_state(calldata->state);
3065 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003066 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003067 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003068 kfree(calldata);
3069}
3070
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003071static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003073 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003076 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Chuck Levera3ca5652012-03-01 17:00:40 -05003078 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003079 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3080 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003081 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 /* hmm. we are done with the inode, and in the process of freeing
3083 * the state_owner. we keep this around to process errors
3084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 switch (task->tk_status) {
3086 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003087 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003088 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003089 pnfs_roc_set_barrier(state->inode,
3090 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003091 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003092 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003093 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003095 case -NFS4ERR_EXPIRED:
3096 nfs4_free_revoked_stateid(server,
3097 &calldata->arg.stateid,
3098 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003099 case -NFS4ERR_OLD_STATEID:
3100 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003101 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003102 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003103 rpc_restart_call_prepare(task);
3104 goto out_release;
3105 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003106 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003109 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003110 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003111 goto out_release;
3112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003114 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3115 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003116out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003117 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003118 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003119 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003122static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003124 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003125 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003126 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003127 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003128 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003129
Chuck Levera3ca5652012-03-01 17:00:40 -05003130 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003131 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003132 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003133
Trond Myklebust88069f72009-12-08 08:33:16 -05003134 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003135 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003136 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3137 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3138 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003139 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003140 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003141 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003142 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003143 if (state->n_rdonly == 0)
3144 call_close |= is_rdonly;
3145 else if (is_rdonly)
3146 calldata->arg.fmode |= FMODE_READ;
3147 if (state->n_wronly == 0)
3148 call_close |= is_wronly;
3149 else if (is_wronly)
3150 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003151 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3152 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003153 } else if (is_rdwr)
3154 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3155
Trond Myklebust5cc78612016-11-14 11:19:56 -05003156 if (!nfs4_valid_open_stateid(state) ||
3157 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003158 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003159 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003160
3161 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003162 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003163 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003164 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003165
Peng Tao500d7012015-09-22 11:35:22 +08003166 if (nfs4_wait_on_layoutreturn(inode, task)) {
3167 nfs_release_seqid(calldata->arg.seqid);
3168 goto out_wait;
3169 }
3170
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003171 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003172 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003173 if (calldata->roc)
3174 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3175
Trond Myklebust6ae37332015-01-30 14:21:14 -05003176 calldata->arg.share_access =
3177 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3178 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003179
Trond Myklebust516a6af2005-10-27 22:12:41 -04003180 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003181 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003182 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003183 &calldata->arg.seq_args,
3184 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003185 task) != 0)
3186 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003187 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003188 return;
3189out_no_action:
3190 task->tk_action = NULL;
3191out_wait:
3192 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003195static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003196 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003197 .rpc_call_done = nfs4_close_done,
3198 .rpc_release = nfs4_free_closedata,
3199};
3200
Peng Taofe08c542014-07-03 13:05:01 +08003201static bool nfs4_roc(struct inode *inode)
3202{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003203 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003204 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003205 return pnfs_roc(inode);
3206}
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208/*
3209 * It is possible for data to be read/written from a mem-mapped file
3210 * after the sys_close call (which hits the vfs layer as a flush).
3211 * This means that we can't safely call nfsv4 close on a file until
3212 * the inode is cleared. This in turn means that we are not good
3213 * NFSv4 citizens - we do not indicate to the server to update the file's
3214 * share state even when we are done with one of the three share
3215 * stateid's in the inode.
3216 *
3217 * NOTE: Caller must be holding the sp->so_owner semaphore!
3218 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003219int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003221 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003222 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003224 struct nfs4_state_owner *sp = state->owner;
3225 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003226 struct rpc_message msg = {
3227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3228 .rpc_cred = state->owner->so_cred,
3229 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003230 struct rpc_task_setup task_setup_data = {
3231 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003232 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003233 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003234 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003235 .flags = RPC_TASK_ASYNC,
3236 };
Trond Myklebust95121352005-10-18 14:20:12 -07003237 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003239 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3240 &task_setup_data.rpc_client, &msg);
3241
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003242 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003244 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003245 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003246 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003248 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003250 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3251 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003252 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003253 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003254 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003255 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003256 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003257 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003258 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003259 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003260 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003261
Trond Myklebust1174dd12010-12-21 10:52:24 -05003262 msg.rpc_argp = &calldata->arg;
3263 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003264 task_setup_data.callback_data = calldata;
3265 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003266 if (IS_ERR(task))
3267 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003268 status = 0;
3269 if (wait)
3270 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003271 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003272 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003273out_free_calldata:
3274 kfree(calldata);
3275out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003276 nfs4_put_open_state(state);
3277 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003278 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
Trond Myklebust2b484292010-09-17 10:56:51 -04003281static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003282nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3283 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003286 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3287
3288 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Trond Myklebust565277f2007-10-15 18:17:53 -04003290 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003291 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003292
3293 nfs4_label_release_security(label);
3294
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003295 if (IS_ERR(state))
3296 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003297 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
Trond Myklebust1185a552009-12-03 15:54:02 -05003300static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003301{
3302 if (ctx->state == NULL)
3303 return;
3304 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003305 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003306 else
Al Viro643168c2011-06-22 18:20:23 -04003307 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Trond Myklebustb944dba2013-11-04 15:20:20 -05003310#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3311#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003312#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3315{
Kinglong Mee8c612822015-08-26 21:12:58 +08003316 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003317 struct nfs4_server_caps_arg args = {
3318 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003319 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003320 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 struct nfs4_server_caps_res res = {};
3322 struct rpc_message msg = {
3323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003324 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 .rpc_resp = &res,
3326 };
3327 int status;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003328 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Kinglong Mee8c612822015-08-26 21:12:58 +08003330 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3331 FATTR4_WORD0_FH_EXPIRE_TYPE |
3332 FATTR4_WORD0_LINK_SUPPORT |
3333 FATTR4_WORD0_SYMLINK_SUPPORT |
3334 FATTR4_WORD0_ACLSUPPORT;
3335 if (minorversion)
3336 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3337
Bryan Schumaker7c513052011-03-24 17:12:24 +00003338 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003340 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003341 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003342 case 0:
3343 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3344 res.attr_bitmask[2] = 0;
3345 break;
3346 case 1:
3347 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3348 break;
3349 case 2:
3350 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003353 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3354 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3355 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3356 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003357 NFS_CAP_CTIME|NFS_CAP_MTIME|
3358 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003359 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3360 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 server->caps |= NFS_CAP_ACLS;
3362 if (res.has_links != 0)
3363 server->caps |= NFS_CAP_HARDLINKS;
3364 if (res.has_symlinks != 0)
3365 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003366 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3367 server->caps |= NFS_CAP_FILEID;
3368 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3369 server->caps |= NFS_CAP_MODE;
3370 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3371 server->caps |= NFS_CAP_NLINK;
3372 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3373 server->caps |= NFS_CAP_OWNER;
3374 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3375 server->caps |= NFS_CAP_OWNER_GROUP;
3376 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3377 server->caps |= NFS_CAP_ATIME;
3378 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3379 server->caps |= NFS_CAP_CTIME;
3380 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3381 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003382#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3383 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3384 server->caps |= NFS_CAP_SECURITY_LABEL;
3385#endif
3386 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3387 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003388 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003389
Trond Myklebusta65318b2009-03-11 14:10:28 -04003390 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3391 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3392 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003393 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003394
3395 /* Avoid a regression due to buggy server */
3396 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3397 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003398 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3399 sizeof(server->exclcreat_bitmask));
Trond Myklebustfca22d52017-05-09 15:47:15 -04003400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003402 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return status;
3406}
3407
Trond Myklebust55a97592006-06-09 09:34:19 -04003408int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
3410 struct nfs4_exception exception = { };
3411 int err;
3412 do {
3413 err = nfs4_handle_exception(server,
3414 _nfs4_server_capabilities(server, fhandle),
3415 &exception);
3416 } while (exception.retry);
3417 return err;
3418}
3419
3420static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3421 struct nfs_fsinfo *info)
3422{
David Quigleyaa9c2662013-05-22 12:50:44 -04003423 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003425 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 };
3427 struct nfs4_lookup_res res = {
3428 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003429 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 .fh = fhandle,
3431 };
3432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3434 .rpc_argp = &args,
3435 .rpc_resp = &res,
3436 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003437
David Quigleyaa9c2662013-05-22 12:50:44 -04003438 bitmask[0] = nfs4_fattr_bitmap[0];
3439 bitmask[1] = nfs4_fattr_bitmap[1];
3440 /*
3441 * Process the label in the upcoming getfattr
3442 */
3443 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3444
Trond Myklebust0e574af2005-10-27 22:12:38 -04003445 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003446 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
3449static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3450 struct nfs_fsinfo *info)
3451{
3452 struct nfs4_exception exception = { };
3453 int err;
3454 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003455 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003456 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003457 switch (err) {
3458 case 0:
3459 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003460 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003461 default:
3462 err = nfs4_handle_exception(server, err, &exception);
3463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003465out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return err;
3467}
3468
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003469static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3470 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3471{
Trond Myklebustc2190662013-08-26 19:23:04 -04003472 struct rpc_auth_create_args auth_args = {
3473 .pseudoflavor = flavor,
3474 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003475 struct rpc_auth *auth;
3476 int ret;
3477
Trond Myklebustc2190662013-08-26 19:23:04 -04003478 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003479 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003480 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003481 goto out;
3482 }
3483 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003484out:
3485 return ret;
3486}
3487
Chuck Lever9a744ba2013-03-16 15:56:02 -04003488/*
3489 * Retry pseudoroot lookup with various security flavors. We do this when:
3490 *
3491 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3492 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3493 *
3494 * Returns zero on success, or a negative NFS4ERR value, or a
3495 * negative errno value.
3496 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003497static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003498 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003500 /* Per 3530bis 15.33.5 */
3501 static const rpc_authflavor_t flav_array[] = {
3502 RPC_AUTH_GSS_KRB5P,
3503 RPC_AUTH_GSS_KRB5I,
3504 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003505 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003506 RPC_AUTH_NULL,
3507 };
3508 int status = -EPERM;
3509 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003511 if (server->auth_info.flavor_len > 0) {
3512 /* try each flavor specified by user */
3513 for (i = 0; i < server->auth_info.flavor_len; i++) {
3514 status = nfs4_lookup_root_sec(server, fhandle, info,
3515 server->auth_info.flavors[i]);
3516 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3517 continue;
3518 break;
3519 }
3520 } else {
3521 /* no flavors specified by user, try default list */
3522 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3523 status = nfs4_lookup_root_sec(server, fhandle, info,
3524 flav_array[i]);
3525 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3526 continue;
3527 break;
3528 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003529 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003530
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003531 /*
3532 * -EACCESS could mean that the user doesn't have correct permissions
3533 * to access the mount. It could also mean that we tried to mount
3534 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3535 * existing mount programs don't handle -EACCES very well so it should
3536 * be mapped to -EPERM instead.
3537 */
3538 if (status == -EACCES)
3539 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003540 return status;
3541}
3542
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003543/**
3544 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3545 * @server: initialized nfs_server handle
3546 * @fhandle: we fill in the pseudo-fs root file handle
3547 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003548 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003549 *
3550 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003551 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003552int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003553 struct nfs_fsinfo *info,
3554 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003555{
Andre Przywarac7757072015-04-23 17:17:40 +01003556 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003557
Andre Przywarac7757072015-04-23 17:17:40 +01003558 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003559 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003560
3561 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003562 status = server->nfs_client->cl_mvops->find_root_sec(server,
3563 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 if (status == 0)
3566 status = nfs4_server_capabilities(server, fhandle);
3567 if (status == 0)
3568 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003569
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003570 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Bryan Schumakerbae36242012-05-10 15:07:31 -04003573static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3574 struct nfs_fsinfo *info)
3575{
3576 int error;
3577 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003578 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003579
3580 error = nfs4_server_capabilities(server, mntfh);
3581 if (error < 0) {
3582 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3583 return error;
3584 }
3585
David Quigley14c43f72013-05-22 12:50:43 -04003586 label = nfs4_label_alloc(server, GFP_KERNEL);
3587 if (IS_ERR(label))
3588 return PTR_ERR(label);
3589
David Quigley1775fd32013-05-22 12:50:42 -04003590 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003591 if (error < 0) {
3592 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003593 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003594 }
3595
3596 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3597 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3598 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3599
David Quigley14c43f72013-05-22 12:50:43 -04003600err_free_label:
3601 nfs4_label_free(label);
3602
Bryan Schumakerbae36242012-05-10 15:07:31 -04003603 return error;
3604}
3605
Manoj Naik6b97fd32006-06-09 09:34:29 -04003606/*
3607 * Get locations and (maybe) other attributes of a referral.
3608 * Note that we'll actually follow the referral later when
3609 * we detect fsid mismatch in inode revalidation
3610 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003611static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3612 const struct qstr *name, struct nfs_fattr *fattr,
3613 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003614{
3615 int status = -ENOMEM;
3616 struct page *page = NULL;
3617 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003618
3619 page = alloc_page(GFP_KERNEL);
3620 if (page == NULL)
3621 goto out;
3622 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3623 if (locations == NULL)
3624 goto out;
3625
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003626 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003627 if (status != 0)
3628 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003629
3630 /*
3631 * If the fsid didn't change, this is a migration event, not a
3632 * referral. Cause us to drop into the exception handler, which
3633 * will kick off migration recovery.
3634 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003635 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003636 dprintk("%s: server did not return a different fsid for"
3637 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003638 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003639 goto out;
3640 }
Andy Adamson533eb462011-06-13 18:25:56 -04003641 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3642 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003643
Andy Adamson533eb462011-06-13 18:25:56 -04003644 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003645 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003646 memset(fhandle, 0, sizeof(struct nfs_fh));
3647out:
3648 if (page)
3649 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003650 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003651 return status;
3652}
3653
David Quigley1775fd32013-05-22 12:50:42 -04003654static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3655 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
3657 struct nfs4_getattr_arg args = {
3658 .fh = fhandle,
3659 .bitmask = server->attr_bitmask,
3660 };
3661 struct nfs4_getattr_res res = {
3662 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003663 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 .server = server,
3665 };
3666 struct rpc_message msg = {
3667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3668 .rpc_argp = &args,
3669 .rpc_resp = &res,
3670 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003671
3672 args.bitmask = nfs4_bitmask(server, label);
3673
Trond Myklebust0e574af2005-10-27 22:12:38 -04003674 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003675 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
David Quigley1775fd32013-05-22 12:50:42 -04003678static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3679 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
3681 struct nfs4_exception exception = { };
3682 int err;
3683 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003684 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3685 trace_nfs4_getattr(server, fhandle, fattr, err);
3686 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 &exception);
3688 } while (exception.retry);
3689 return err;
3690}
3691
3692/*
3693 * The file is not closed if it is opened due to the a request to change
3694 * the size of the file. The open call will not be needed once the
3695 * VFS layer lookup-intents are implemented.
3696 *
3697 * Close is called when the inode is destroyed.
3698 * If we haven't opened the file for O_WRONLY, we
3699 * need to in the size_change case to obtain a stateid.
3700 *
3701 * Got race?
3702 * Because OPEN is always done by name in nfsv4, it is
3703 * possible that we opened a different file by the same
3704 * name. We can recognize this race condition, but we
3705 * can't do anything about it besides returning an error.
3706 *
3707 * This will be fixed with VFS changes (lookup-intent).
3708 */
3709static int
3710nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3711 struct iattr *sattr)
3712{
David Howells2b0143b2015-03-17 22:25:59 +00003713 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003714 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003715 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003716 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 int status;
3718
Peng Tao88ac8152014-09-12 11:04:10 +08003719 if (pnfs_ld_layoutret_on_setattr(inode) &&
3720 sattr->ia_valid & ATTR_SIZE &&
3721 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003722 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003723
Trond Myklebust0e574af2005-10-27 22:12:38 -04003724 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Andy Adamson26699402012-05-30 16:12:24 -04003726 /* Deal with open(O_TRUNC) */
3727 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003728 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003729
3730 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003731 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003732 return 0;
3733
Trond Myklebustd5308382005-11-04 15:33:38 -05003734 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003735 if (sattr->ia_valid & ATTR_FILE) {
3736 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003737
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003738 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003739 if (ctx) {
3740 cred = ctx->cred;
3741 state = ctx->state;
3742 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003743 }
3744
David Quigley14c43f72013-05-22 12:50:43 -04003745 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3746 if (IS_ERR(label))
3747 return PTR_ERR(label);
3748
3749 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003750 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003751 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003752 nfs_setsecurity(inode, fattr, label);
3753 }
David Quigley14c43f72013-05-22 12:50:43 -04003754 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return status;
3756}
3757
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003758static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3759 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003760 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003761{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003762 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003763 int status;
3764 struct nfs4_lookup_arg args = {
3765 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003766 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003767 .name = name,
3768 };
3769 struct nfs4_lookup_res res = {
3770 .server = server,
3771 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003772 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003773 .fh = fhandle,
3774 };
3775 struct rpc_message msg = {
3776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3777 .rpc_argp = &args,
3778 .rpc_resp = &res,
3779 };
3780
David Quigleyaa9c2662013-05-22 12:50:44 -04003781 args.bitmask = nfs4_bitmask(server, label);
3782
David Howells2b3de442006-08-22 20:06:09 -04003783 nfs_fattr_init(fattr);
3784
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003786 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 dprintk("NFS reply lookup: %d\n", status);
3788 return status;
3789}
3790
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003791static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003792{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003793 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003794 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003795 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3796 fattr->nlink = 2;
3797}
3798
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003799static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003800 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003801 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003802{
3803 struct nfs4_exception exception = { };
3804 struct rpc_clnt *client = *clnt;
3805 int err;
3806 do {
David Quigley1775fd32013-05-22 12:50:42 -04003807 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003808 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003809 switch (err) {
3810 case -NFS4ERR_BADNAME:
3811 err = -ENOENT;
3812 goto out;
3813 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003814 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003815 if (err == -NFS4ERR_MOVED)
3816 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003817 goto out;
3818 case -NFS4ERR_WRONGSEC:
3819 err = -EPERM;
3820 if (client != *clnt)
3821 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003822 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003823 if (IS_ERR(client))
3824 return PTR_ERR(client);
3825
3826 exception.retry = 1;
3827 break;
3828 default:
3829 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3830 }
3831 } while (exception.retry);
3832
3833out:
3834 if (err == 0)
3835 *clnt = client;
3836 else if (client != *clnt)
3837 rpc_shutdown_client(client);
3838
3839 return err;
3840}
3841
Al Virobeffb8f2016-07-20 16:34:42 -04003842static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003843 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3844 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003846 int status;
3847 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003848
David Quigley1775fd32013-05-22 12:50:42 -04003849 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003850 if (client != NFS_CLIENT(dir)) {
3851 rpc_shutdown_client(client);
3852 nfs_fixup_secinfo_attributes(fattr);
3853 }
3854 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855}
3856
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003857struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003858nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003859 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3860{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003861 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003862 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003863
David Quigley1775fd32013-05-22 12:50:42 -04003864 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003865 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003866 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003867 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003868}
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3871{
Trond Myklebust76b32992007-08-10 17:45:11 -04003872 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 struct nfs4_accessargs args = {
3874 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003875 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003877 struct nfs4_accessres res = {
3878 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003879 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 struct rpc_message msg = {
3881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3882 .rpc_argp = &args,
3883 .rpc_resp = &res,
3884 .rpc_cred = entry->cred,
3885 };
3886 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003887 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
3889 /*
3890 * Determine which access bits we want to ask for...
3891 */
3892 if (mode & MAY_READ)
3893 args.access |= NFS4_ACCESS_READ;
3894 if (S_ISDIR(inode->i_mode)) {
3895 if (mode & MAY_WRITE)
3896 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3897 if (mode & MAY_EXEC)
3898 args.access |= NFS4_ACCESS_LOOKUP;
3899 } else {
3900 if (mode & MAY_WRITE)
3901 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3902 if (mode & MAY_EXEC)
3903 args.access |= NFS4_ACCESS_EXECUTE;
3904 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003905
3906 res.fattr = nfs_alloc_fattr();
3907 if (res.fattr == NULL)
3908 return -ENOMEM;
3909
Bryan Schumaker7c513052011-03-24 17:12:24 +00003910 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003912 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003913 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003915 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 return status;
3917}
3918
3919static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3920{
3921 struct nfs4_exception exception = { };
3922 int err;
3923 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003924 err = _nfs4_proc_access(inode, entry);
3925 trace_nfs4_access(inode, err);
3926 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 &exception);
3928 } while (exception.retry);
3929 return err;
3930}
3931
3932/*
3933 * TODO: For the time being, we don't try to get any attributes
3934 * along with any of the zero-copy operations READ, READDIR,
3935 * READLINK, WRITE.
3936 *
3937 * In the case of the first three, we want to put the GETATTR
3938 * after the read-type operation -- this is because it is hard
3939 * to predict the length of a GETATTR response in v4, and thus
3940 * align the READ data correctly. This means that the GETATTR
3941 * may end up partially falling into the page cache, and we should
3942 * shift it into the 'tail' of the xdr_buf before processing.
3943 * To do this efficiently, we need to know the total length
3944 * of data received, which doesn't seem to be available outside
3945 * of the RPC layer.
3946 *
3947 * In the case of WRITE, we also want to put the GETATTR after
3948 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003949 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 *
3951 * Both of these changes to the XDR layer would in fact be quite
3952 * minor, but I decided to leave them for a subsequent patch.
3953 */
3954static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3955 unsigned int pgbase, unsigned int pglen)
3956{
3957 struct nfs4_readlink args = {
3958 .fh = NFS_FH(inode),
3959 .pgbase = pgbase,
3960 .pglen = pglen,
3961 .pages = &page,
3962 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003963 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3966 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003967 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 };
3969
Bryan Schumaker7c513052011-03-24 17:12:24 +00003970 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 -07003971}
3972
3973static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3974 unsigned int pgbase, unsigned int pglen)
3975{
3976 struct nfs4_exception exception = { };
3977 int err;
3978 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003979 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3980 trace_nfs4_readlink(inode, err);
3981 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 &exception);
3983 } while (exception.retry);
3984 return err;
3985}
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003988 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990static int
3991nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003992 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
David Quigleyaa9c2662013-05-22 12:50:44 -04003994 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003995 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 int status = 0;
3998
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003999 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
4000 if (IS_ERR(ctx))
4001 return PTR_ERR(ctx);
4002
David Quigleyaa9c2662013-05-22 12:50:44 -04004003 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4004
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004005 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004006 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 if (IS_ERR(state)) {
4008 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004012 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004013 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 return status;
4015}
4016
Al Virobeffb8f2016-07-20 16:34:42 -04004017static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Trond Myklebust16e42952005-10-27 22:12:44 -04004019 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004020 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004022 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004024 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004025 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4029 .rpc_argp = &args,
4030 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004032 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004033
Bryan Schumaker7c513052011-03-24 17:12:24 +00004034 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004035 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004036 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 return status;
4038}
4039
Al Virobeffb8f2016-07-20 16:34:42 -04004040static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
4042 struct nfs4_exception exception = { };
4043 int err;
4044 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004045 err = _nfs4_proc_remove(dir, name);
4046 trace_nfs4_remove(dir, name, err);
4047 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 &exception);
4049 } while (exception.retry);
4050 return err;
4051}
4052
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004053static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004055 struct nfs_server *server = NFS_SERVER(dir);
4056 struct nfs_removeargs *args = msg->rpc_argp;
4057 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004059 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004061 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004062
4063 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004066static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4067{
Al Viro884be172016-04-28 23:56:31 -04004068 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004069 &data->args.seq_args,
4070 &data->res.seq_res,
4071 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072}
4073
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004074static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004076 struct nfs_unlinkdata *data = task->tk_calldata;
4077 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004078
Trond Myklebust14516c32010-07-31 14:29:06 -04004079 if (!nfs4_sequence_done(task, &res->seq_res))
4080 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004081 if (nfs4_async_handle_error(task, res->server, NULL,
4082 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004083 return 0;
4084 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004085 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
Jeff Laytond3d41522010-09-17 17:31:57 -04004088static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4089{
4090 struct nfs_server *server = NFS_SERVER(dir);
4091 struct nfs_renameargs *arg = msg->rpc_argp;
4092 struct nfs_renameres *res = msg->rpc_resp;
4093
4094 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004095 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004096 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004097}
4098
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004099static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4100{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004101 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4102 &data->args.seq_args,
4103 &data->res.seq_res,
4104 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004105}
4106
4107static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4108 struct inode *new_dir)
4109{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004110 struct nfs_renamedata *data = task->tk_calldata;
4111 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004112
4113 if (!nfs4_sequence_done(task, &res->seq_res))
4114 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004115 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004116 return 0;
4117
4118 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004119 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004120 return 1;
4121}
4122
Al Virobeffb8f2016-07-20 16:34:42 -04004123static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004125 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 struct nfs4_link_arg arg = {
4127 .fh = NFS_FH(inode),
4128 .dir_fh = NFS_FH(dir),
4129 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004130 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004132 struct nfs4_link_res res = {
4133 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004134 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 struct rpc_message msg = {
4137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4138 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004139 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004141 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Trond Myklebust136f2622010-04-16 16:22:49 -04004143 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004144 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004145 goto out;
4146
David Quigley14c43f72013-05-22 12:50:43 -04004147 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4148 if (IS_ERR(res.label)) {
4149 status = PTR_ERR(res.label);
4150 goto out;
4151 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004152 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004153
Bryan Schumaker7c513052011-03-24 17:12:24 +00004154 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004155 if (!status) {
4156 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004157 status = nfs_post_op_update_inode(inode, res.fattr);
4158 if (!status)
4159 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004160 }
David Quigley14c43f72013-05-22 12:50:43 -04004161
4162
4163 nfs4_label_free(res.label);
4164
Trond Myklebust136f2622010-04-16 16:22:49 -04004165out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004166 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return status;
4168}
4169
Al Virobeffb8f2016-07-20 16:34:42 -04004170static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171{
4172 struct nfs4_exception exception = { };
4173 int err;
4174 do {
4175 err = nfs4_handle_exception(NFS_SERVER(inode),
4176 _nfs4_proc_link(inode, dir, name),
4177 &exception);
4178 } while (exception.retry);
4179 return err;
4180}
4181
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004182struct nfs4_createdata {
4183 struct rpc_message msg;
4184 struct nfs4_create_arg arg;
4185 struct nfs4_create_res res;
4186 struct nfs_fh fh;
4187 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004188 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004189};
4190
4191static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004192 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004193{
4194 struct nfs4_createdata *data;
4195
4196 data = kzalloc(sizeof(*data), GFP_KERNEL);
4197 if (data != NULL) {
4198 struct nfs_server *server = NFS_SERVER(dir);
4199
David Quigley14c43f72013-05-22 12:50:43 -04004200 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4201 if (IS_ERR(data->label))
4202 goto out_free;
4203
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4205 data->msg.rpc_argp = &data->arg;
4206 data->msg.rpc_resp = &data->res;
4207 data->arg.dir_fh = NFS_FH(dir);
4208 data->arg.server = server;
4209 data->arg.name = name;
4210 data->arg.attrs = sattr;
4211 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004212 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004213 data->res.server = server;
4214 data->res.fh = &data->fh;
4215 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004216 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004217 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004218 }
4219 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004220out_free:
4221 kfree(data);
4222 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004223}
4224
4225static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4226{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004227 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004228 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004229 if (status == 0) {
4230 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004231 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232 }
4233 return status;
4234}
4235
4236static void nfs4_free_createdata(struct nfs4_createdata *data)
4237{
David Quigley14c43f72013-05-22 12:50:43 -04004238 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004239 kfree(data);
4240}
4241
Chuck Lever4f390c12006-08-22 20:06:22 -04004242static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004243 struct page *page, unsigned int len, struct iattr *sattr,
4244 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246 struct nfs4_createdata *data;
4247 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Chuck Lever94a6d752006-08-22 20:06:23 -04004249 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004250 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004251
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 status = -ENOMEM;
4253 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4254 if (data == NULL)
4255 goto out;
4256
4257 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4258 data->arg.u.symlink.pages = &page;
4259 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004260 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004262 status = nfs4_do_create(dir, dentry, data);
4263
4264 nfs4_free_createdata(data);
4265out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return status;
4267}
4268
Chuck Lever4f390c12006-08-22 20:06:22 -04004269static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004270 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004273 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004275
4276 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004279 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4280 trace_nfs4_symlink(dir, &dentry->d_name, err);
4281 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 &exception);
4283 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004284
4285 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return err;
4287}
4288
4289static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004290 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004292 struct nfs4_createdata *data;
4293 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004295 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4296 if (data == NULL)
4297 goto out;
4298
David Quigley1775fd32013-05-22 12:50:42 -04004299 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004300 status = nfs4_do_create(dir, dentry, data);
4301
4302 nfs4_free_createdata(data);
4303out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return status;
4305}
4306
4307static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4308 struct iattr *sattr)
4309{
4310 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004311 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004313
David Quigleyaa9c2662013-05-22 12:50:44 -04004314 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4315
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004316 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004318 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4319 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4320 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 &exception);
4322 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004323 nfs4_label_release_security(label);
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return err;
4326}
4327
4328static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004329 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
David Howells2b0143b2015-03-17 22:25:59 +00004331 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 struct nfs4_readdir_arg args = {
4333 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004334 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 .pgbase = 0,
4336 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004337 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004338 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 };
4340 struct nfs4_readdir_res res;
4341 struct rpc_message msg = {
4342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4343 .rpc_argp = &args,
4344 .rpc_resp = &res,
4345 .rpc_cred = cred,
4346 };
4347 int status;
4348
Al Viro6de14722013-09-16 10:53:17 -04004349 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4350 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004351 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004352 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004354 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 -05004355 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004356 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004357 status += args.pgbase;
4358 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004359
4360 nfs_invalidate_atime(dir);
4361
Harvey Harrison3110ff82008-05-02 13:42:44 -07004362 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 return status;
4364}
4365
4366static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004367 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
4369 struct nfs4_exception exception = { };
4370 int err;
4371 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004372 err = _nfs4_proc_readdir(dentry, cred, cookie,
4373 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004374 trace_nfs4_readdir(d_inode(dentry), err);
4375 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 &exception);
4377 } while (exception.retry);
4378 return err;
4379}
4380
4381static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004382 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004384 struct nfs4_createdata *data;
4385 int mode = sattr->ia_mode;
4386 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004388 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4389 if (data == NULL)
4390 goto out;
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004393 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004395 data->arg.ftype = NF4BLK;
4396 data->arg.u.device.specdata1 = MAJOR(rdev);
4397 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 }
4399 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004400 data->arg.ftype = NF4CHR;
4401 data->arg.u.device.specdata1 = MAJOR(rdev);
4402 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004403 } else if (!S_ISSOCK(mode)) {
4404 status = -EINVAL;
4405 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 }
David Quigley1775fd32013-05-22 12:50:42 -04004407
David Quigleyaa9c2662013-05-22 12:50:44 -04004408 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004409 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004410out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004411 nfs4_free_createdata(data);
4412out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return status;
4414}
4415
4416static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4417 struct iattr *sattr, dev_t rdev)
4418{
4419 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004420 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004422
David Quigleyaa9c2662013-05-22 12:50:44 -04004423 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4424
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004425 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004427 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4428 trace_nfs4_mknod(dir, &dentry->d_name, err);
4429 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 &exception);
4431 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004432
4433 nfs4_label_release_security(label);
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return err;
4436}
4437
4438static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4439 struct nfs_fsstat *fsstat)
4440{
4441 struct nfs4_statfs_arg args = {
4442 .fh = fhandle,
4443 .bitmask = server->attr_bitmask,
4444 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004445 struct nfs4_statfs_res res = {
4446 .fsstat = fsstat,
4447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 struct rpc_message msg = {
4449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4450 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004451 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 };
4453
Trond Myklebust0e574af2005-10-27 22:12:38 -04004454 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004455 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}
4457
4458static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4459{
4460 struct nfs4_exception exception = { };
4461 int err;
4462 do {
4463 err = nfs4_handle_exception(server,
4464 _nfs4_proc_statfs(server, fhandle, fsstat),
4465 &exception);
4466 } while (exception.retry);
4467 return err;
4468}
4469
4470static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4471 struct nfs_fsinfo *fsinfo)
4472{
4473 struct nfs4_fsinfo_arg args = {
4474 .fh = fhandle,
4475 .bitmask = server->attr_bitmask,
4476 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004477 struct nfs4_fsinfo_res res = {
4478 .fsinfo = fsinfo,
4479 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 struct rpc_message msg = {
4481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4482 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004483 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 };
4485
Bryan Schumaker7c513052011-03-24 17:12:24 +00004486 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
4489static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4490{
4491 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004492 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 int err;
4494
4495 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004496 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004497 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004498 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004499 nfs4_set_lease_period(server->nfs_client,
4500 fsinfo->lease_time * HZ,
4501 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004502 break;
4503 }
4504 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 } while (exception.retry);
4506 return err;
4507}
4508
4509static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4510{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004511 int error;
4512
Trond Myklebust0e574af2005-10-27 22:12:38 -04004513 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004514 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004515 if (error == 0) {
4516 /* block layout checks this! */
4517 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004518 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004519 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004520
4521 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522}
4523
4524static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4525 struct nfs_pathconf *pathconf)
4526{
4527 struct nfs4_pathconf_arg args = {
4528 .fh = fhandle,
4529 .bitmask = server->attr_bitmask,
4530 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004531 struct nfs4_pathconf_res res = {
4532 .pathconf = pathconf,
4533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 struct rpc_message msg = {
4535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4536 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004537 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 };
4539
4540 /* None of the pathconf attributes are mandatory to implement */
4541 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4542 memset(pathconf, 0, sizeof(*pathconf));
4543 return 0;
4544 }
4545
Trond Myklebust0e574af2005-10-27 22:12:38 -04004546 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004547 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548}
4549
4550static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4551 struct nfs_pathconf *pathconf)
4552{
4553 struct nfs4_exception exception = { };
4554 int err;
4555
4556 do {
4557 err = nfs4_handle_exception(server,
4558 _nfs4_proc_pathconf(server, fhandle, pathconf),
4559 &exception);
4560 } while (exception.retry);
4561 return err;
4562}
4563
Trond Myklebust5521abf2013-03-16 20:54:34 -04004564int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004565 const struct nfs_open_context *ctx,
4566 const struct nfs_lock_context *l_ctx,
4567 fmode_t fmode)
4568{
4569 const struct nfs_lockowner *lockowner = NULL;
4570
4571 if (l_ctx != NULL)
4572 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004573 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004574}
4575EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4576
Trond Myklebust5521abf2013-03-16 20:54:34 -04004577static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4578 const struct nfs_open_context *ctx,
4579 const struct nfs_lock_context *l_ctx,
4580 fmode_t fmode)
4581{
4582 nfs4_stateid current_stateid;
4583
Trond Myklebuste1253be2014-03-05 08:44:23 -05004584 /* If the current stateid represents a lost lock, then exit */
4585 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4586 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004587 return nfs4_stateid_match(stateid, &current_stateid);
4588}
4589
4590static bool nfs4_error_stateid_expired(int err)
4591{
4592 switch (err) {
4593 case -NFS4ERR_DELEG_REVOKED:
4594 case -NFS4ERR_ADMIN_REVOKED:
4595 case -NFS4ERR_BAD_STATEID:
4596 case -NFS4ERR_STALE_STATEID:
4597 case -NFS4ERR_OLD_STATEID:
4598 case -NFS4ERR_OPENMODE:
4599 case -NFS4ERR_EXPIRED:
4600 return true;
4601 }
4602 return false;
4603}
4604
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004605static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004607 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004609 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004610 if (task->tk_status < 0) {
4611 struct nfs4_exception exception = {
4612 .inode = hdr->inode,
4613 .state = hdr->args.context->state,
4614 .stateid = &hdr->args.stateid,
4615 };
4616 task->tk_status = nfs4_async_handle_exception(task,
4617 server, task->tk_status, &exception);
4618 if (exception.retry) {
4619 rpc_restart_call_prepare(task);
4620 return -EAGAIN;
4621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004625 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627}
4628
Trond Myklebust5521abf2013-03-16 20:54:34 -04004629static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004630 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004631{
4632
4633 if (!nfs4_error_stateid_expired(task->tk_status) ||
4634 nfs4_stateid_is_current(&args->stateid,
4635 args->context,
4636 args->lock_context,
4637 FMODE_READ))
4638 return false;
4639 rpc_restart_call_prepare(task);
4640 return true;
4641}
4642
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004644{
4645
4646 dprintk("--> %s\n", __func__);
4647
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004648 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004649 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004650 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004651 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004652 if (task->tk_status > 0)
4653 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004654 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4655 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004656}
4657
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004658static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4659 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004662 if (!hdr->pgio_done_cb)
4663 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004664 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666}
4667
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4669 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004670{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4672 &hdr->args.seq_args,
4673 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004674 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004675 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004676 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4677 hdr->args.lock_context,
4678 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004679 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004681 return -EIO;
4682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685static int nfs4_write_done_cb(struct rpc_task *task,
4686 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004689
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004691 if (task->tk_status < 0) {
4692 struct nfs4_exception exception = {
4693 .inode = hdr->inode,
4694 .state = hdr->args.context->state,
4695 .stateid = &hdr->args.stateid,
4696 };
4697 task->tk_status = nfs4_async_handle_exception(task,
4698 NFS_SERVER(inode), task->tk_status,
4699 &exception);
4700 if (exception.retry) {
4701 rpc_restart_call_prepare(task);
4702 return -EAGAIN;
4703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004705 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004706 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004707 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004708 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
Trond Myklebust5521abf2013-03-16 20:54:34 -04004712static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004713 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004714{
4715
4716 if (!nfs4_error_stateid_expired(task->tk_status) ||
4717 nfs4_stateid_is_current(&args->stateid,
4718 args->context,
4719 args->lock_context,
4720 FMODE_WRITE))
4721 return false;
4722 rpc_restart_call_prepare(task);
4723 return true;
4724}
4725
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004727{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004728 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004729 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004730 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004731 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004732 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4733 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004734}
4735
Trond Myklebust5a37f852012-04-28 14:55:16 -04004736static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004737bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004738{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004739 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004741 return false;
4742 /* Otherwise, request attributes if and only if we don't hold
4743 * a delegation
4744 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004745 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004746}
Fred Isamana69aef12011-03-03 15:13:47 +00004747
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004748static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4749 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004751 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004752
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004753 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4754 hdr->args.bitmask = NULL;
4755 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004756 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004757 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004758
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004759 if (!hdr->pgio_done_cb)
4760 hdr->pgio_done_cb = nfs4_write_done_cb;
4761 hdr->res.server = server;
4762 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004764 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004765 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
Fred Isaman0b7c0152012-04-20 14:47:39 -04004768static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4769{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004770 nfs4_setup_sequence(NFS_SERVER(data->inode),
4771 &data->args.seq_args,
4772 &data->res.seq_res,
4773 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774}
4775
Fred Isaman0b7c0152012-04-20 14:47:39 -04004776static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004779
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004780 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004781 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4782 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004783 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004784 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787}
4788
Fred Isaman0b7c0152012-04-20 14:47:39 -04004789static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004790{
4791 if (!nfs4_sequence_done(task, &data->res.seq_res))
4792 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004793 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004794}
4795
Fred Isaman0b7c0152012-04-20 14:47:39 -04004796static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004798 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004799
Fred Isaman0b7c0152012-04-20 14:47:39 -04004800 if (data->commit_done_cb == NULL)
4801 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004802 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004803 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004804 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004807struct nfs4_renewdata {
4808 struct nfs_client *client;
4809 unsigned long timestamp;
4810};
4811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812/*
4813 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4814 * standalone procedure for queueing an asynchronous RENEW.
4815 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004816static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004817{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004818 struct nfs4_renewdata *data = calldata;
4819 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004820
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004821 if (atomic_read(&clp->cl_count) > 1)
4822 nfs4_schedule_state_renewal(clp);
4823 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004824 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004825}
4826
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004827static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004829 struct nfs4_renewdata *data = calldata;
4830 struct nfs_client *clp = data->client;
4831 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004833 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004834 switch (task->tk_status) {
4835 case 0:
4836 break;
4837 case -NFS4ERR_LEASE_MOVED:
4838 nfs4_schedule_lease_moved_recovery(clp);
4839 break;
4840 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004841 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004842 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4843 return;
4844 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004845 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004846 return;
4847 }
4848 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004850 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004853static const struct rpc_call_ops nfs4_renew_ops = {
4854 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004855 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004856};
4857
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004858static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
4860 struct rpc_message msg = {
4861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4862 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004863 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004865 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004867 if (renew_flags == 0)
4868 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004869 if (!atomic_inc_not_zero(&clp->cl_count))
4870 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004871 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004872 if (data == NULL)
4873 return -ENOMEM;
4874 data->client = clp;
4875 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004876 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004877 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
4879
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004880static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881{
4882 struct rpc_message msg = {
4883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4884 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004885 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 };
4887 unsigned long now = jiffies;
4888 int status;
4889
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004890 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 if (status < 0)
4892 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004893 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 return 0;
4895}
4896
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004897static inline int nfs4_server_supports_acls(struct nfs_server *server)
4898{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004899 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004900}
4901
Trond Myklebust21f498c2012-08-24 10:59:25 -04004902/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4903 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004904 * the stack.
4905 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004906#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004907
Neil Hormane9e3d722011-03-04 19:26:03 -05004908static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004909 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004910{
4911 struct page *newpage, **spages;
4912 int rc = 0;
4913 size_t len;
4914 spages = pages;
4915
4916 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004917 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004918 newpage = alloc_page(GFP_KERNEL);
4919
4920 if (newpage == NULL)
4921 goto unwind;
4922 memcpy(page_address(newpage), buf, len);
4923 buf += len;
4924 buflen -= len;
4925 *pages++ = newpage;
4926 rc++;
4927 } while (buflen != 0);
4928
4929 return rc;
4930
4931unwind:
4932 for(; rc > 0; rc--)
4933 __free_page(spages[rc-1]);
4934 return -ENOMEM;
4935}
4936
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004937struct nfs4_cached_acl {
4938 int cached;
4939 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004940 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004941};
4942
4943static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004944{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004945 struct nfs_inode *nfsi = NFS_I(inode);
4946
4947 spin_lock(&inode->i_lock);
4948 kfree(nfsi->nfs4_acl);
4949 nfsi->nfs4_acl = acl;
4950 spin_unlock(&inode->i_lock);
4951}
4952
4953static void nfs4_zap_acl_attr(struct inode *inode)
4954{
4955 nfs4_set_cached_acl(inode, NULL);
4956}
4957
4958static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4959{
4960 struct nfs_inode *nfsi = NFS_I(inode);
4961 struct nfs4_cached_acl *acl;
4962 int ret = -ENOENT;
4963
4964 spin_lock(&inode->i_lock);
4965 acl = nfsi->nfs4_acl;
4966 if (acl == NULL)
4967 goto out;
4968 if (buf == NULL) /* user is just asking for length */
4969 goto out_len;
4970 if (acl->cached == 0)
4971 goto out;
4972 ret = -ERANGE; /* see getxattr(2) man page */
4973 if (acl->len > buflen)
4974 goto out;
4975 memcpy(buf, acl->data, acl->len);
4976out_len:
4977 ret = acl->len;
4978out:
4979 spin_unlock(&inode->i_lock);
4980 return ret;
4981}
4982
Sachin Prabhu5794d212012-04-17 14:36:40 +01004983static 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 +00004984{
4985 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004986 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004987
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004988 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004989 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004990 if (acl == NULL)
4991 goto out;
4992 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004993 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004994 } else {
4995 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4996 if (acl == NULL)
4997 goto out;
4998 acl->cached = 0;
4999 }
5000 acl->len = acl_len;
5001out:
5002 nfs4_set_cached_acl(inode, acl);
5003}
5004
Andy Adamsonbf118a32011-12-07 11:55:27 -05005005/*
5006 * The getxattr API returns the required buffer length when called with a
5007 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5008 * the required buf. On a NULL buf, we send a page of data to the server
5009 * guessing that the ACL request can be serviced by a page. If so, we cache
5010 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5011 * the cache. If not so, we throw away the page, and cache the required
5012 * length. The next getxattr call will then produce another round trip to
5013 * the server, this time with the input buf of the required size.
5014 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005015static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005016{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005017 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005018 struct nfs_getaclargs args = {
5019 .fh = NFS_FH(inode),
5020 .acl_pages = pages,
5021 .acl_len = buflen,
5022 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005023 struct nfs_getaclres res = {
5024 .acl_len = buflen,
5025 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005026 struct rpc_message msg = {
5027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5028 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005029 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005030 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005031 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005032 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005033
Trond Myklebust21f498c2012-08-24 10:59:25 -04005034 if (npages > ARRAY_SIZE(pages))
5035 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005036
Andy Adamsonbf118a32011-12-07 11:55:27 -05005037 for (i = 0; i < npages; i++) {
5038 pages[i] = alloc_page(GFP_KERNEL);
5039 if (!pages[i])
5040 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005041 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005042
5043 /* for decoding across pages */
5044 res.acl_scratch = alloc_page(GFP_KERNEL);
5045 if (!res.acl_scratch)
5046 goto out_free;
5047
Andy Adamsonbf118a32011-12-07 11:55:27 -05005048 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005049
Peng Taode040be2012-01-10 22:42:47 +08005050 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005051 __func__, buf, buflen, npages, args.acl_len);
5052 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5053 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005054 if (ret)
5055 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005056
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005057 /* Handle the case where the passed-in buffer is too short */
5058 if (res.acl_flags & NFS4_ACL_TRUNC) {
5059 /* Did the user only issue a request for the acl length? */
5060 if (buf == NULL)
5061 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005062 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005063 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005064 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005065 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005066 if (buf) {
5067 if (res.acl_len > buflen) {
5068 ret = -ERANGE;
5069 goto out_free;
5070 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005071 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005072 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005073out_ok:
5074 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005075out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005076 for (i = 0; i < npages; i++)
5077 if (pages[i])
5078 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005079 if (res.acl_scratch)
5080 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005081 return ret;
5082}
5083
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005084static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5085{
5086 struct nfs4_exception exception = { };
5087 ssize_t ret;
5088 do {
5089 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005090 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005091 if (ret >= 0)
5092 break;
5093 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5094 } while (exception.retry);
5095 return ret;
5096}
5097
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005098static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5099{
5100 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005101 int ret;
5102
5103 if (!nfs4_server_supports_acls(server))
5104 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005105 ret = nfs_revalidate_inode(server, inode);
5106 if (ret < 0)
5107 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005108 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5109 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005110 ret = nfs4_read_cached_acl(inode, buf, buflen);
5111 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005112 /* -ENOENT is returned if there is no ACL or if there is an ACL
5113 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005114 return ret;
5115 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005116}
5117
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005118static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005119{
5120 struct nfs_server *server = NFS_SERVER(inode);
5121 struct page *pages[NFS4ACL_MAXPAGES];
5122 struct nfs_setaclargs arg = {
5123 .fh = NFS_FH(inode),
5124 .acl_pages = pages,
5125 .acl_len = buflen,
5126 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005127 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005128 struct rpc_message msg = {
5129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5130 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005131 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005132 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005133 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005134 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005135
5136 if (!nfs4_server_supports_acls(server))
5137 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005138 if (npages > ARRAY_SIZE(pages))
5139 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005140 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005141 if (i < 0)
5142 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005143 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005144 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005145
5146 /*
5147 * Free each page after tx, so the only ref left is
5148 * held by the network stack
5149 */
5150 for (; i > 0; i--)
5151 put_page(pages[i-1]);
5152
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005153 /*
5154 * Acl update can result in inode attribute update.
5155 * so mark the attribute cache invalid.
5156 */
5157 spin_lock(&inode->i_lock);
5158 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5159 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005160 nfs_access_zap_cache(inode);
5161 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005162 return ret;
5163}
5164
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005165static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5166{
5167 struct nfs4_exception exception = { };
5168 int err;
5169 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005170 err = __nfs4_proc_set_acl(inode, buf, buflen);
5171 trace_nfs4_set_acl(inode, err);
5172 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005173 &exception);
5174 } while (exception.retry);
5175 return err;
5176}
5177
David Quigleyaa9c2662013-05-22 12:50:44 -04005178#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5179static int _nfs4_get_security_label(struct inode *inode, void *buf,
5180 size_t buflen)
5181{
5182 struct nfs_server *server = NFS_SERVER(inode);
5183 struct nfs_fattr fattr;
5184 struct nfs4_label label = {0, 0, buflen, buf};
5185
5186 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005187 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005188 .fh = NFS_FH(inode),
5189 .bitmask = bitmask,
5190 };
5191 struct nfs4_getattr_res res = {
5192 .fattr = &fattr,
5193 .label = &label,
5194 .server = server,
5195 };
5196 struct rpc_message msg = {
5197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005198 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005199 .rpc_resp = &res,
5200 };
5201 int ret;
5202
5203 nfs_fattr_init(&fattr);
5204
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005205 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005206 if (ret)
5207 return ret;
5208 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5209 return -ENOENT;
5210 if (buflen < label.len)
5211 return -ERANGE;
5212 return 0;
5213}
5214
5215static int nfs4_get_security_label(struct inode *inode, void *buf,
5216 size_t buflen)
5217{
5218 struct nfs4_exception exception = { };
5219 int err;
5220
5221 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5222 return -EOPNOTSUPP;
5223
5224 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005225 err = _nfs4_get_security_label(inode, buf, buflen);
5226 trace_nfs4_get_security_label(inode, err);
5227 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005228 &exception);
5229 } while (exception.retry);
5230 return err;
5231}
5232
5233static int _nfs4_do_set_security_label(struct inode *inode,
5234 struct nfs4_label *ilabel,
5235 struct nfs_fattr *fattr,
5236 struct nfs4_label *olabel)
5237{
5238
5239 struct iattr sattr = {0};
5240 struct nfs_server *server = NFS_SERVER(inode);
5241 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005242 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005243 .fh = NFS_FH(inode),
5244 .iap = &sattr,
5245 .server = server,
5246 .bitmask = bitmask,
5247 .label = ilabel,
5248 };
5249 struct nfs_setattrres res = {
5250 .fattr = fattr,
5251 .label = olabel,
5252 .server = server,
5253 };
5254 struct rpc_message msg = {
5255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005256 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005257 .rpc_resp = &res,
5258 };
5259 int status;
5260
Jeff Layton12207f62013-11-01 10:49:32 -04005261 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005262
Jeff Layton12207f62013-11-01 10:49:32 -04005263 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005264 if (status)
5265 dprintk("%s failed: %d\n", __func__, status);
5266
5267 return status;
5268}
5269
5270static int nfs4_do_set_security_label(struct inode *inode,
5271 struct nfs4_label *ilabel,
5272 struct nfs_fattr *fattr,
5273 struct nfs4_label *olabel)
5274{
5275 struct nfs4_exception exception = { };
5276 int err;
5277
5278 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005279 err = _nfs4_do_set_security_label(inode, ilabel,
5280 fattr, olabel);
5281 trace_nfs4_set_security_label(inode, err);
5282 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005283 &exception);
5284 } while (exception.retry);
5285 return err;
5286}
5287
5288static int
Al Viro59301222016-05-27 10:19:30 -04005289nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005290{
5291 struct nfs4_label ilabel, *olabel = NULL;
5292 struct nfs_fattr fattr;
5293 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005294 int status;
5295
5296 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5297 return -EOPNOTSUPP;
5298
5299 nfs_fattr_init(&fattr);
5300
5301 ilabel.pi = 0;
5302 ilabel.lfs = 0;
5303 ilabel.label = (char *)buf;
5304 ilabel.len = buflen;
5305
5306 cred = rpc_lookup_cred();
5307 if (IS_ERR(cred))
5308 return PTR_ERR(cred);
5309
5310 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5311 if (IS_ERR(olabel)) {
5312 status = -PTR_ERR(olabel);
5313 goto out;
5314 }
5315
5316 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5317 if (status == 0)
5318 nfs_setsecurity(inode, &fattr, olabel);
5319
5320 nfs4_label_free(olabel);
5321out:
5322 put_rpccred(cred);
5323 return status;
5324}
5325#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5326
5327
Chuck Leverf0920752012-05-21 22:45:41 -04005328static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5329 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005330{
5331 __be32 verf[2];
5332
Chuck Lever2c820d92012-05-21 22:45:33 -04005333 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5334 /* An impossible timestamp guarantees this value
5335 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005336 verf[0] = cpu_to_be32(U32_MAX);
5337 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005338 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005339 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005340 u64 ns = ktime_to_ns(nn->boot_time);
5341
5342 verf[0] = cpu_to_be32(ns >> 32);
5343 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005344 }
Chuck Levercd937102012-03-02 17:14:31 -05005345 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5346}
5347
Jeff Laytona3192682015-06-09 19:43:59 -04005348static int
5349nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005350{
Jeff Laytona3192682015-06-09 19:43:59 -04005351 size_t len;
5352 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005353
Trond Myklebustceb3a162015-01-03 15:16:04 -05005354 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005355 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005356
Jeff Laytona3192682015-06-09 19:43:59 -04005357 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005358 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005359 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5360 1 +
5361 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5362 1;
5363 rcu_read_unlock();
5364
5365 if (len > NFS4_OPAQUE_LIMIT + 1)
5366 return -EINVAL;
5367
5368 /*
5369 * Since this string is allocated at mount time, and held until the
5370 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5371 * about a memory-reclaim deadlock.
5372 */
5373 str = kmalloc(len, GFP_KERNEL);
5374 if (!str)
5375 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005376
Chuck Levere984a552012-09-14 17:24:21 -04005377 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005378 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005379 clp->cl_ipaddr,
5380 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5381 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005382 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005383
Jeff Laytona3192682015-06-09 19:43:59 -04005384 clp->cl_owner_id = str;
5385 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005386}
5387
Jeff Layton873e3852015-06-09 19:44:00 -04005388static int
5389nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005390{
Jeff Layton873e3852015-06-09 19:44:00 -04005391 size_t len;
5392 char *str;
5393
5394 len = 10 + 10 + 1 + 10 + 1 +
5395 strlen(nfs4_client_id_uniquifier) + 1 +
5396 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5397
5398 if (len > NFS4_OPAQUE_LIMIT + 1)
5399 return -EINVAL;
5400
5401 /*
5402 * Since this string is allocated at mount time, and held until the
5403 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5404 * about a memory-reclaim deadlock.
5405 */
5406 str = kmalloc(len, GFP_KERNEL);
5407 if (!str)
5408 return -ENOMEM;
5409
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005410 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005411 clp->rpc_ops->version, clp->cl_minorversion,
5412 nfs4_client_id_uniquifier,
5413 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005414 clp->cl_owner_id = str;
5415 return 0;
5416}
5417
5418static int
5419nfs4_init_uniform_client_string(struct nfs_client *clp)
5420{
Jeff Layton873e3852015-06-09 19:44:00 -04005421 size_t len;
5422 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005423
5424 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005425 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005426
5427 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005428 return nfs4_init_uniquifier_client_string(clp);
5429
5430 len = 10 + 10 + 1 + 10 + 1 +
5431 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5432
5433 if (len > NFS4_OPAQUE_LIMIT + 1)
5434 return -EINVAL;
5435
5436 /*
5437 * Since this string is allocated at mount time, and held until the
5438 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5439 * about a memory-reclaim deadlock.
5440 */
5441 str = kmalloc(len, GFP_KERNEL);
5442 if (!str)
5443 return -ENOMEM;
5444
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005445 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005446 clp->rpc_ops->version, clp->cl_minorversion,
5447 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005448 clp->cl_owner_id = str;
5449 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005450}
5451
Chuck Lever706cb8d2014-03-12 12:51:47 -04005452/*
5453 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5454 * services. Advertise one based on the address family of the
5455 * clientaddr.
5456 */
5457static unsigned int
5458nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5459{
5460 if (strchr(clp->cl_ipaddr, ':') != NULL)
5461 return scnprintf(buf, len, "tcp6");
5462 else
5463 return scnprintf(buf, len, "tcp");
5464}
5465
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005466static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5467{
5468 struct nfs4_setclientid *sc = calldata;
5469
5470 if (task->tk_status == 0)
5471 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5472}
5473
5474static const struct rpc_call_ops nfs4_setclientid_ops = {
5475 .rpc_call_done = nfs4_setclientid_done,
5476};
5477
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005478/**
5479 * nfs4_proc_setclientid - Negotiate client ID
5480 * @clp: state data structure
5481 * @program: RPC program for NFSv4 callback service
5482 * @port: IP port number for NFS4 callback service
5483 * @cred: RPC credential to use for this call
5484 * @res: where to place the result
5485 *
5486 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5487 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005488int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5489 unsigned short port, struct rpc_cred *cred,
5490 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491{
5492 nfs4_verifier sc_verifier;
5493 struct nfs4_setclientid setclientid = {
5494 .sc_verifier = &sc_verifier,
5495 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005496 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 };
5498 struct rpc_message msg = {
5499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5500 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005501 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005502 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005504 struct rpc_task *task;
5505 struct rpc_task_setup task_setup_data = {
5506 .rpc_client = clp->cl_rpcclient,
5507 .rpc_message = &msg,
5508 .callback_ops = &nfs4_setclientid_ops,
5509 .callback_data = &setclientid,
5510 .flags = RPC_TASK_TIMEOUT,
5511 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005512 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Chuck Leverde734832012-07-11 16:30:50 -04005514 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005515 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005516
5517 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5518 status = nfs4_init_uniform_client_string(clp);
5519 else
Jeff Laytona3192682015-06-09 19:43:59 -04005520 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005521
5522 if (status)
5523 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005524
Chuck Leverde734832012-07-11 16:30:50 -04005525 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005526 setclientid.sc_netid_len =
5527 nfs4_init_callback_netid(clp,
5528 setclientid.sc_netid,
5529 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005530 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005531 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 clp->cl_ipaddr, port >> 8, port & 255);
5533
Jeff Layton3a6bb732015-06-09 19:43:57 -04005534 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005535 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005536 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005537 task = rpc_run_task(&task_setup_data);
5538 if (IS_ERR(task)) {
5539 status = PTR_ERR(task);
5540 goto out;
5541 }
5542 status = task->tk_status;
5543 if (setclientid.sc_cred) {
Chuck Lever05f0b032019-10-04 09:58:54 -04005544 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005545 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5546 put_rpccred(setclientid.sc_cred);
5547 }
5548 rpc_put_task(task);
5549out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005550 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005551 dprintk("NFS reply setclientid: %d\n", status);
5552 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553}
5554
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005555/**
5556 * nfs4_proc_setclientid_confirm - Confirm client ID
5557 * @clp: state data structure
5558 * @res: result of a previous SETCLIENTID
5559 * @cred: RPC credential to use for this call
5560 *
5561 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5562 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005563int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005564 struct nfs4_setclientid_res *arg,
5565 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 struct rpc_message msg = {
5568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005569 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005570 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 int status;
5573
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005574 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5575 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5576 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005577 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005578 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005579 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 return status;
5581}
5582
Trond Myklebustfe650402006-01-03 09:55:18 +01005583struct nfs4_delegreturndata {
5584 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005585 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005586 struct nfs_fh fh;
5587 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005588 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005589 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005590 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005591 struct inode *inode;
5592 bool roc;
5593 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005594};
5595
Trond Myklebustfe650402006-01-03 09:55:18 +01005596static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5597{
5598 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005599
Trond Myklebust14516c32010-07-31 14:29:06 -04005600 if (!nfs4_sequence_done(task, &data->res.seq_res))
5601 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005602
Trond Myklebustca8acf82013-08-13 10:36:56 -04005603 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005604 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005605 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005606 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005607 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005608 case -NFS4ERR_ADMIN_REVOKED:
5609 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005610 case -NFS4ERR_EXPIRED:
5611 nfs4_free_revoked_stateid(data->res.server,
5612 data->args.stateid,
5613 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005614 case -NFS4ERR_BAD_STATEID:
5615 case -NFS4ERR_OLD_STATEID:
5616 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005617 task->tk_status = 0;
5618 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005619 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005620 if (nfs4_async_handle_error(task, data->res.server,
5621 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005622 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005623 return;
5624 }
5625 }
5626 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005627 if (data->roc && data->rpc_status == 0)
5628 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005629}
5630
5631static void nfs4_delegreturn_release(void *calldata)
5632{
Peng Tao039b7562014-07-03 13:05:02 +08005633 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005634 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005635
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005636 if (inode) {
5637 if (data->roc)
5638 pnfs_roc_release(inode);
5639 nfs_iput_and_deactive(inode);
5640 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005641 kfree(calldata);
5642}
5643
Andy Adamson938e1012009-04-01 09:22:28 -04005644static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5645{
5646 struct nfs4_delegreturndata *d_data;
5647
5648 d_data = (struct nfs4_delegreturndata *)data;
5649
Peng Tao500d7012015-09-22 11:35:22 +08005650 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5651 return;
5652
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005653 if (d_data->roc)
5654 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005655
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005656 nfs4_setup_sequence(d_data->res.server,
5657 &d_data->args.seq_args,
5658 &d_data->res.seq_res,
5659 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005660}
Andy Adamson938e1012009-04-01 09:22:28 -04005661
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005662static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005663 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005664 .rpc_call_done = nfs4_delegreturn_done,
5665 .rpc_release = nfs4_delegreturn_release,
5666};
5667
Trond Myklebuste6f81072008-01-24 18:14:34 -05005668static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005669{
5670 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005671 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005672 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005673 struct rpc_message msg = {
5674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5675 .rpc_cred = cred,
5676 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005677 struct rpc_task_setup task_setup_data = {
5678 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005679 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005680 .callback_ops = &nfs4_delegreturn_ops,
5681 .flags = RPC_TASK_ASYNC,
5682 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005683 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005684
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005685 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005686 if (data == NULL)
5687 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005688 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005689
5690 nfs4_state_protect(server->nfs_client,
5691 NFS_SP4_MACH_CRED_CLEANUP,
5692 &task_setup_data.rpc_client, &msg);
5693
Trond Myklebustfe650402006-01-03 09:55:18 +01005694 data->args.fhandle = &data->fh;
5695 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005696 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005697 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005698 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005699 data->res.fattr = &data->fattr;
5700 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005701 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005702 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005703 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005704 data->inode = nfs_igrab_and_active(inode);
5705 if (data->inode)
5706 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005707
Trond Myklebustc970aa82007-07-14 15:39:59 -04005708 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005709 msg.rpc_argp = &data->args;
5710 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005711 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005712 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005713 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005714 if (!issync)
5715 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005716 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005717 if (status != 0)
5718 goto out;
5719 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005720 if (status == 0)
5721 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5722 else
5723 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005724out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005725 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005726 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727}
5728
Trond Myklebuste6f81072008-01-24 18:14:34 -05005729int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730{
5731 struct nfs_server *server = NFS_SERVER(inode);
5732 struct nfs4_exception exception = { };
5733 int err;
5734 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005735 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005736 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 switch (err) {
5738 case -NFS4ERR_STALE_STATEID:
5739 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 case 0:
5741 return 0;
5742 }
5743 err = nfs4_handle_exception(server, err, &exception);
5744 } while (exception.retry);
5745 return err;
5746}
5747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5749{
5750 struct inode *inode = state->inode;
5751 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005752 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005753 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005755 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005757 struct nfs_lockt_res res = {
5758 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 };
5760 struct rpc_message msg = {
5761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5762 .rpc_argp = &arg,
5763 .rpc_resp = &res,
5764 .rpc_cred = state->owner->so_cred,
5765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 struct nfs4_lock_state *lsp;
5767 int status;
5768
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005769 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005770 status = nfs4_set_lock_state(state, request);
5771 if (status != 0)
5772 goto out;
5773 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005774 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005775 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005776 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005777 switch (status) {
5778 case 0:
5779 request->fl_type = F_UNLCK;
5780 break;
5781 case -NFS4ERR_DENIED:
5782 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005784 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005785 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005786out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 return status;
5788}
5789
5790static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5791{
5792 struct nfs4_exception exception = { };
5793 int err;
5794
5795 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005796 err = _nfs4_proc_getlk(state, cmd, request);
5797 trace_nfs4_get_lock(request, state, cmd, err);
5798 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 &exception);
5800 } while (exception.retry);
5801 return err;
5802}
5803
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005804struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005805 struct nfs_locku_args arg;
5806 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005807 struct nfs4_lock_state *lsp;
5808 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005809 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005810 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005811 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005812};
5813
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005814static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5815 struct nfs_open_context *ctx,
5816 struct nfs4_lock_state *lsp,
5817 struct nfs_seqid *seqid)
5818{
5819 struct nfs4_unlockdata *p;
5820 struct inode *inode = lsp->ls_state->inode;
5821
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005822 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005823 if (p == NULL)
5824 return NULL;
5825 p->arg.fh = NFS_FH(inode);
5826 p->arg.fl = &p->fl;
5827 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005828 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005829 p->lsp = lsp;
5830 atomic_inc(&lsp->ls_count);
5831 /* Ensure we don't close file until we're done freeing locks! */
5832 p->ctx = get_nfs_open_context(ctx);
5833 memcpy(&p->fl, fl, sizeof(p->fl));
5834 p->server = NFS_SERVER(inode);
5835 return p;
5836}
5837
Trond Myklebust06f814a2006-01-03 09:55:07 +01005838static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005839{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005840 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005841 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005842 nfs4_put_lock_state(calldata->lsp);
5843 put_nfs_open_context(calldata->ctx);
5844 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005845}
5846
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005847static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005848{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005849 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005850
Trond Myklebust14516c32010-07-31 14:29:06 -04005851 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5852 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005853 switch (task->tk_status) {
5854 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005855 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005856 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005857 if (nfs4_update_lock_stateid(calldata->lsp,
5858 &calldata->res.stateid))
5859 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005860 case -NFS4ERR_ADMIN_REVOKED:
5861 case -NFS4ERR_EXPIRED:
5862 nfs4_free_revoked_stateid(calldata->server,
5863 &calldata->arg.stateid,
5864 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005865 case -NFS4ERR_BAD_STATEID:
5866 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005867 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005868 if (!nfs4_stateid_match(&calldata->arg.stateid,
5869 &calldata->lsp->ls_stateid))
5870 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005871 break;
5872 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005873 if (nfs4_async_handle_error(task, calldata->server,
5874 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005875 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005876 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005877 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005878}
5879
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005880static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005881{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005882 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005884 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005885 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005886 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005887 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005888 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005889 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005890 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005891 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005892 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005893 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005894 &calldata->res.seq_res,
5895 task) != 0)
5896 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005897 return;
5898out_no_action:
5899 task->tk_action = NULL;
5900out_wait:
5901 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902}
5903
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005904static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005905 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005906 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005907 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005908};
5909
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005910static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5911 struct nfs_open_context *ctx,
5912 struct nfs4_lock_state *lsp,
5913 struct nfs_seqid *seqid)
5914{
5915 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005916 struct rpc_message msg = {
5917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5918 .rpc_cred = ctx->cred,
5919 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005920 struct rpc_task_setup task_setup_data = {
5921 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005922 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005923 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005924 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005925 .flags = RPC_TASK_ASYNC,
5926 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005927
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005928 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5929 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5930
Frank Filz137d6ac2007-07-09 15:32:29 -07005931 /* Ensure this is an unlock - when canceling a lock, the
5932 * canceled lock is passed in, and it won't be an unlock.
5933 */
5934 fl->fl_type = F_UNLCK;
5935
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005936 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5937 if (data == NULL) {
5938 nfs_free_seqid(seqid);
5939 return ERR_PTR(-ENOMEM);
5940 }
5941
Chuck Levera9c92d62013-08-09 12:48:18 -04005942 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005943 msg.rpc_argp = &data->arg;
5944 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005945 task_setup_data.callback_data = data;
5946 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005947}
5948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5950{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005951 struct inode *inode = state->inode;
5952 struct nfs4_state_owner *sp = state->owner;
5953 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005954 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005955 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005956 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005957 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005958 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005959 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
Trond Myklebust9b073572006-06-29 16:38:34 -04005961 status = nfs4_set_lock_state(state, request);
5962 /* Unlock _before_ we do the RPC call */
5963 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005964 /* Exclude nfs_delegation_claim_locks() */
5965 mutex_lock(&sp->so_delegreturn_mutex);
5966 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005967 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005968 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005969 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005970 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005971 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005972 }
5973 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005974 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005975 if (status != 0)
5976 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005977 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005978 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005979 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5980 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005981 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5982 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005983 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005984 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005985 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005986 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005987 status = PTR_ERR(task);
5988 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005989 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005990 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005991 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005992out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005993 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005994 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005995 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996}
5997
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005998struct nfs4_lockdata {
5999 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006000 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006001 struct nfs4_lock_state *lsp;
6002 struct nfs_open_context *ctx;
6003 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006004 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006005 int rpc_status;
6006 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006007 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008};
6009
6010static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006011 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6012 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006013{
6014 struct nfs4_lockdata *p;
6015 struct inode *inode = lsp->ls_state->inode;
6016 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006017 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006018
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006019 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006020 if (p == NULL)
6021 return NULL;
6022
6023 p->arg.fh = NFS_FH(inode);
6024 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006025 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006026 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006027 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006028 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6029 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006030 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006031 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006032 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006033 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006034 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006035 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006036 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006037 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006038 atomic_inc(&lsp->ls_count);
6039 p->ctx = get_nfs_open_context(ctx);
6040 memcpy(&p->fl, fl, sizeof(p->fl));
6041 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006042out_free_seqid:
6043 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006044out_free:
6045 kfree(p);
6046 return NULL;
6047}
6048
6049static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6050{
6051 struct nfs4_lockdata *data = calldata;
6052 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Harvey Harrison3110ff82008-05-02 13:42:44 -07006054 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006055 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006056 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006057 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006058 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006059 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006060 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006061 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006062 nfs4_stateid_copy(&data->arg.open_stateid,
6063 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006065 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006066 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006067 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006068 nfs4_stateid_copy(&data->arg.lock_stateid,
6069 &data->lsp->ls_stateid);
6070 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006071 if (!nfs4_valid_open_stateid(state)) {
6072 data->rpc_status = -EBADF;
6073 task->tk_action = NULL;
6074 goto out_release_open_seqid;
6075 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006076 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006077 if (nfs4_setup_sequence(data->server,
6078 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006079 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006080 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006081 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006082out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006083 nfs_release_seqid(data->arg.open_seqid);
6084out_release_lock_seqid:
6085 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006086out_wait:
6087 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006088 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006089}
6090
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6092{
6093 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006094 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006095
Harvey Harrison3110ff82008-05-02 13:42:44 -07006096 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006097
Trond Myklebust14516c32010-07-31 14:29:06 -04006098 if (!nfs4_sequence_done(task, &data->res.seq_res))
6099 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006100
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006102 switch (task->tk_status) {
6103 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006104 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006105 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006106 if (data->arg.new_lock) {
6107 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006108 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006109 rpc_restart_call_prepare(task);
6110 break;
6111 }
6112 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006113 if (data->arg.new_lock_owner != 0) {
6114 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6115 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6116 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6117 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6118 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006119 break;
6120 case -NFS4ERR_BAD_STATEID:
6121 case -NFS4ERR_OLD_STATEID:
6122 case -NFS4ERR_STALE_STATEID:
6123 case -NFS4ERR_EXPIRED:
6124 if (data->arg.new_lock_owner != 0) {
6125 if (!nfs4_stateid_match(&data->arg.open_stateid,
6126 &lsp->ls_state->open_stateid))
6127 rpc_restart_call_prepare(task);
6128 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6129 &lsp->ls_stateid))
6130 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006131 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006132 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006133}
6134
6135static void nfs4_lock_release(void *calldata)
6136{
6137 struct nfs4_lockdata *data = calldata;
6138
Harvey Harrison3110ff82008-05-02 13:42:44 -07006139 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006140 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006141 if (data->cancelled != 0) {
6142 struct rpc_task *task;
6143 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6144 data->arg.lock_seqid);
6145 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006146 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006147 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006148 } else
6149 nfs_free_seqid(data->arg.lock_seqid);
6150 nfs4_put_lock_state(data->lsp);
6151 put_nfs_open_context(data->ctx);
6152 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006153 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006154}
6155
6156static const struct rpc_call_ops nfs4_lock_ops = {
6157 .rpc_call_prepare = nfs4_lock_prepare,
6158 .rpc_call_done = nfs4_lock_done,
6159 .rpc_release = nfs4_lock_release,
6160};
6161
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006162static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6163{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006164 switch (error) {
6165 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006166 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006167 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006168 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006169 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006170 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006171 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006172 break;
6173 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006174 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006175 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006176 };
6177}
6178
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006179static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006180{
6181 struct nfs4_lockdata *data;
6182 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006183 struct rpc_message msg = {
6184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6185 .rpc_cred = state->owner->so_cred,
6186 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006187 struct rpc_task_setup task_setup_data = {
6188 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006189 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006190 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006191 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006192 .flags = RPC_TASK_ASYNC,
6193 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006194 int ret;
6195
Harvey Harrison3110ff82008-05-02 13:42:44 -07006196 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006197 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006198 fl->fl_u.nfs4_fl.owner,
6199 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006200 if (data == NULL)
6201 return -ENOMEM;
6202 if (IS_SETLKW(cmd))
6203 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006204 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006205 msg.rpc_argp = &data->arg;
6206 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006207 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006208 if (recovery_type > NFS_LOCK_NEW) {
6209 if (recovery_type == NFS_LOCK_RECLAIM)
6210 data->arg.reclaim = NFS_LOCK_RECLAIM;
6211 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006212 } else
6213 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006214 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006215 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006216 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 ret = nfs4_wait_for_completion_rpc_task(task);
6218 if (ret == 0) {
6219 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006220 if (ret)
6221 nfs4_handle_setlk_error(data->server, data->lsp,
6222 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006223 } else
6224 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006225 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006226 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006227 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229}
6230
6231static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6232{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006233 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006234 struct nfs4_exception exception = {
6235 .inode = state->inode,
6236 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006237 int err;
6238
6239 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006240 /* Cache the lock if possible... */
6241 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6242 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006243 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006244 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006245 break;
6246 nfs4_handle_exception(server, err, &exception);
6247 } while (exception.retry);
6248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249}
6250
6251static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6252{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006253 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006254 struct nfs4_exception exception = {
6255 .inode = state->inode,
6256 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006257 int err;
6258
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006259 err = nfs4_set_lock_state(state, request);
6260 if (err != 0)
6261 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006262 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006263 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6264 return 0;
6265 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006266 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006267 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6268 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006269 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006270 switch (err) {
6271 default:
6272 goto out;
6273 case -NFS4ERR_GRACE:
6274 case -NFS4ERR_DELAY:
6275 nfs4_handle_exception(server, err, &exception);
6276 err = 0;
6277 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006278 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006279out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281}
6282
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006283#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006284static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6285{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006286 struct nfs4_lock_state *lsp;
6287 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006288
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006289 status = nfs4_set_lock_state(state, request);
6290 if (status != 0)
6291 return status;
6292 lsp = request->fl_u.nfs4_fl.owner;
6293 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6294 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6295 return 0;
6296 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006297 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006298}
6299#endif
6300
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6302{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006303 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006304 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006305 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006306 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
Trond Myklebust01c3b862006-06-29 16:38:39 -04006308 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006309 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006310 if (status < 0)
6311 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006312 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006313 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006314 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006315 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006316 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006317 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006318 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006319 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006320 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006321 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006322 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006323 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006324 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006325 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006326out:
6327 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 return status;
6329}
6330
6331static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6332{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006333 struct nfs4_exception exception = {
6334 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006335 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 int err;
6338
6339 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006340 err = _nfs4_proc_setlk(state, cmd, request);
6341 if (err == -NFS4ERR_DENIED)
6342 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006344 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 } while (exception.retry);
6346 return err;
6347}
6348
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006349#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6350#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6351
6352static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006353nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6354 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006355{
6356 int status = -ERESTARTSYS;
6357 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6358
6359 while(!signalled()) {
6360 status = nfs4_proc_setlk(state, cmd, request);
6361 if ((status != -EAGAIN) || IS_SETLK(cmd))
6362 break;
6363 freezable_schedule_timeout_interruptible(timeout);
6364 timeout *= 2;
6365 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6366 status = -ERESTARTSYS;
6367 }
6368 return status;
6369}
6370
Jeff Laytona1d617d82016-09-17 18:17:39 -04006371#ifdef CONFIG_NFS_V4_1
6372struct nfs4_lock_waiter {
6373 struct task_struct *task;
6374 struct inode *inode;
6375 struct nfs_lowner *owner;
6376 bool notified;
6377};
6378
6379static int
6380nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6381{
6382 int ret;
6383 struct cb_notify_lock_args *cbnl = key;
6384 struct nfs4_lock_waiter *waiter = wait->private;
6385 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6386 *wowner = waiter->owner;
6387
6388 /* Only wake if the callback was for the same owner */
6389 if (lowner->clientid != wowner->clientid ||
6390 lowner->id != wowner->id ||
6391 lowner->s_dev != wowner->s_dev)
6392 return 0;
6393
6394 /* Make sure it's for the right inode */
6395 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6396 return 0;
6397
6398 waiter->notified = true;
6399
6400 /* override "private" so we can use default_wake_function */
6401 wait->private = waiter->task;
6402 ret = autoremove_wake_function(wait, mode, flags, key);
6403 wait->private = waiter;
6404 return ret;
6405}
6406
6407static int
6408nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6409{
6410 int status = -ERESTARTSYS;
6411 unsigned long flags;
6412 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6413 struct nfs_server *server = NFS_SERVER(state->inode);
6414 struct nfs_client *clp = server->nfs_client;
6415 wait_queue_head_t *q = &clp->cl_lock_waitq;
6416 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6417 .id = lsp->ls_seqid.owner_id,
6418 .s_dev = server->s_dev };
6419 struct nfs4_lock_waiter waiter = { .task = current,
6420 .inode = state->inode,
6421 .owner = &owner,
6422 .notified = false };
6423 wait_queue_t wait;
6424
6425 /* Don't bother with waitqueue if we don't expect a callback */
6426 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6427 return nfs4_retry_setlk_simple(state, cmd, request);
6428
6429 init_wait(&wait);
6430 wait.private = &waiter;
6431 wait.func = nfs4_wake_lock_waiter;
6432 add_wait_queue(q, &wait);
6433
6434 while(!signalled()) {
6435 status = nfs4_proc_setlk(state, cmd, request);
6436 if ((status != -EAGAIN) || IS_SETLK(cmd))
6437 break;
6438
6439 status = -ERESTARTSYS;
6440 spin_lock_irqsave(&q->lock, flags);
6441 if (waiter.notified) {
6442 spin_unlock_irqrestore(&q->lock, flags);
6443 continue;
6444 }
6445 set_current_state(TASK_INTERRUPTIBLE);
6446 spin_unlock_irqrestore(&q->lock, flags);
6447
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006448 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006449 }
6450
6451 finish_wait(q, &wait);
6452 return status;
6453}
6454#else /* !CONFIG_NFS_V4_1 */
6455static inline int
6456nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6457{
6458 return nfs4_retry_setlk_simple(state, cmd, request);
6459}
6460#endif
6461
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462static int
6463nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6464{
6465 struct nfs_open_context *ctx;
6466 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 int status;
6468
6469 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006470 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 state = ctx->state;
6472
6473 if (request->fl_start < 0 || request->fl_end < 0)
6474 return -EINVAL;
6475
Trond Myklebustd9531262009-07-21 19:22:38 -04006476 if (IS_GETLK(cmd)) {
6477 if (state != NULL)
6478 return nfs4_proc_getlk(state, F_GETLK, request);
6479 return 0;
6480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481
6482 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6483 return -EINVAL;
6484
Trond Myklebustd9531262009-07-21 19:22:38 -04006485 if (request->fl_type == F_UNLCK) {
6486 if (state != NULL)
6487 return nfs4_proc_unlck(state, cmd, request);
6488 return 0;
6489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490
Trond Myklebustd9531262009-07-21 19:22:38 -04006491 if (state == NULL)
6492 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006493
6494 if ((request->fl_flags & FL_POSIX) &&
6495 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6496 return -ENOLCK;
6497
Trond Myklebust55725512012-04-18 12:48:35 -04006498 /*
6499 * Don't rely on the VFS having checked the file open mode,
6500 * since it won't do this for flock() locks.
6501 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006502 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006503 case F_RDLCK:
6504 if (!(filp->f_mode & FMODE_READ))
6505 return -EBADF;
6506 break;
6507 case F_WRLCK:
6508 if (!(filp->f_mode & FMODE_WRITE))
6509 return -EBADF;
6510 }
6511
Jeff Layton1ea67db2016-09-17 18:17:37 -04006512 status = nfs4_set_lock_state(state, request);
6513 if (status != 0)
6514 return status;
6515
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006516 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517}
6518
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006519int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006520{
6521 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006522 int err;
6523
6524 err = nfs4_set_lock_state(state, fl);
6525 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006526 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006527 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrown70f254a2017-12-13 09:57:09 +11006528 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006529}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006530
Trond Myklebustcf470c32012-03-07 13:49:12 -05006531struct nfs_release_lockowner_data {
6532 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006533 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006534 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006535 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006536 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006537};
6538
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006539static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6540{
6541 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006542 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006543 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6544 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006545 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006546 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006547}
6548
6549static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6550{
6551 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006552 struct nfs_server *server = data->server;
6553
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006554 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006555
6556 switch (task->tk_status) {
6557 case 0:
6558 renew_lease(server, data->timestamp);
6559 break;
6560 case -NFS4ERR_STALE_CLIENTID:
6561 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006562 nfs4_schedule_lease_recovery(server->nfs_client);
6563 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006564 case -NFS4ERR_LEASE_MOVED:
6565 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006566 if (nfs4_async_handle_error(task, server,
6567 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006568 rpc_restart_call_prepare(task);
6569 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006570}
6571
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006572static void nfs4_release_lockowner_release(void *calldata)
6573{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006574 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006575 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006576 kfree(calldata);
6577}
6578
Trond Myklebust17280172012-03-11 13:11:00 -04006579static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006580 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6581 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006582 .rpc_release = nfs4_release_lockowner_release,
6583};
6584
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006585static void
6586nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006587{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006588 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006589 struct rpc_message msg = {
6590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6591 };
6592
6593 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006594 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006595
Trond Myklebustcf470c32012-03-07 13:49:12 -05006596 data = kmalloc(sizeof(*data), GFP_NOFS);
6597 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006598 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006599 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006600 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006601 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6602 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6603 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006604
Trond Myklebustcf470c32012-03-07 13:49:12 -05006605 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006606 msg.rpc_resp = &data->res;
6607 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006608 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006609}
6610
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006611#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6612
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006613static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006614 struct dentry *unused, struct inode *inode,
6615 const char *key, const void *buf,
6616 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006617{
Al Viro59301222016-05-27 10:19:30 -04006618 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006619}
6620
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006621static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006622 struct dentry *unused, struct inode *inode,
6623 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006624{
Al Virob2968212016-04-10 20:48:24 -04006625 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006626}
6627
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006628static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006629{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006630 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006631}
6632
David Quigleyc9bccef2013-05-22 12:50:45 -04006633#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006634
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006635static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006636 struct dentry *unused, struct inode *inode,
6637 const char *key, const void *buf,
6638 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006639{
6640 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006641 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006642
6643 return -EOPNOTSUPP;
6644}
6645
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006646static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006647 struct dentry *unused, struct inode *inode,
6648 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006649{
6650 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006651 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006652 return -EOPNOTSUPP;
6653}
6654
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006655static ssize_t
6656nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006657{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006658 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006659
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006660 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6661 len = security_inode_listsecurity(inode, list, list_len);
6662 if (list_len && len > list_len)
6663 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006664 }
6665 return len;
6666}
6667
6668static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6669 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006670 .get = nfs4_xattr_get_nfs4_label,
6671 .set = nfs4_xattr_set_nfs4_label,
6672};
David Quigleyc9bccef2013-05-22 12:50:45 -04006673
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006674#else
6675
6676static ssize_t
6677nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6678{
6679 return 0;
6680}
6681
6682#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006683
Andy Adamson533eb462011-06-13 18:25:56 -04006684/*
6685 * nfs_fhget will use either the mounted_on_fileid or the fileid
6686 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006687static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6688{
Andy Adamson533eb462011-06-13 18:25:56 -04006689 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6690 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6691 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006692 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006693 return;
6694
6695 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006696 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006697 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6698 fattr->nlink = 2;
6699}
6700
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006701static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6702 const struct qstr *name,
6703 struct nfs4_fs_locations *fs_locations,
6704 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006705{
6706 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006707 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006708 struct nfs4_fs_locations_arg args = {
6709 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006710 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006711 .page = page,
6712 .bitmask = bitmask,
6713 };
Benny Halevy22958462009-04-01 09:22:02 -04006714 struct nfs4_fs_locations_res res = {
6715 .fs_locations = fs_locations,
6716 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006717 struct rpc_message msg = {
6718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6719 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006720 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006721 };
6722 int status;
6723
Harvey Harrison3110ff82008-05-02 13:42:44 -07006724 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006725
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006726 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6727 bitmask[1] = nfs4_fattr_bitmap[1];
6728
Andy Adamson533eb462011-06-13 18:25:56 -04006729 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6730 * is not supported */
6731 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006732 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006733 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006734 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006735
Trond Myklebustc228fd32007-01-13 02:28:11 -05006736 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006737 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006738 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006739 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006740 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006741 return status;
6742}
6743
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006744int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6745 const struct qstr *name,
6746 struct nfs4_fs_locations *fs_locations,
6747 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006748{
6749 struct nfs4_exception exception = { };
6750 int err;
6751 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006752 err = _nfs4_proc_fs_locations(client, dir, name,
6753 fs_locations, page);
6754 trace_nfs4_get_fs_locations(dir, name, err);
6755 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006756 &exception);
6757 } while (exception.retry);
6758 return err;
6759}
6760
Chuck Leverb03d7352013-10-17 14:12:50 -04006761/*
6762 * This operation also signals the server that this client is
6763 * performing migration recovery. The server can stop returning
6764 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6765 * appended to this compound to identify the client ID which is
6766 * performing recovery.
6767 */
6768static int _nfs40_proc_get_locations(struct inode *inode,
6769 struct nfs4_fs_locations *locations,
6770 struct page *page, struct rpc_cred *cred)
6771{
6772 struct nfs_server *server = NFS_SERVER(inode);
6773 struct rpc_clnt *clnt = server->client;
6774 u32 bitmask[2] = {
6775 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6776 };
6777 struct nfs4_fs_locations_arg args = {
6778 .clientid = server->nfs_client->cl_clientid,
6779 .fh = NFS_FH(inode),
6780 .page = page,
6781 .bitmask = bitmask,
6782 .migration = 1, /* skip LOOKUP */
6783 .renew = 1, /* append RENEW */
6784 };
6785 struct nfs4_fs_locations_res res = {
6786 .fs_locations = locations,
6787 .migration = 1,
6788 .renew = 1,
6789 };
6790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6792 .rpc_argp = &args,
6793 .rpc_resp = &res,
6794 .rpc_cred = cred,
6795 };
6796 unsigned long now = jiffies;
6797 int status;
6798
6799 nfs_fattr_init(&locations->fattr);
6800 locations->server = server;
6801 locations->nlocations = 0;
6802
6803 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6804 nfs4_set_sequence_privileged(&args.seq_args);
6805 status = nfs4_call_sync_sequence(clnt, server, &msg,
6806 &args.seq_args, &res.seq_res);
6807 if (status)
6808 return status;
6809
6810 renew_lease(server, now);
6811 return 0;
6812}
6813
6814#ifdef CONFIG_NFS_V4_1
6815
6816/*
6817 * This operation also signals the server that this client is
6818 * performing migration recovery. The server can stop asserting
6819 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6820 * performing this operation is identified in the SEQUENCE
6821 * operation in this compound.
6822 *
6823 * When the client supports GETATTR(fs_locations_info), it can
6824 * be plumbed in here.
6825 */
6826static int _nfs41_proc_get_locations(struct inode *inode,
6827 struct nfs4_fs_locations *locations,
6828 struct page *page, struct rpc_cred *cred)
6829{
6830 struct nfs_server *server = NFS_SERVER(inode);
6831 struct rpc_clnt *clnt = server->client;
6832 u32 bitmask[2] = {
6833 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6834 };
6835 struct nfs4_fs_locations_arg args = {
6836 .fh = NFS_FH(inode),
6837 .page = page,
6838 .bitmask = bitmask,
6839 .migration = 1, /* skip LOOKUP */
6840 };
6841 struct nfs4_fs_locations_res res = {
6842 .fs_locations = locations,
6843 .migration = 1,
6844 };
6845 struct rpc_message msg = {
6846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6847 .rpc_argp = &args,
6848 .rpc_resp = &res,
6849 .rpc_cred = cred,
6850 };
6851 int status;
6852
6853 nfs_fattr_init(&locations->fattr);
6854 locations->server = server;
6855 locations->nlocations = 0;
6856
6857 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6858 nfs4_set_sequence_privileged(&args.seq_args);
6859 status = nfs4_call_sync_sequence(clnt, server, &msg,
6860 &args.seq_args, &res.seq_res);
6861 if (status == NFS4_OK &&
6862 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6863 status = -NFS4ERR_LEASE_MOVED;
6864 return status;
6865}
6866
6867#endif /* CONFIG_NFS_V4_1 */
6868
6869/**
6870 * nfs4_proc_get_locations - discover locations for a migrated FSID
6871 * @inode: inode on FSID that is migrating
6872 * @locations: result of query
6873 * @page: buffer
6874 * @cred: credential to use for this operation
6875 *
6876 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6877 * operation failed, or a negative errno if a local error occurred.
6878 *
6879 * On success, "locations" is filled in, but if the server has
6880 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6881 * asserted.
6882 *
6883 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6884 * from this client that require migration recovery.
6885 */
6886int nfs4_proc_get_locations(struct inode *inode,
6887 struct nfs4_fs_locations *locations,
6888 struct page *page, struct rpc_cred *cred)
6889{
6890 struct nfs_server *server = NFS_SERVER(inode);
6891 struct nfs_client *clp = server->nfs_client;
6892 const struct nfs4_mig_recovery_ops *ops =
6893 clp->cl_mvops->mig_recovery_ops;
6894 struct nfs4_exception exception = { };
6895 int status;
6896
6897 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6898 (unsigned long long)server->fsid.major,
6899 (unsigned long long)server->fsid.minor,
6900 clp->cl_hostname);
6901 nfs_display_fhandle(NFS_FH(inode), __func__);
6902
6903 do {
6904 status = ops->get_locations(inode, locations, page, cred);
6905 if (status != -NFS4ERR_DELAY)
6906 break;
6907 nfs4_handle_exception(server, status, &exception);
6908 } while (exception.retry);
6909 return status;
6910}
6911
Chuck Lever44c99932013-10-17 14:13:30 -04006912/*
6913 * This operation also signals the server that this client is
6914 * performing "lease moved" recovery. The server can stop
6915 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6916 * is appended to this compound to identify the client ID which is
6917 * performing recovery.
6918 */
6919static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6920{
6921 struct nfs_server *server = NFS_SERVER(inode);
6922 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6923 struct rpc_clnt *clnt = server->client;
6924 struct nfs4_fsid_present_arg args = {
6925 .fh = NFS_FH(inode),
6926 .clientid = clp->cl_clientid,
6927 .renew = 1, /* append RENEW */
6928 };
6929 struct nfs4_fsid_present_res res = {
6930 .renew = 1,
6931 };
6932 struct rpc_message msg = {
6933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6934 .rpc_argp = &args,
6935 .rpc_resp = &res,
6936 .rpc_cred = cred,
6937 };
6938 unsigned long now = jiffies;
6939 int status;
6940
6941 res.fh = nfs_alloc_fhandle();
6942 if (res.fh == NULL)
6943 return -ENOMEM;
6944
6945 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6946 nfs4_set_sequence_privileged(&args.seq_args);
6947 status = nfs4_call_sync_sequence(clnt, server, &msg,
6948 &args.seq_args, &res.seq_res);
6949 nfs_free_fhandle(res.fh);
6950 if (status)
6951 return status;
6952
6953 do_renew_lease(clp, now);
6954 return 0;
6955}
6956
6957#ifdef CONFIG_NFS_V4_1
6958
6959/*
6960 * This operation also signals the server that this client is
6961 * performing "lease moved" recovery. The server can stop asserting
6962 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6963 * this operation is identified in the SEQUENCE operation in this
6964 * compound.
6965 */
6966static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6967{
6968 struct nfs_server *server = NFS_SERVER(inode);
6969 struct rpc_clnt *clnt = server->client;
6970 struct nfs4_fsid_present_arg args = {
6971 .fh = NFS_FH(inode),
6972 };
6973 struct nfs4_fsid_present_res res = {
6974 };
6975 struct rpc_message msg = {
6976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6977 .rpc_argp = &args,
6978 .rpc_resp = &res,
6979 .rpc_cred = cred,
6980 };
6981 int status;
6982
6983 res.fh = nfs_alloc_fhandle();
6984 if (res.fh == NULL)
6985 return -ENOMEM;
6986
6987 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6988 nfs4_set_sequence_privileged(&args.seq_args);
6989 status = nfs4_call_sync_sequence(clnt, server, &msg,
6990 &args.seq_args, &res.seq_res);
6991 nfs_free_fhandle(res.fh);
6992 if (status == NFS4_OK &&
6993 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6994 status = -NFS4ERR_LEASE_MOVED;
6995 return status;
6996}
6997
6998#endif /* CONFIG_NFS_V4_1 */
6999
7000/**
7001 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7002 * @inode: inode on FSID to check
7003 * @cred: credential to use for this operation
7004 *
7005 * Server indicates whether the FSID is present, moved, or not
7006 * recognized. This operation is necessary to clear a LEASE_MOVED
7007 * condition for this client ID.
7008 *
7009 * Returns NFS4_OK if the FSID is present on this server,
7010 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7011 * NFS4ERR code if some error occurred on the server, or a
7012 * negative errno if a local failure occurred.
7013 */
7014int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7015{
7016 struct nfs_server *server = NFS_SERVER(inode);
7017 struct nfs_client *clp = server->nfs_client;
7018 const struct nfs4_mig_recovery_ops *ops =
7019 clp->cl_mvops->mig_recovery_ops;
7020 struct nfs4_exception exception = { };
7021 int status;
7022
7023 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7024 (unsigned long long)server->fsid.major,
7025 (unsigned long long)server->fsid.minor,
7026 clp->cl_hostname);
7027 nfs_display_fhandle(NFS_FH(inode), __func__);
7028
7029 do {
7030 status = ops->fsid_present(inode, cred);
7031 if (status != -NFS4ERR_DELAY)
7032 break;
7033 nfs4_handle_exception(server, status, &exception);
7034 } while (exception.retry);
7035 return status;
7036}
7037
Andy Adamson5ec16a82013-08-08 10:57:55 -04007038/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007039 * If 'use_integrity' is true and the state managment nfs_client
7040 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7041 * and the machine credential as per RFC3530bis and RFC5661 Security
7042 * Considerations sections. Otherwise, just use the user cred with the
7043 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007044 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007045static 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 +00007046{
7047 int status;
7048 struct nfs4_secinfo_arg args = {
7049 .dir_fh = NFS_FH(dir),
7050 .name = name,
7051 };
7052 struct nfs4_secinfo_res res = {
7053 .flavors = flavors,
7054 };
7055 struct rpc_message msg = {
7056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7057 .rpc_argp = &args,
7058 .rpc_resp = &res,
7059 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007060 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007061 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007062
7063 if (use_integrity) {
7064 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007065 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7066 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007067 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007068
7069 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007070
7071 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7072 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7073
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007074 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7075 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007076 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007077
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007078 if (cred)
7079 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007080
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007081 return status;
7082}
7083
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007084int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7085 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007086{
7087 struct nfs4_exception exception = { };
7088 int err;
7089 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007090 err = -NFS4ERR_WRONGSEC;
7091
7092 /* try to use integrity protection with machine cred */
7093 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7094 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7095
7096 /*
7097 * if unable to use integrity protection, or SECINFO with
7098 * integrity protection returns NFS4ERR_WRONGSEC (which is
7099 * disallowed by spec, but exists in deployed servers) use
7100 * the current filesystem's rpc_client and the user cred.
7101 */
7102 if (err == -NFS4ERR_WRONGSEC)
7103 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7104
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007105 trace_nfs4_secinfo(dir, name, err);
7106 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007107 &exception);
7108 } while (exception.retry);
7109 return err;
7110}
7111
Andy Adamson557134a2009-04-01 09:21:53 -04007112#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007113/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007114 * Check the exchange flags returned by the server for invalid flags, having
7115 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7116 * DS flags set.
7117 */
7118static int nfs4_check_cl_exchange_flags(u32 flags)
7119{
7120 if (flags & ~EXCHGID4_FLAG_MASK_R)
7121 goto out_inval;
7122 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7123 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7124 goto out_inval;
7125 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7126 goto out_inval;
7127 return NFS_OK;
7128out_inval:
7129 return -NFS4ERR_INVAL;
7130}
7131
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007132static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007133nfs41_same_server_scope(struct nfs41_server_scope *a,
7134 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007135{
7136 if (a->server_scope_sz == b->server_scope_sz &&
7137 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7138 return true;
7139
7140 return false;
7141}
7142
Andy Adamson02a95de2016-02-05 16:08:37 -05007143static void
7144nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7145{
7146}
7147
7148static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7149 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7150};
7151
Andy Adamson357f54d2010-12-14 10:11:57 -05007152/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007153 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007154 *
7155 * The 4.1 client currently uses the same TCP connection for the
7156 * fore and backchannel.
7157 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007158static
7159int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7160 struct rpc_xprt *xprt,
7161 struct nfs_client *clp,
7162 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007163{
7164 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007165 struct nfs41_bind_conn_to_session_args args = {
7166 .client = clp,
7167 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7168 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007169 struct nfs41_bind_conn_to_session_res res;
7170 struct rpc_message msg = {
7171 .rpc_proc =
7172 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007173 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007174 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007175 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007176 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007177 struct rpc_task_setup task_setup_data = {
7178 .rpc_client = clnt,
7179 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007180 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007181 .rpc_message = &msg,
7182 .flags = RPC_TASK_TIMEOUT,
7183 };
7184 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007185
7186 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007187
Trond Myklebust71a097c2015-02-18 09:27:18 -08007188 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7189 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7190 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007191
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007192 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7193 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7194 args.dir = NFS4_CDFC4_FORE;
7195
7196 task = rpc_run_task(&task_setup_data);
7197 if (!IS_ERR(task)) {
7198 status = task->tk_status;
7199 rpc_put_task(task);
7200 } else
7201 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007202 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007203 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007204 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007205 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7206 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7207 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007208 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007209 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007210 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007211 dprintk("NFS: %s: Unexpected direction from server\n",
7212 __func__);
7213 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007214 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007215 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007216 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007217 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7218 __func__);
7219 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007220 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007221 }
7222 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007223out:
7224 dprintk("<-- %s status= %d\n", __func__, status);
7225 return status;
7226}
7227
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007228struct rpc_bind_conn_calldata {
7229 struct nfs_client *clp;
7230 struct rpc_cred *cred;
7231};
7232
7233static int
7234nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7235 struct rpc_xprt *xprt,
7236 void *calldata)
7237{
7238 struct rpc_bind_conn_calldata *p = calldata;
7239
7240 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7241}
7242
7243int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7244{
7245 struct rpc_bind_conn_calldata data = {
7246 .clp = clp,
7247 .cred = cred,
7248 };
7249 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7250 nfs4_proc_bind_conn_to_session_callback, &data);
7251}
7252
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007253/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007254 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7255 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007256 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007257static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7258 .how = SP4_MACH_CRED,
7259 .enforce.u.words = {
7260 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7261 1 << (OP_EXCHANGE_ID - 32) |
7262 1 << (OP_CREATE_SESSION - 32) |
7263 1 << (OP_DESTROY_SESSION - 32) |
7264 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007265 },
7266 .allow.u.words = {
7267 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007268 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007269 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007270 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007271 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007272 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007273 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007274 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007275 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007276 1 << (OP_FREE_STATEID - 32) |
7277 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007278 }
7279};
7280
7281/*
7282 * Select the state protection mode for client `clp' given the server results
7283 * from exchange_id in `sp'.
7284 *
7285 * Returns 0 on success, negative errno otherwise.
7286 */
7287static int nfs4_sp4_select_mode(struct nfs_client *clp,
7288 struct nfs41_state_protection *sp)
7289{
7290 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7291 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7292 1 << (OP_EXCHANGE_ID - 32) |
7293 1 << (OP_CREATE_SESSION - 32) |
7294 1 << (OP_DESTROY_SESSION - 32) |
7295 1 << (OP_DESTROY_CLIENTID - 32)
7296 };
7297 unsigned int i;
7298
7299 if (sp->how == SP4_MACH_CRED) {
7300 /* Print state protect result */
7301 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7302 for (i = 0; i <= LAST_NFS4_OP; i++) {
7303 if (test_bit(i, sp->enforce.u.longs))
7304 dfprintk(MOUNT, " enforce op %d\n", i);
7305 if (test_bit(i, sp->allow.u.longs))
7306 dfprintk(MOUNT, " allow op %d\n", i);
7307 }
7308
7309 /* make sure nothing is on enforce list that isn't supported */
7310 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7311 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7312 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7313 return -EINVAL;
7314 }
7315 }
7316
7317 /*
7318 * Minimal mode - state operations are allowed to use machine
7319 * credential. Note this already happens by default, so the
7320 * client doesn't have to do anything more than the negotiation.
7321 *
7322 * NOTE: we don't care if EXCHANGE_ID is in the list -
7323 * we're already using the machine cred for exchange_id
7324 * and will never use a different cred.
7325 */
7326 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7327 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7328 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7329 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7330 dfprintk(MOUNT, "sp4_mach_cred:\n");
7331 dfprintk(MOUNT, " minimal mode enabled\n");
7332 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7333 } else {
7334 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7335 return -EINVAL;
7336 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007337
7338 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007339 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7340 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007341 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7342 dfprintk(MOUNT, " cleanup mode enabled\n");
7343 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7344 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007345
Andrew Elble99ade3c2015-12-02 09:39:51 -05007346 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7347 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7348 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7349 &clp->cl_sp4_flags);
7350 }
7351
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007352 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7353 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7354 dfprintk(MOUNT, " secinfo mode enabled\n");
7355 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7356 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007357
7358 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7359 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7360 dfprintk(MOUNT, " stateid mode enabled\n");
7361 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7362 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007363
7364 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7365 dfprintk(MOUNT, " write mode enabled\n");
7366 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7367 }
7368
7369 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7370 dfprintk(MOUNT, " commit mode enabled\n");
7371 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7372 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007373 }
7374
7375 return 0;
7376}
7377
Andy Adamson8d89bd72016-09-09 09:22:18 -04007378struct nfs41_exchange_id_data {
7379 struct nfs41_exchange_id_res res;
7380 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007381 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007382 int rpc_status;
7383};
7384
7385static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007386{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007387 struct nfs41_exchange_id_data *cdata =
7388 (struct nfs41_exchange_id_data *)data;
7389 struct nfs_client *clp = cdata->args.client;
7390 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007391
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007392 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007393
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007394 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007395 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007396
7397 if (cdata->xprt && status == 0) {
7398 status = nfs4_detect_session_trunking(clp, &cdata->res,
7399 cdata->xprt);
7400 goto out;
7401 }
7402
Andy Adamson8d89bd72016-09-09 09:22:18 -04007403 if (status == 0)
7404 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007405
Chuck Lever177313f2012-05-21 22:44:58 -04007406 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007407 clp->cl_clientid = cdata->res.clientid;
7408 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007409 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007410 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007411 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007412 &clp->cl_session->session_state);
7413 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007414 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007415
Chuck Leveracdeb692012-05-21 22:46:16 -04007416 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007417 clp->cl_serverowner = cdata->res.server_owner;
7418 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007419
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007420 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007421 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007422 clp->cl_implid = cdata->res.impl_id;
7423 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007424
Chuck Lever177313f2012-05-21 22:44:58 -04007425 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007426 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007427 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007428 dprintk("%s: server_scope mismatch detected\n",
7429 __func__);
7430 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007431 kfree(clp->cl_serverscope);
7432 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007433 }
7434
Chuck Lever177313f2012-05-21 22:44:58 -04007435 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007436 clp->cl_serverscope = cdata->res.server_scope;
7437 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007438 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007439 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007440 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007441 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007442 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007443out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007444 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007445 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446}
7447
7448static void nfs4_exchange_id_release(void *data)
7449{
7450 struct nfs41_exchange_id_data *cdata =
7451 (struct nfs41_exchange_id_data *)data;
7452
Andy Adamsonad0849a2016-09-09 09:22:28 -04007453 if (cdata->xprt) {
7454 xprt_put(cdata->xprt);
7455 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7456 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007457 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007458 kfree(cdata->res.impl_id);
7459 kfree(cdata->res.server_scope);
7460 kfree(cdata->res.server_owner);
7461 kfree(cdata);
7462}
7463
7464static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7465 .rpc_call_done = nfs4_exchange_id_done,
7466 .rpc_release = nfs4_exchange_id_release,
7467};
7468
Benny Halevy99fe60d2009-04-01 09:22:29 -04007469/*
7470 * _nfs4_proc_exchange_id()
7471 *
7472 * Wrapper for EXCHANGE_ID operation.
7473 */
7474static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007475 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007476{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007477 struct rpc_message msg = {
7478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007479 .rpc_cred = cred,
7480 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 struct rpc_task_setup task_setup_data = {
7482 .rpc_client = clp->cl_rpcclient,
7483 .callback_ops = &nfs4_exchange_id_call_ops,
7484 .rpc_message = &msg,
7485 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7486 };
7487 struct nfs41_exchange_id_data *calldata;
7488 struct rpc_task *task;
7489 int status = -EIO;
7490
7491 if (!atomic_inc_not_zero(&clp->cl_count))
7492 goto out;
7493
7494 status = -ENOMEM;
7495 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7496 if (!calldata)
7497 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007498
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007499 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007500
7501 status = nfs4_init_uniform_client_string(clp);
7502 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007503 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007504
7505 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7506 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7507 clp->cl_owner_id);
7508
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7510 GFP_NOFS);
7511 status = -ENOMEM;
7512 if (unlikely(calldata->res.server_owner == NULL))
7513 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007514
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007516 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007517 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007518 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007519
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7521 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007522 goto out_server_scope;
7523
7524 switch (sp4_how) {
7525 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007527 break;
7528
7529 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007530 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007531 break;
7532
7533 default:
7534 /* unsupported! */
7535 WARN_ON_ONCE(1);
7536 status = -EINVAL;
7537 goto out_impl_id;
7538 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007539 if (xprt) {
7540 calldata->xprt = xprt;
7541 task_setup_data.rpc_xprt = xprt;
7542 task_setup_data.flags =
7543 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007544 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7545 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007546 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007547 calldata->args.client = clp;
7548#ifdef CONFIG_NFS_V4_1_MIGRATION
7549 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7550 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7551 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7552#else
7553 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7554 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7555#endif
7556 msg.rpc_argp = &calldata->args;
7557 msg.rpc_resp = &calldata->res;
7558 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007559
Andy Adamson8d89bd72016-09-09 09:22:18 -04007560 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007561 if (IS_ERR(task))
7562 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007563
Andy Adamsonad0849a2016-09-09 09:22:28 -04007564 if (!xprt) {
7565 status = rpc_wait_for_completion_task(task);
7566 if (!status)
7567 status = calldata->rpc_status;
7568 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007569 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007570
Andy Adamson8d89bd72016-09-09 09:22:18 -04007571 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007572out:
Chuck Lever177313f2012-05-21 22:44:58 -04007573 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007574 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007575 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007576 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007577 clp->cl_implid->date.seconds,
7578 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007579 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007580 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007581
7582out_impl_id:
7583 kfree(calldata->res.impl_id);
7584out_server_scope:
7585 kfree(calldata->res.server_scope);
7586out_server_owner:
7587 kfree(calldata->res.server_owner);
7588out_calldata:
7589 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007590 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007591 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007592}
7593
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007594/*
7595 * nfs4_proc_exchange_id()
7596 *
7597 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7598 *
7599 * Since the clientid has expired, all compounds using sessions
7600 * associated with the stale clientid will be returning
7601 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7602 * be in some phase of session reset.
7603 *
7604 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7605 */
7606int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7607{
7608 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7609 int status;
7610
7611 /* try SP4_MACH_CRED if krb5i/p */
7612 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7613 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007614 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007615 if (!status)
7616 return 0;
7617 }
7618
7619 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007620 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007621}
7622
Andy Adamson04fa2c62016-09-09 09:22:29 -04007623/**
7624 * nfs4_test_session_trunk
7625 *
7626 * This is an add_xprt_test() test function called from
7627 * rpc_clnt_setup_test_and_add_xprt.
7628 *
7629 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7630 * and is dereferrenced in nfs4_exchange_id_release
7631 *
7632 * Upon success, add the new transport to the rpc_clnt
7633 *
7634 * @clnt: struct rpc_clnt to get new transport
7635 * @xprt: the rpc_xprt to test
7636 * @data: call data for _nfs4_proc_exchange_id.
7637 */
7638int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7639 void *data)
7640{
7641 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7642 u32 sp4_how;
7643
7644 dprintk("--> %s try %s\n", __func__,
7645 xprt->address_strings[RPC_DISPLAY_ADDR]);
7646
7647 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7648
7649 /* Test connection for session trunking. Async exchange_id call */
7650 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7651}
7652EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7653
Trond Myklebust66245532012-05-25 17:18:09 -04007654static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7655 struct rpc_cred *cred)
7656{
7657 struct rpc_message msg = {
7658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7659 .rpc_argp = clp,
7660 .rpc_cred = cred,
7661 };
7662 int status;
7663
7664 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007665 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007666 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007667 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007668 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7669 return status;
7670}
7671
7672static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7673 struct rpc_cred *cred)
7674{
7675 unsigned int loop;
7676 int ret;
7677
7678 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7679 ret = _nfs4_proc_destroy_clientid(clp, cred);
7680 switch (ret) {
7681 case -NFS4ERR_DELAY:
7682 case -NFS4ERR_CLIENTID_BUSY:
7683 ssleep(1);
7684 break;
7685 default:
7686 return ret;
7687 }
7688 }
7689 return 0;
7690}
7691
7692int nfs4_destroy_clientid(struct nfs_client *clp)
7693{
7694 struct rpc_cred *cred;
7695 int ret = 0;
7696
7697 if (clp->cl_mvops->minor_version < 1)
7698 goto out;
7699 if (clp->cl_exchange_flags == 0)
7700 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007701 if (clp->cl_preserve_clid)
7702 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007703 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007704 ret = nfs4_proc_destroy_clientid(clp, cred);
7705 if (cred)
7706 put_rpccred(cred);
7707 switch (ret) {
7708 case 0:
7709 case -NFS4ERR_STALE_CLIENTID:
7710 clp->cl_exchange_flags = 0;
7711 }
7712out:
7713 return ret;
7714}
7715
Andy Adamson2050f0c2009-04-01 09:22:30 -04007716struct nfs4_get_lease_time_data {
7717 struct nfs4_get_lease_time_args *args;
7718 struct nfs4_get_lease_time_res *res;
7719 struct nfs_client *clp;
7720};
7721
7722static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7723 void *calldata)
7724{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007725 struct nfs4_get_lease_time_data *data =
7726 (struct nfs4_get_lease_time_data *)calldata;
7727
7728 dprintk("--> %s\n", __func__);
7729 /* just setup sequence, do not trigger session recovery
7730 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007731 nfs41_setup_sequence(data->clp->cl_session,
7732 &data->args->la_seq_args,
7733 &data->res->lr_seq_res,
7734 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007735 dprintk("<-- %s\n", __func__);
7736}
7737
7738/*
7739 * Called from nfs4_state_manager thread for session setup, so don't recover
7740 * from sequence operation or clientid errors.
7741 */
7742static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7743{
7744 struct nfs4_get_lease_time_data *data =
7745 (struct nfs4_get_lease_time_data *)calldata;
7746
7747 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007748 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7749 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007750 switch (task->tk_status) {
7751 case -NFS4ERR_DELAY:
7752 case -NFS4ERR_GRACE:
7753 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7754 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7755 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007756 /* fall through */
7757 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007758 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007759 return;
7760 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007761 dprintk("<-- %s\n", __func__);
7762}
7763
Trond Myklebust17280172012-03-11 13:11:00 -04007764static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007765 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7766 .rpc_call_done = nfs4_get_lease_time_done,
7767};
7768
7769int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7770{
7771 struct rpc_task *task;
7772 struct nfs4_get_lease_time_args args;
7773 struct nfs4_get_lease_time_res res = {
7774 .lr_fsinfo = fsinfo,
7775 };
7776 struct nfs4_get_lease_time_data data = {
7777 .args = &args,
7778 .res = &res,
7779 .clp = clp,
7780 };
7781 struct rpc_message msg = {
7782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7783 .rpc_argp = &args,
7784 .rpc_resp = &res,
7785 };
7786 struct rpc_task_setup task_setup = {
7787 .rpc_client = clp->cl_rpcclient,
7788 .rpc_message = &msg,
7789 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007790 .callback_data = &data,
7791 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007792 };
7793 int status;
7794
Chuck Levera9c92d62013-08-09 12:48:18 -04007795 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007796 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007797 dprintk("--> %s\n", __func__);
7798 task = rpc_run_task(&task_setup);
7799
7800 if (IS_ERR(task))
7801 status = PTR_ERR(task);
7802 else {
7803 status = task->tk_status;
7804 rpc_put_task(task);
7805 }
7806 dprintk("<-- %s return %d\n", __func__, status);
7807
7808 return status;
7809}
7810
Andy Adamsonfc931582009-04-01 09:22:31 -04007811/*
7812 * Initialize the values to be used by the client in CREATE_SESSION
7813 * If nfs4_init_session set the fore channel request and response sizes,
7814 * use them.
7815 *
7816 * Set the back channel max_resp_sz_cached to zero to force the client to
7817 * always set csa_cachethis to FALSE because the current implementation
7818 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7819 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007820static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7821 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007822{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007823 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007824 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007825
Andy Adamson18aad3d2013-06-26 12:21:49 -04007826 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7827 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7828
Andy Adamsonfc931582009-04-01 09:22:31 -04007829 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007830 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7831 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007832 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007833 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007834
7835 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007836 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007837 __func__,
7838 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007839 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007840
7841 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007842 args->bc_attrs.max_rqst_sz = max_bc_payload;
7843 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007844 args->bc_attrs.max_resp_sz_cached = 0;
7845 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007846 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007847
7848 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7849 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7850 __func__,
7851 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7852 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7853 args->bc_attrs.max_reqs);
7854}
7855
Trond Myklebust79969dd2015-02-18 11:30:18 -08007856static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7857 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007858{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007859 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007860 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007861
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007862 if (rcvd->max_resp_sz > sent->max_resp_sz)
7863 return -EINVAL;
7864 /*
7865 * Our requested max_ops is the minimum we need; we're not
7866 * prepared to break up compounds into smaller pieces than that.
7867 * So, no point even trying to continue if the server won't
7868 * cooperate:
7869 */
7870 if (rcvd->max_ops < sent->max_ops)
7871 return -EINVAL;
7872 if (rcvd->max_reqs == 0)
7873 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007874 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7875 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007876 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007877}
7878
Trond Myklebust79969dd2015-02-18 11:30:18 -08007879static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7880 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007881{
7882 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007883 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007884
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007885 if (!(res->flags & SESSION4_BACK_CHAN))
7886 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007887 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7888 return -EINVAL;
7889 if (rcvd->max_resp_sz < sent->max_resp_sz)
7890 return -EINVAL;
7891 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7892 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007893 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007894 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007895 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007896 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007897out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007898 return 0;
7899}
Andy Adamson8d353012009-04-01 09:22:32 -04007900
Andy Adamson8d353012009-04-01 09:22:32 -04007901static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007902 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007903{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007904 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007905
Trond Myklebust79969dd2015-02-18 11:30:18 -08007906 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007907 if (ret)
7908 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007909 return nfs4_verify_back_channel_attrs(args, res);
7910}
7911
7912static void nfs4_update_session(struct nfs4_session *session,
7913 struct nfs41_create_session_res *res)
7914{
7915 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007916 /* Mark client id and session as being confirmed */
7917 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7918 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007919 session->flags = res->flags;
7920 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007921 if (res->flags & SESSION4_BACK_CHAN)
7922 memcpy(&session->bc_attrs, &res->bc_attrs,
7923 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007924}
7925
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007926static int _nfs4_proc_create_session(struct nfs_client *clp,
7927 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007928{
7929 struct nfs4_session *session = clp->cl_session;
7930 struct nfs41_create_session_args args = {
7931 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007932 .clientid = clp->cl_clientid,
7933 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007934 .cb_program = NFS4_CALLBACK,
7935 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007936 struct nfs41_create_session_res res;
7937
Andy Adamsonfc931582009-04-01 09:22:31 -04007938 struct rpc_message msg = {
7939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7940 .rpc_argp = &args,
7941 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007942 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007943 };
7944 int status;
7945
Chuck Lever6b26cc82016-05-02 14:40:40 -04007946 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007947 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007948
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007949 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007950 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007951
Trond Myklebustb519d402016-09-11 14:50:01 -04007952 switch (status) {
7953 case -NFS4ERR_STALE_CLIENTID:
7954 case -NFS4ERR_DELAY:
7955 case -ETIMEDOUT:
7956 case -EACCES:
7957 case -EAGAIN:
7958 goto out;
7959 };
7960
7961 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007962 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007963 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007964 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007965 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007966 if (status)
7967 goto out;
7968 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007969 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007970out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007971 return status;
7972}
7973
7974/*
7975 * Issues a CREATE_SESSION operation to the server.
7976 * It is the responsibility of the caller to verify the session is
7977 * expired before calling this routine.
7978 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007979int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007980{
7981 int status;
7982 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007983 struct nfs4_session *session = clp->cl_session;
7984
7985 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7986
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007987 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007988 if (status)
7989 goto out;
7990
Andy Adamsonaacd5532011-11-09 13:58:21 -05007991 /* Init or reset the session slot tables */
7992 status = nfs4_setup_session_slot_tables(session);
7993 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007994 if (status)
7995 goto out;
7996
7997 ptr = (unsigned *)&session->sess_id.data[0];
7998 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7999 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008000out:
8001 dprintk("<-- %s\n", __func__);
8002 return status;
8003}
8004
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008005/*
8006 * Issue the over-the-wire RPC DESTROY_SESSION.
8007 * The caller must serialize access to this routine.
8008 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008009int nfs4_proc_destroy_session(struct nfs4_session *session,
8010 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008011{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008012 struct rpc_message msg = {
8013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8014 .rpc_argp = session,
8015 .rpc_cred = cred,
8016 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008017 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008018
8019 dprintk("--> nfs4_proc_destroy_session\n");
8020
8021 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008022 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8023 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008024
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008025 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008026 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008027
8028 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008029 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008030 "Session has been destroyed regardless...\n", status);
8031
8032 dprintk("<-- nfs4_proc_destroy_session\n");
8033 return status;
8034}
8035
Trond Myklebust7b38c362012-05-23 13:23:31 -04008036/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008037 * Renew the cl_session lease.
8038 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008039struct nfs4_sequence_data {
8040 struct nfs_client *clp;
8041 struct nfs4_sequence_args args;
8042 struct nfs4_sequence_res res;
8043};
8044
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008045static void nfs41_sequence_release(void *data)
8046{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008047 struct nfs4_sequence_data *calldata = data;
8048 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008049
Alexandros Batsakis71358402010-02-05 03:45:05 -08008050 if (atomic_read(&clp->cl_count) > 1)
8051 nfs4_schedule_state_renewal(clp);
8052 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008053 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008054}
8055
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008056static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8057{
8058 switch(task->tk_status) {
8059 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008060 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8061 return -EAGAIN;
8062 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008063 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008064 }
8065 return 0;
8066}
8067
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008068static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008069{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008070 struct nfs4_sequence_data *calldata = data;
8071 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008072
Trond Myklebust14516c32010-07-31 14:29:06 -04008073 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8074 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008075
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008076 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008077 if (task->tk_status < 0) {
8078 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008079 if (atomic_read(&clp->cl_count) == 1)
8080 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008081
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008082 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8083 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084 return;
8085 }
8086 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008087 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008088out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008089 dprintk("<-- %s\n", __func__);
8090}
8091
8092static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8093{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008094 struct nfs4_sequence_data *calldata = data;
8095 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008096 struct nfs4_sequence_args *args;
8097 struct nfs4_sequence_res *res;
8098
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008099 args = task->tk_msg.rpc_argp;
8100 res = task->tk_msg.rpc_resp;
8101
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008102 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008103}
8104
8105static const struct rpc_call_ops nfs41_sequence_ops = {
8106 .rpc_call_done = nfs41_sequence_call_done,
8107 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008108 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008109};
8110
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008111static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8112 struct rpc_cred *cred,
8113 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008114{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008115 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008116 struct rpc_message msg = {
8117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8118 .rpc_cred = cred,
8119 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008120 struct rpc_task_setup task_setup_data = {
8121 .rpc_client = clp->cl_rpcclient,
8122 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008123 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008124 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008125 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008126
Alexandros Batsakis71358402010-02-05 03:45:05 -08008127 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008128 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008129 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008130 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008131 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008132 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008133 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008134 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008135 if (is_privileged)
8136 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008137 msg.rpc_argp = &calldata->args;
8138 msg.rpc_resp = &calldata->res;
8139 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008140 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008141
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008142 return rpc_run_task(&task_setup_data);
8143}
8144
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008145static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008146{
8147 struct rpc_task *task;
8148 int ret = 0;
8149
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008150 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008151 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008152 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008153 if (IS_ERR(task))
8154 ret = PTR_ERR(task);
8155 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008156 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008157 dprintk("<-- %s status=%d\n", __func__, ret);
8158 return ret;
8159}
8160
8161static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8162{
8163 struct rpc_task *task;
8164 int ret;
8165
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008166 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008167 if (IS_ERR(task)) {
8168 ret = PTR_ERR(task);
8169 goto out;
8170 }
8171 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008172 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008173 ret = task->tk_status;
8174 rpc_put_task(task);
8175out:
8176 dprintk("<-- %s status=%d\n", __func__, ret);
8177 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008178}
8179
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008180struct nfs4_reclaim_complete_data {
8181 struct nfs_client *clp;
8182 struct nfs41_reclaim_complete_args arg;
8183 struct nfs41_reclaim_complete_res res;
8184};
8185
8186static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8187{
8188 struct nfs4_reclaim_complete_data *calldata = data;
8189
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008190 nfs41_setup_sequence(calldata->clp->cl_session,
8191 &calldata->arg.seq_args,
8192 &calldata->res.seq_res,
8193 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008194}
8195
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008196static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8197{
8198 switch(task->tk_status) {
8199 case 0:
8200 case -NFS4ERR_COMPLETE_ALREADY:
8201 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8202 break;
8203 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008204 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008205 /* fall through */
8206 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008207 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008208 case -NFS4ERR_BADSESSION:
8209 case -NFS4ERR_DEADSESSION:
8210 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8211 nfs4_schedule_session_recovery(clp->cl_session,
8212 task->tk_status);
8213 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008214 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008215 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008216 }
8217 return 0;
8218}
8219
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008220static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8221{
8222 struct nfs4_reclaim_complete_data *calldata = data;
8223 struct nfs_client *clp = calldata->clp;
8224 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8225
8226 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008227 if (!nfs41_sequence_done(task, res))
8228 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008229
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008230 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008231 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8232 rpc_restart_call_prepare(task);
8233 return;
8234 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008235 dprintk("<-- %s\n", __func__);
8236}
8237
8238static void nfs4_free_reclaim_complete_data(void *data)
8239{
8240 struct nfs4_reclaim_complete_data *calldata = data;
8241
8242 kfree(calldata);
8243}
8244
8245static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8246 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8247 .rpc_call_done = nfs4_reclaim_complete_done,
8248 .rpc_release = nfs4_free_reclaim_complete_data,
8249};
8250
8251/*
8252 * Issue a global reclaim complete.
8253 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008254static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8255 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008256{
8257 struct nfs4_reclaim_complete_data *calldata;
8258 struct rpc_task *task;
8259 struct rpc_message msg = {
8260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008261 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008262 };
8263 struct rpc_task_setup task_setup_data = {
8264 .rpc_client = clp->cl_rpcclient,
8265 .rpc_message = &msg,
8266 .callback_ops = &nfs4_reclaim_complete_call_ops,
8267 .flags = RPC_TASK_ASYNC,
8268 };
8269 int status = -ENOMEM;
8270
8271 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008272 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008273 if (calldata == NULL)
8274 goto out;
8275 calldata->clp = clp;
8276 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008277
Chuck Levera9c92d62013-08-09 12:48:18 -04008278 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008279 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008280 msg.rpc_argp = &calldata->arg;
8281 msg.rpc_resp = &calldata->res;
8282 task_setup_data.callback_data = calldata;
8283 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008284 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008285 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008286 goto out;
8287 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008288 status = nfs4_wait_for_completion_rpc_task(task);
8289 if (status == 0)
8290 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008291 rpc_put_task(task);
8292out:
8293 dprintk("<-- %s status=%d\n", __func__, status);
8294 return status;
8295}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008296
8297static void
8298nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8299{
8300 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008301 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008302 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008303
8304 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008305 nfs41_setup_sequence(session, &lgp->args.seq_args,
8306 &lgp->res.seq_res, task);
8307 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008308}
8309
8310static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8311{
8312 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008313
8314 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008315 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008316 dprintk("<-- %s\n", __func__);
8317}
8318
8319static int
8320nfs4_layoutget_handle_exception(struct rpc_task *task,
8321 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8322{
Trond Myklebustee314c22012-10-01 17:25:48 -07008323 struct inode *inode = lgp->args.inode;
8324 struct nfs_server *server = NFS_SERVER(inode);
8325 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008326 int nfs4err = task->tk_status;
8327 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008328 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008329
Boaz Harroshed7e5422014-01-22 20:34:54 +02008330 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008331
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008332 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008333 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008334 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008335
8336 /*
8337 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8338 * on the file. set tk_status to -ENODATA to tell upper layer to
8339 * retry go inband.
8340 */
8341 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008342 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008343 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008344 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008345 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8346 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8347 */
8348 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008349 status = -EOVERFLOW;
8350 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008351 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008352 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008353 * (or clients) writing to the same RAID stripe except when
8354 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008355 *
8356 * Treat it like we would RECALLCONFLICT -- we retry for a little
8357 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008358 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008359 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008360 if (lgp->args.minlength == 0) {
8361 status = -EOVERFLOW;
8362 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008363 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008364 status = -EBUSY;
8365 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008366 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008367 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008368 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008369 case -NFS4ERR_DELEG_REVOKED:
8370 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008371 case -NFS4ERR_EXPIRED:
8372 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008373 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008374 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008375 lo = NFS_I(inode)->layout;
8376 /* If the open stateid was bad, then recover it. */
8377 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8378 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008379 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008380 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008381 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008382 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008383 break;
8384 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008385
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008386 /*
8387 * Mark the bad layout state as invalid, then retry
8388 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008389 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008390 spin_unlock(&inode->i_lock);
8391 pnfs_free_lseg_list(&head);
8392 status = -EAGAIN;
8393 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008394 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008395
Trond Myklebust0a702352017-01-23 22:44:12 -05008396 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008397 err = nfs4_handle_exception(server, nfs4err, exception);
8398 if (!status) {
8399 if (exception->retry)
8400 status = -EAGAIN;
8401 else
8402 status = err;
8403 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008404out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008405 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008406 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008407}
8408
Idan Kedar85541162012-08-02 11:47:10 +03008409static size_t max_response_pages(struct nfs_server *server)
8410{
8411 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8412 return nfs_page_array_len(0, max_resp_sz);
8413}
8414
8415static void nfs4_free_pages(struct page **pages, size_t size)
8416{
8417 int i;
8418
8419 if (!pages)
8420 return;
8421
8422 for (i = 0; i < size; i++) {
8423 if (!pages[i])
8424 break;
8425 __free_page(pages[i]);
8426 }
8427 kfree(pages);
8428}
8429
8430static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8431{
8432 struct page **pages;
8433 int i;
8434
8435 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8436 if (!pages) {
8437 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8438 return NULL;
8439 }
8440
8441 for (i = 0; i < size; i++) {
8442 pages[i] = alloc_page(gfp_flags);
8443 if (!pages[i]) {
8444 dprintk("%s: failed to allocate page\n", __func__);
8445 nfs4_free_pages(pages, size);
8446 return NULL;
8447 }
8448 }
8449
8450 return pages;
8451}
8452
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008453static void nfs4_layoutget_release(void *calldata)
8454{
8455 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008456 struct inode *inode = lgp->args.inode;
8457 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008458 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459
8460 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008461 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008462 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008463 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008464 put_nfs_open_context(lgp->args.ctx);
8465 kfree(calldata);
8466 dprintk("<-- %s\n", __func__);
8467}
8468
8469static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8470 .rpc_call_prepare = nfs4_layoutget_prepare,
8471 .rpc_call_done = nfs4_layoutget_done,
8472 .rpc_release = nfs4_layoutget_release,
8473};
8474
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008475struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008476nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008477{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008478 struct inode *inode = lgp->args.inode;
8479 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008480 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008481 struct rpc_task *task;
8482 struct rpc_message msg = {
8483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8484 .rpc_argp = &lgp->args,
8485 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008486 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008487 };
8488 struct rpc_task_setup task_setup_data = {
8489 .rpc_client = server->client,
8490 .rpc_message = &msg,
8491 .callback_ops = &nfs4_layoutget_call_ops,
8492 .callback_data = lgp,
8493 .flags = RPC_TASK_ASYNC,
8494 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008495 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008496 struct nfs4_exception exception = {
8497 .inode = inode,
8498 .timeout = *timeout,
8499 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008500 int status = 0;
8501
8502 dprintk("--> %s\n", __func__);
8503
Peng Tao4bd5a982014-11-17 11:05:17 +08008504 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8505 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8506
Idan Kedar85541162012-08-02 11:47:10 +03008507 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8508 if (!lgp->args.layout.pages) {
8509 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008510 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008511 }
8512 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8513
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008514 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008515 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008516 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008517
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008518 task = rpc_run_task(&task_setup_data);
8519 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008520 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008521 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008522 if (status == 0) {
8523 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8524 *timeout = exception.timeout;
8525 }
8526
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008527 trace_nfs4_layoutget(lgp->args.ctx,
8528 &lgp->args.range,
8529 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008530 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008531 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008532
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008533 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8534 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008535 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008536 rpc_put_task(task);
8537 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008538 if (status)
8539 return ERR_PTR(status);
8540 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008541}
8542
Benny Halevycbe82602011-05-22 19:52:37 +03008543static void
8544nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8545{
8546 struct nfs4_layoutreturn *lrp = calldata;
8547
8548 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008549 nfs41_setup_sequence(lrp->clp->cl_session,
8550 &lrp->args.seq_args,
8551 &lrp->res.seq_res,
8552 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008553}
8554
8555static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8556{
8557 struct nfs4_layoutreturn *lrp = calldata;
8558 struct nfs_server *server;
8559
8560 dprintk("--> %s\n", __func__);
8561
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008562 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008563 return;
8564
8565 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008566 switch (task->tk_status) {
8567 default:
8568 task->tk_status = 0;
8569 case 0:
8570 break;
8571 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008572 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008573 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008574 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008575 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008576 return;
8577 }
Benny Halevycbe82602011-05-22 19:52:37 +03008578 dprintk("<-- %s\n", __func__);
8579}
8580
8581static void nfs4_layoutreturn_release(void *calldata)
8582{
8583 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008584 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008585 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008586
8587 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008588 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008589 if (lrp->res.lrs_present) {
8590 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8591 &lrp->args.range,
8592 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008593 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008594 } else
8595 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008596 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008597 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008598 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008599 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008600 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008601 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008602 kfree(calldata);
8603 dprintk("<-- %s\n", __func__);
8604}
8605
8606static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8607 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8608 .rpc_call_done = nfs4_layoutreturn_done,
8609 .rpc_release = nfs4_layoutreturn_release,
8610};
8611
Peng Tao6c166052014-11-17 09:30:40 +08008612int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008613{
8614 struct rpc_task *task;
8615 struct rpc_message msg = {
8616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8617 .rpc_argp = &lrp->args,
8618 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008619 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008620 };
8621 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008622 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008623 .rpc_message = &msg,
8624 .callback_ops = &nfs4_layoutreturn_call_ops,
8625 .callback_data = lrp,
8626 };
Peng Tao6c166052014-11-17 09:30:40 +08008627 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008628
Andrew Elble99ade3c2015-12-02 09:39:51 -05008629 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8630 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8631 &task_setup_data.rpc_client, &msg);
8632
Benny Halevycbe82602011-05-22 19:52:37 +03008633 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008634 if (!sync) {
8635 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8636 if (!lrp->inode) {
8637 nfs4_layoutreturn_release(lrp);
8638 return -EAGAIN;
8639 }
8640 task_setup_data.flags |= RPC_TASK_ASYNC;
8641 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008642 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008643 task = rpc_run_task(&task_setup_data);
8644 if (IS_ERR(task))
8645 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008646 if (sync)
8647 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008648 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008649 dprintk("<-- %s status=%d\n", __func__, status);
8650 rpc_put_task(task);
8651 return status;
8652}
8653
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008654static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008655_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8656 struct pnfs_device *pdev,
8657 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008658{
8659 struct nfs4_getdeviceinfo_args args = {
8660 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008661 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8662 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663 };
8664 struct nfs4_getdeviceinfo_res res = {
8665 .pdev = pdev,
8666 };
8667 struct rpc_message msg = {
8668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8669 .rpc_argp = &args,
8670 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008671 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008672 };
8673 int status;
8674
8675 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008676 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008677 if (res.notification & ~args.notify_types)
8678 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008679 if (res.notification != args.notify_types)
8680 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008681
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008682 dprintk("<-- %s status=%d\n", __func__, status);
8683
8684 return status;
8685}
8686
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008687int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8688 struct pnfs_device *pdev,
8689 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008690{
8691 struct nfs4_exception exception = { };
8692 int err;
8693
8694 do {
8695 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008696 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008697 &exception);
8698 } while (exception.retry);
8699 return err;
8700}
8701EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8702
Andy Adamson863a3c62011-03-23 13:27:54 +00008703static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8704{
8705 struct nfs4_layoutcommit_data *data = calldata;
8706 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008707 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008708
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008709 nfs41_setup_sequence(session,
8710 &data->args.seq_args,
8711 &data->res.seq_res,
8712 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008713}
8714
8715static void
8716nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8717{
8718 struct nfs4_layoutcommit_data *data = calldata;
8719 struct nfs_server *server = NFS_SERVER(data->args.inode);
8720
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008721 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008722 return;
8723
8724 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008725 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8726 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8727 case -NFS4ERR_BADLAYOUT: /* no layout */
8728 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008729 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008730 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008731 break;
8732 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008733 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008734 rpc_restart_call_prepare(task);
8735 return;
8736 }
8737 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008738}
8739
8740static void nfs4_layoutcommit_release(void *calldata)
8741{
8742 struct nfs4_layoutcommit_data *data = calldata;
8743
Andy Adamsondb29c082011-07-30 20:52:38 -04008744 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008745 nfs_post_op_update_inode_force_wcc(data->args.inode,
8746 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008747 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008748 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008749 kfree(data);
8750}
8751
8752static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8753 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8754 .rpc_call_done = nfs4_layoutcommit_done,
8755 .rpc_release = nfs4_layoutcommit_release,
8756};
8757
8758int
Andy Adamsonef311532011-03-12 02:58:10 -05008759nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008760{
8761 struct rpc_message msg = {
8762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8763 .rpc_argp = &data->args,
8764 .rpc_resp = &data->res,
8765 .rpc_cred = data->cred,
8766 };
8767 struct rpc_task_setup task_setup_data = {
8768 .task = &data->task,
8769 .rpc_client = NFS_CLIENT(data->args.inode),
8770 .rpc_message = &msg,
8771 .callback_ops = &nfs4_layoutcommit_ops,
8772 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008773 };
8774 struct rpc_task *task;
8775 int status = 0;
8776
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008777 dprintk("NFS: initiating layoutcommit call. sync %d "
8778 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008779 data->args.lastbytewritten,
8780 data->args.inode->i_ino);
8781
Trond Myklebust472e2592015-02-05 16:50:30 -05008782 if (!sync) {
8783 data->inode = nfs_igrab_and_active(data->args.inode);
8784 if (data->inode == NULL) {
8785 nfs4_layoutcommit_release(data);
8786 return -EAGAIN;
8787 }
8788 task_setup_data.flags = RPC_TASK_ASYNC;
8789 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008790 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008791 task = rpc_run_task(&task_setup_data);
8792 if (IS_ERR(task))
8793 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008794 if (sync)
8795 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008796 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008797 dprintk("%s: status %d\n", __func__, status);
8798 rpc_put_task(task);
8799 return status;
8800}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008801
Andy Adamson97431202013-08-08 10:57:56 -04008802/**
8803 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8804 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8805 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008806static int
8807_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008808 struct nfs_fsinfo *info,
8809 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008810{
8811 struct nfs41_secinfo_no_name_args args = {
8812 .style = SECINFO_STYLE_CURRENT_FH,
8813 };
8814 struct nfs4_secinfo_res res = {
8815 .flavors = flavors,
8816 };
8817 struct rpc_message msg = {
8818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8819 .rpc_argp = &args,
8820 .rpc_resp = &res,
8821 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008822 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008823 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008824 int status;
8825
8826 if (use_integrity) {
8827 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008828 cred = nfs4_get_clid_cred(server->nfs_client);
8829 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008830 }
8831
8832 dprintk("--> %s\n", __func__);
8833 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8834 &res.seq_res, 0);
8835 dprintk("<-- %s status=%d\n", __func__, status);
8836
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008837 if (cred)
8838 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008839
8840 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008841}
8842
8843static int
8844nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8845 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8846{
8847 struct nfs4_exception exception = { };
8848 int err;
8849 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008850 /* first try using integrity protection */
8851 err = -NFS4ERR_WRONGSEC;
8852
8853 /* try to use integrity protection with machine cred */
8854 if (_nfs4_is_integrity_protected(server->nfs_client))
8855 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8856 flavors, true);
8857
8858 /*
8859 * if unable to use integrity protection, or SECINFO with
8860 * integrity protection returns NFS4ERR_WRONGSEC (which is
8861 * disallowed by spec, but exists in deployed servers) use
8862 * the current filesystem's rpc_client and the user cred.
8863 */
8864 if (err == -NFS4ERR_WRONGSEC)
8865 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8866 flavors, false);
8867
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008868 switch (err) {
8869 case 0:
8870 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008871 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008872 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008873 default:
8874 err = nfs4_handle_exception(server, err, &exception);
8875 }
8876 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008877out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008878 return err;
8879}
8880
8881static int
8882nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8883 struct nfs_fsinfo *info)
8884{
8885 int err;
8886 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008887 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008888 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008889 struct nfs4_secinfo4 *secinfo;
8890 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008891
8892 page = alloc_page(GFP_KERNEL);
8893 if (!page) {
8894 err = -ENOMEM;
8895 goto out;
8896 }
8897
8898 flavors = page_address(page);
8899 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8900
8901 /*
8902 * Fall back on "guess and check" method if
8903 * the server doesn't support SECINFO_NO_NAME
8904 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008905 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008906 err = nfs4_find_root_sec(server, fhandle, info);
8907 goto out_freepage;
8908 }
8909 if (err)
8910 goto out_freepage;
8911
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008912 for (i = 0; i < flavors->num_flavors; i++) {
8913 secinfo = &flavors->flavors[i];
8914
8915 switch (secinfo->flavor) {
8916 case RPC_AUTH_NULL:
8917 case RPC_AUTH_UNIX:
8918 case RPC_AUTH_GSS:
8919 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8920 &secinfo->flavor_info);
8921 break;
8922 default:
8923 flavor = RPC_AUTH_MAXFLAVOR;
8924 break;
8925 }
8926
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008927 if (!nfs_auth_info_match(&server->auth_info, flavor))
8928 flavor = RPC_AUTH_MAXFLAVOR;
8929
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008930 if (flavor != RPC_AUTH_MAXFLAVOR) {
8931 err = nfs4_lookup_root_sec(server, fhandle,
8932 info, flavor);
8933 if (!err)
8934 break;
8935 }
8936 }
8937
8938 if (flavor == RPC_AUTH_MAXFLAVOR)
8939 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008940
8941out_freepage:
8942 put_page(page);
8943 if (err == -EACCES)
8944 return -EPERM;
8945out:
8946 return err;
8947}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008948
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008949static int _nfs41_test_stateid(struct nfs_server *server,
8950 nfs4_stateid *stateid,
8951 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008952{
8953 int status;
8954 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008955 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008956 };
8957 struct nfs41_test_stateid_res res;
8958 struct rpc_message msg = {
8959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8960 .rpc_argp = &args,
8961 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008962 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008963 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008964 struct rpc_clnt *rpc_client = server->client;
8965
8966 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8967 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008968
Chuck Lever38527b12012-07-11 16:30:23 -04008969 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008970 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008971 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008972 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008973 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008974 if (status != NFS_OK) {
8975 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008976 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008977 }
8978 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008979 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008980}
8981
Trond Myklebust43912bb2016-09-22 13:38:56 -04008982static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8983 int err, struct nfs4_exception *exception)
8984{
8985 exception->retry = 0;
8986 switch(err) {
8987 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008988 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008989 nfs4_handle_exception(server, err, exception);
8990 break;
8991 case -NFS4ERR_BADSESSION:
8992 case -NFS4ERR_BADSLOT:
8993 case -NFS4ERR_BAD_HIGH_SLOT:
8994 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8995 case -NFS4ERR_DEADSESSION:
8996 nfs4_do_handle_exception(server, err, exception);
8997 }
8998}
8999
Chuck Lever38527b12012-07-11 16:30:23 -04009000/**
9001 * nfs41_test_stateid - perform a TEST_STATEID operation
9002 *
9003 * @server: server / transport on which to perform the operation
9004 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009005 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009006 *
9007 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9008 * Otherwise a negative NFS4ERR value is returned if the operation
9009 * failed or the state ID is not currently valid.
9010 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009011static int nfs41_test_stateid(struct nfs_server *server,
9012 nfs4_stateid *stateid,
9013 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009014{
9015 struct nfs4_exception exception = { };
9016 int err;
9017 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009018 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009019 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009020 } while (exception.retry);
9021 return err;
9022}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009023
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009024struct nfs_free_stateid_data {
9025 struct nfs_server *server;
9026 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009027 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009028};
9029
9030static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9031{
9032 struct nfs_free_stateid_data *data = calldata;
9033 nfs41_setup_sequence(nfs4_get_session(data->server),
9034 &data->args.seq_args,
9035 &data->res.seq_res,
9036 task);
9037}
9038
9039static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9040{
9041 struct nfs_free_stateid_data *data = calldata;
9042
9043 nfs41_sequence_done(task, &data->res.seq_res);
9044
9045 switch (task->tk_status) {
9046 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009047 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009048 rpc_restart_call_prepare(task);
9049 }
9050}
9051
9052static void nfs41_free_stateid_release(void *calldata)
9053{
9054 kfree(calldata);
9055}
9056
Trond Myklebust17f26b12013-08-21 15:48:42 -04009057static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009058 .rpc_call_prepare = nfs41_free_stateid_prepare,
9059 .rpc_call_done = nfs41_free_stateid_done,
9060 .rpc_release = nfs41_free_stateid_release,
9061};
9062
9063static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009064 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009065 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009066 bool privileged)
9067{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009068 struct rpc_message msg = {
9069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009070 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009071 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009072 struct rpc_task_setup task_setup = {
9073 .rpc_client = server->client,
9074 .rpc_message = &msg,
9075 .callback_ops = &nfs41_free_stateid_ops,
9076 .flags = RPC_TASK_ASYNC,
9077 };
9078 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009079
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009080 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9081 &task_setup.rpc_client, &msg);
9082
Chuck Lever38527b12012-07-11 16:30:23 -04009083 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009084 data = kmalloc(sizeof(*data), GFP_NOFS);
9085 if (!data)
9086 return ERR_PTR(-ENOMEM);
9087 data->server = server;
9088 nfs4_stateid_copy(&data->args.stateid, stateid);
9089
9090 task_setup.callback_data = data;
9091
9092 msg.rpc_argp = &data->args;
9093 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009094 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009095 if (privileged)
9096 nfs4_set_sequence_privileged(&data->args.seq_args);
9097
9098 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009099}
9100
Chuck Lever38527b12012-07-11 16:30:23 -04009101/**
9102 * nfs41_free_stateid - perform a FREE_STATEID operation
9103 *
9104 * @server: server / transport on which to perform the operation
9105 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009106 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009107 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009108 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009109 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009110 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009111static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009112 const nfs4_stateid *stateid,
9113 struct rpc_cred *cred,
9114 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009115{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009116 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009117
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009118 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009119 if (IS_ERR(task))
9120 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009121 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009122 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009123}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009124
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009125static void
9126nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009127{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009128 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009129
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009130 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009131 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009132}
9133
Trond Myklebust36281ca2012-03-04 18:13:56 -05009134static bool nfs41_match_stateid(const nfs4_stateid *s1,
9135 const nfs4_stateid *s2)
9136{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009137 if (s1->type != s2->type)
9138 return false;
9139
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009140 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009141 return false;
9142
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009143 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009144 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009145 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009146 return true;
9147
9148 return false;
9149}
9150
Andy Adamson557134a2009-04-01 09:21:53 -04009151#endif /* CONFIG_NFS_V4_1 */
9152
Trond Myklebust36281ca2012-03-04 18:13:56 -05009153static bool nfs4_match_stateid(const nfs4_stateid *s1,
9154 const nfs4_stateid *s2)
9155{
Trond Myklebustf597c532012-03-04 18:13:56 -05009156 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009157}
9158
9159
Trond Myklebust17280172012-03-11 13:11:00 -04009160static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009161 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009162 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009163 .recover_open = nfs4_open_reclaim,
9164 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009165 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009166 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167};
9168
Andy Adamson591d71c2009-04-01 09:22:47 -04009169#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009170static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009171 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9172 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9173 .recover_open = nfs4_open_reclaim,
9174 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009175 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009176 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009177 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009178};
9179#endif /* CONFIG_NFS_V4_1 */
9180
Trond Myklebust17280172012-03-11 13:11:00 -04009181static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009182 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009183 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009184 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009185 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009186 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187};
9188
Andy Adamson591d71c2009-04-01 09:22:47 -04009189#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009190static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009191 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9192 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009193 .recover_open = nfs41_open_expired,
9194 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009195 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009196};
9197#endif /* CONFIG_NFS_V4_1 */
9198
Trond Myklebust17280172012-03-11 13:11:00 -04009199static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009200 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009201 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009202 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009203};
9204
9205#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009206static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009207 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009208 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009209 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009210};
9211#endif
9212
Chuck Leverec011fe2013-10-17 14:12:39 -04009213static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009214 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009215 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009216};
9217
9218#if defined(CONFIG_NFS_V4_1)
9219static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009220 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009221 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009222};
9223#endif /* CONFIG_NFS_V4_1 */
9224
Trond Myklebust97dc1352010-06-16 09:52:26 -04009225static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9226 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009227 .init_caps = NFS_CAP_READDIRPLUS
9228 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009229 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009230 .init_client = nfs40_init_client,
9231 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009232 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009233 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009234 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009235 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009236 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009237 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009238 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9239 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9240 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009241 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009242};
9243
9244#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009245static struct nfs_seqid *
9246nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9247{
9248 return NULL;
9249}
9250
Trond Myklebust97dc1352010-06-16 09:52:26 -04009251static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9252 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009253 .init_caps = NFS_CAP_READDIRPLUS
9254 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009255 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009256 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009257 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009258 .init_client = nfs41_init_client,
9259 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009260 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009261 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009262 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009263 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009264 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009265 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009266 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009267 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9268 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9269 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009270 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009271};
9272#endif
9273
Steve Dickson42c2c422013-05-22 12:50:38 -04009274#if defined(CONFIG_NFS_V4_2)
9275static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9276 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009277 .init_caps = NFS_CAP_READDIRPLUS
9278 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009279 | NFS_CAP_POSIX_LOCK
9280 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009281 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009282 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009283 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009284 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009285 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009286 | NFS_CAP_LAYOUTSTATS
9287 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009288 .init_client = nfs41_init_client,
9289 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009290 .match_stateid = nfs41_match_stateid,
9291 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009292 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009293 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009294 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009295 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009296 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009297 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9298 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9299 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009300 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009301};
9302#endif
9303
Trond Myklebust97dc1352010-06-16 09:52:26 -04009304const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9305 [0] = &nfs_v4_0_minor_ops,
9306#if defined(CONFIG_NFS_V4_1)
9307 [1] = &nfs_v4_1_minor_ops,
9308#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009309#if defined(CONFIG_NFS_V4_2)
9310 [2] = &nfs_v4_2_minor_ops,
9311#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009312};
9313
Trond Myklebust13997822016-07-24 17:10:52 -04009314static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009315{
9316 ssize_t error, error2;
9317
9318 error = generic_listxattr(dentry, list, size);
9319 if (error < 0)
9320 return error;
9321 if (list) {
9322 list += error;
9323 size -= error;
9324 }
9325
9326 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9327 if (error2 < 0)
9328 return error2;
9329 return error + error2;
9330}
9331
Trond Myklebust17f26b12013-08-21 15:48:42 -04009332static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009333 .create = nfs_create,
9334 .lookup = nfs_lookup,
9335 .atomic_open = nfs_atomic_open,
9336 .link = nfs_link,
9337 .unlink = nfs_unlink,
9338 .symlink = nfs_symlink,
9339 .mkdir = nfs_mkdir,
9340 .rmdir = nfs_rmdir,
9341 .mknod = nfs_mknod,
9342 .rename = nfs_rename,
9343 .permission = nfs_permission,
9344 .getattr = nfs_getattr,
9345 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009346 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009347};
9348
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009349static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009350 .permission = nfs_permission,
9351 .getattr = nfs_getattr,
9352 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009353 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009354};
9355
David Howells509de812006-08-22 20:06:11 -04009356const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357 .version = 4, /* protocol version */
9358 .dentry_ops = &nfs4_dentry_operations,
9359 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009360 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009361 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009363 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009364 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .getattr = nfs4_proc_getattr,
9366 .setattr = nfs4_proc_setattr,
9367 .lookup = nfs4_proc_lookup,
9368 .access = nfs4_proc_access,
9369 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009370 .create = nfs4_proc_create,
9371 .remove = nfs4_proc_remove,
9372 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009373 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009375 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009376 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009377 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378 .link = nfs4_proc_link,
9379 .symlink = nfs4_proc_symlink,
9380 .mkdir = nfs4_proc_mkdir,
9381 .rmdir = nfs4_proc_remove,
9382 .readdir = nfs4_proc_readdir,
9383 .mknod = nfs4_proc_mknod,
9384 .statfs = nfs4_proc_statfs,
9385 .fsinfo = nfs4_proc_fsinfo,
9386 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009387 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009389 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009391 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009393 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009395 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009396 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009397 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009398 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009399 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009400 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009401 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009402 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009403 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009404 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009405 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009406 .create_server = nfs4_create_server,
9407 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408};
9409
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009410static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009411 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009412 .list = nfs4_xattr_list_nfs4_acl,
9413 .get = nfs4_xattr_get_nfs4_acl,
9414 .set = nfs4_xattr_set_nfs4_acl,
9415};
9416
9417const struct xattr_handler *nfs4_xattr_handlers[] = {
9418 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009419#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9420 &nfs4_xattr_nfs4_label_handler,
9421#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009422 NULL
9423};
9424
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425/*
9426 * Local variables:
9427 * c-basic-offset: 8
9428 * End:
9429 */