blob: 8354dfae7038e53bb1f456d43b056f5036cf8754 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Trond Myklebust4ce79712005-06-22 17:16:21 +000058#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050060#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050061#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040062#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040063#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040064#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040065#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020076/* file attributes which can be mapped to nfs attributes */
77#define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
86
Trond Myklebustcdd4e682006-01-03 09:55:12 +010087struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010088static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080089static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050091static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040092static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040094static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040096 struct nfs4_state *state, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040098#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040099static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
100 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400101static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
102 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400103#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400104
105#ifdef CONFIG_NFS_V4_SECURITY_LABEL
106static inline struct nfs4_label *
107nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
108 struct iattr *sattr, struct nfs4_label *label)
109{
110 int err;
111
112 if (label == NULL)
113 return NULL;
114
115 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
116 return NULL;
117
David Quigleyaa9c2662013-05-22 12:50:44 -0400118 err = security_dentry_init_security(dentry, sattr->ia_mode,
119 &dentry->d_name, (void **)&label->label, &label->len);
120 if (err == 0)
121 return label;
122
123 return NULL;
124}
125static inline void
126nfs4_label_release_security(struct nfs4_label *label)
127{
128 if (label)
129 security_release_secctx(label->label, label->len);
130}
131static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
132{
133 if (label)
134 return server->attr_bitmask;
135
136 return server->attr_bitmask_nl;
137}
138#else
139static inline struct nfs4_label *
140nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
141 struct iattr *sattr, struct nfs4_label *l)
142{ return NULL; }
143static inline void
144nfs4_label_release_security(struct nfs4_label *label)
145{ return; }
146static inline u32 *
147nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
148{ return server->attr_bitmask; }
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500152static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Trond Myklebust52567b02009-10-23 14:46:42 -0400154 if (err >= -1000)
155 return err;
156 switch (err) {
157 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500158 case -NFS4ERR_LAYOUTTRYLATER:
159 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000161 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400162 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000163 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800164 case -NFS4ERR_BADOWNER:
165 case -NFS4ERR_BADNAME:
166 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400167 case -NFS4ERR_SHARE_DENIED:
168 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400169 case -NFS4ERR_MINOR_VERS_MISMATCH:
170 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400171 case -NFS4ERR_FILE_OPEN:
172 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700175 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is our standard bitmap for GETATTR requests.
183 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400184const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID,
190 FATTR4_WORD1_MODE
191 | FATTR4_WORD1_NUMLINKS
192 | FATTR4_WORD1_OWNER
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400200#ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Trond Myklebust1549210f2012-06-05 09:16:47 -0400205static const u32 nfs4_pnfs_open_bitmap[3] = {
206 FATTR4_WORD0_TYPE
207 | FATTR4_WORD0_CHANGE
208 | FATTR4_WORD0_SIZE
209 | FATTR4_WORD0_FSID
210 | FATTR4_WORD0_FILEID,
211 FATTR4_WORD1_MODE
212 | FATTR4_WORD1_NUMLINKS
213 | FATTR4_WORD1_OWNER
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY,
220 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500221#ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
223#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400224};
225
Andy Adamsone23008e2012-10-02 21:07:32 -0400226static const u32 nfs4_open_noattr_bitmap[3] = {
227 FATTR4_WORD0_TYPE
228 | FATTR4_WORD0_CHANGE
229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500258 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Lever8ead2ac2017-11-05 15:45:22 -0500263 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD1_OWNER_GROUP
265 | FATTR4_WORD1_RAWDEV
266 | FATTR4_WORD1_SPACE_USED
267 | FATTR4_WORD1_TIME_ACCESS
268 | FATTR4_WORD1_TIME_METADATA
269 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400270 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400271};
272
Al Virobc4785c2006-10-19 23:28:51 -0700273static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct nfs4_readdir_arg *readdir)
275{
Al Viro0dbb4c62006-10-19 23:28:49 -0700276 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000279 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
281 return;
282 }
283
284 readdir->cookie = 0;
285 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
286 if (cookie == 2)
287 return;
288
289 /*
290 * NFSv4 servers do not return entries for '.' and '..'
291 * Therefore, we fake these entries here. We let '.'
292 * have cookie 0 and '..' have cookie 1. Note that
293 * when talking to the server, we always send cookie 0
294 * instead of 1 or 2.
295 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800296 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 if (cookie == 0) {
299 *p++ = xdr_one; /* next */
300 *p++ = xdr_zero; /* cookie, first word */
301 *p++ = xdr_one; /* cookie, second word */
302 *p++ = xdr_one; /* entry len */
303 memcpy(p, ".\0\0\0", 4); /* entry */
304 p++;
305 *p++ = xdr_one; /* bitmap length */
306 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
307 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000308 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 *p++ = xdr_one; /* next */
312 *p++ = xdr_zero; /* cookie, first word */
313 *p++ = xdr_two; /* cookie, second word */
314 *p++ = xdr_two; /* entry len */
315 memcpy(p, "..\0\0", 4); /* entry */
316 p++;
317 *p++ = xdr_one; /* bitmap length */
318 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
319 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000320 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 readdir->pgbase = (char *)p - (char *)start;
323 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800324 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Trond Myklebust26d36302016-09-22 13:39:05 -0400327static void nfs4_test_and_free_stateid(struct nfs_server *server,
328 nfs4_stateid *stateid,
329 struct rpc_cred *cred)
330{
331 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
332
333 ops->test_and_free_expired(server, stateid, cred);
334}
335
336static void __nfs4_free_revoked_stateid(struct nfs_server *server,
337 nfs4_stateid *stateid,
338 struct rpc_cred *cred)
339{
340 stateid->type = NFS4_REVOKED_STATEID_TYPE;
341 nfs4_test_and_free_stateid(server, stateid, cred);
342}
343
344static void nfs4_free_revoked_stateid(struct nfs_server *server,
345 const nfs4_stateid *stateid,
346 struct rpc_cred *cred)
347{
348 nfs4_stateid tmp;
349
350 nfs4_stateid_copy(&tmp, stateid);
351 __nfs4_free_revoked_stateid(server, &tmp, cred);
352}
353
NeilBrown8478eaa2014-09-18 16:09:27 +1000354static long nfs4_update_delay(long *timeout)
355{
356 long ret;
357 if (!timeout)
358 return NFS4_POLL_RETRY_MAX;
359 if (*timeout <= 0)
360 *timeout = NFS4_POLL_RETRY_MIN;
361 if (*timeout > NFS4_POLL_RETRY_MAX)
362 *timeout = NFS4_POLL_RETRY_MAX;
363 ret = *timeout;
364 *timeout <<= 1;
365 return ret;
366}
367
Trond Myklebust65de8722008-12-23 15:21:44 -0500368static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
369{
370 int res = 0;
371
372 might_sleep();
373
NeilBrown8478eaa2014-09-18 16:09:27 +1000374 freezable_schedule_timeout_killable_unsafe(
375 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500376 if (fatal_signal_pending(current))
377 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 return res;
379}
380
381/* This is the error handling routine for processes that are allowed
382 * to sleep.
383 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400384static int nfs4_do_handle_exception(struct nfs_server *server,
385 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500386{
387 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500388 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400389 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500390 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500391 int ret = errorcode;
392
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400393 exception->delay = 0;
394 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500395 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400396
397 if (stateid == NULL && state != NULL)
398 stateid = &state->stateid;
399
Trond Myklebust65de8722008-12-23 15:21:44 -0500400 switch(errorcode) {
401 case 0:
402 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400403 case -NFS4ERR_DELEG_REVOKED:
404 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400405 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 if (inode != NULL && stateid != NULL) {
408 nfs_inode_find_state_and_recover(inode,
409 stateid);
410 goto wait_on_recovery;
411 }
412 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400413 if (inode) {
414 int err;
415
416 err = nfs_async_inode_return_delegation(inode,
417 stateid);
418 if (err == 0)
419 goto wait_on_recovery;
420 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
421 exception->retry = 1;
422 break;
423 }
424 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 if (state == NULL)
426 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400427 ret = nfs4_schedule_stateid_recovery(server, state);
428 if (ret < 0)
429 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500430 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500431 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500433 nfs4_schedule_lease_recovery(clp);
434 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400435 case -NFS4ERR_MOVED:
436 ret = nfs4_schedule_migration_recovery(server);
437 if (ret < 0)
438 break;
439 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400440 case -NFS4ERR_LEASE_MOVED:
441 nfs4_schedule_lease_moved_recovery(clp);
442 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500443#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400444 case -NFS4ERR_BADSESSION:
445 case -NFS4ERR_BADSLOT:
446 case -NFS4ERR_BAD_HIGH_SLOT:
447 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
448 case -NFS4ERR_DEADSESSION:
449 case -NFS4ERR_SEQ_FALSE_RETRY:
450 case -NFS4ERR_SEQ_MISORDERED:
451 dprintk("%s ERROR: %d Reset session\n", __func__,
452 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400453 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500457 if (exception->timeout > HZ) {
458 /* We have retried a decent amount, time to
459 * fail
460 */
461 ret = -EBUSY;
462 break;
463 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400465 nfs_inc_server_stats(server, NFSIOS_DELAY);
466 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400467 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400468 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400469 exception->delay = 1;
470 return 0;
471
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400472 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 case -NFS4ERR_OLD_STATEID:
474 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800475 break;
476 case -NFS4ERR_BADOWNER:
477 /* The following works around a Linux server bug! */
478 case -NFS4ERR_BADNAME:
479 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
480 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
481 exception->retry = 1;
482 printk(KERN_WARNING "NFS: v4 server %s "
483 "does not accept raw "
484 "uid/gids. "
485 "Reenabling the idmapper.\n",
486 server->nfs_client->cl_hostname);
487 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500488 }
489 /* We failed to handle the error */
490 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500491wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400492 exception->recovering = 1;
493 return 0;
494}
495
496/* This is the error handling routine for processes that are allowed
497 * to sleep.
498 */
499int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
500{
501 struct nfs_client *clp = server->nfs_client;
502 int ret;
503
504 ret = nfs4_do_handle_exception(server, errorcode, exception);
505 if (exception->delay) {
506 ret = nfs4_delay(server->client, &exception->timeout);
507 goto out_retry;
508 }
509 if (exception->recovering) {
510 ret = nfs4_wait_clnt_recover(clp);
511 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
512 return -EIO;
513 goto out_retry;
514 }
515 return ret;
516out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500517 if (ret == 0)
518 exception->retry = 1;
519 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500520}
521
Trond Myklebust037fc982015-09-20 15:51:00 -0400522static int
523nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
524 int errorcode, struct nfs4_exception *exception)
525{
526 struct nfs_client *clp = server->nfs_client;
527 int ret;
528
529 ret = nfs4_do_handle_exception(server, errorcode, exception);
530 if (exception->delay) {
531 rpc_delay(task, nfs4_update_delay(&exception->timeout));
532 goto out_retry;
533 }
534 if (exception->recovering) {
535 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
536 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
537 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
538 goto out_retry;
539 }
540 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
541 ret = -EIO;
542 return ret;
543out_retry:
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500544 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400545 exception->retry = 1;
Bill Bakerb5bc39d2018-06-19 16:24:58 -0500546 /*
547 * For NFS4ERR_MOVED, the client transport will need to
548 * be recomputed after migration recovery has completed.
549 */
550 if (errorcode == -NFS4ERR_MOVED)
551 rpc_task_release_transport(task);
552 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
585
586 if (flavor == RPC_AUTH_GSS_KRB5I ||
587 flavor == RPC_AUTH_GSS_KRB5P)
588 return true;
589
590 return false;
591}
Trond Myklebust65de8722008-12-23 15:21:44 -0500592
Trond Myklebust452e9352010-07-31 14:29:06 -0400593static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&clp->cl_lock);
596 if (time_before(clp->cl_last_renewal,timestamp))
597 clp->cl_last_renewal = timestamp;
598 spin_unlock(&clp->cl_lock);
599}
600
Trond Myklebust452e9352010-07-31 14:29:06 -0400601static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
602{
Trond Myklebustbe824162015-07-05 14:50:46 -0400603 struct nfs_client *clp = server->nfs_client;
604
605 if (!nfs4_has_session(clp))
606 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400607}
608
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400609struct nfs4_call_sync_data {
610 const struct nfs_server *seq_server;
611 struct nfs4_sequence_args *seq_args;
612 struct nfs4_sequence_res *seq_res;
613};
614
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800615void nfs4_init_sequence(struct nfs4_sequence_args *args,
616 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400617{
618 args->sa_slot = NULL;
619 args->sa_cache_this = cache_reply;
620 args->sa_privileged = 0;
621
622 res->sr_slot = NULL;
623}
624
625static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
626{
627 args->sa_privileged = 1;
628}
629
Peng Taocb04ad22014-06-11 05:24:15 +0800630int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
631 struct nfs4_sequence_args *args,
632 struct nfs4_sequence_res *res,
633 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400634{
Chuck Lever3bd23842013-08-09 12:49:19 -0400635 struct nfs4_slot *slot;
636
637 /* slot already allocated? */
638 if (res->sr_slot != NULL)
639 goto out_start;
640
641 spin_lock(&tbl->slot_tbl_lock);
642 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
643 goto out_sleep;
644
645 slot = nfs4_alloc_slot(tbl);
646 if (IS_ERR(slot)) {
647 if (slot == ERR_PTR(-ENOMEM))
648 task->tk_timeout = HZ >> 2;
649 goto out_sleep;
650 }
651 spin_unlock(&tbl->slot_tbl_lock);
652
Trond Myklebust0a014a42016-09-22 13:38:51 -0400653 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400654 args->sa_slot = slot;
655 res->sr_slot = slot;
656
657out_start:
658 rpc_call_start(task);
659 return 0;
660
661out_sleep:
662 if (args->sa_privileged)
663 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
664 NULL, RPC_PRIORITY_PRIVILEGED);
665 else
666 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
667 spin_unlock(&tbl->slot_tbl_lock);
668 return -EAGAIN;
669}
Peng Taocb04ad22014-06-11 05:24:15 +0800670EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400671
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400672static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400673{
674 struct nfs4_slot *slot = res->sr_slot;
675 struct nfs4_slot_table *tbl;
676
Chuck Lever3bd23842013-08-09 12:49:19 -0400677 tbl = slot->table;
678 spin_lock(&tbl->slot_tbl_lock);
679 if (!nfs41_wake_and_assign_slot(tbl, slot))
680 nfs4_free_slot(tbl, slot);
681 spin_unlock(&tbl->slot_tbl_lock);
682
683 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400684}
685
686static int nfs40_sequence_done(struct rpc_task *task,
687 struct nfs4_sequence_res *res)
688{
689 if (res->sr_slot != NULL)
690 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400691 return 1;
692}
693
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694#if defined(CONFIG_NFS_V4_1)
695
Trond Myklebustd185a332010-06-16 09:52:25 -0400696static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400699 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500700 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500701 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500704 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500705
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400706 /* Bump the slot sequence number */
707 if (slot->seq_done)
708 slot->seq_nr++;
709 slot->seq_done = 0;
710
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500711 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500712 /* Be nice to the server: try to ensure that the last transmitted
713 * value for highest_user_slotid <= target_highest_slotid
714 */
715 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
716 send_new_highest_used_slotid = true;
717
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719 send_new_highest_used_slotid = false;
720 goto out_unlock;
721 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500722 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723
724 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
725 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500726out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500727 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400728 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500729 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400730 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400731 if (waitqueue_active(&tbl->slot_waitq))
732 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400733}
734
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400735static int nfs41_sequence_process(struct rpc_task *task,
736 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400737{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500738 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500741 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500742 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400743
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500744 if (slot == NULL)
745 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400746 /* don't increment the sequence number if the task wasn't sent */
747 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400748 goto out;
749
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500750 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500751
Trond Myklebustac20d162012-12-15 15:36:07 -0500752 if (slot->interrupted) {
753 slot->interrupted = 0;
754 interrupted = true;
755 }
756
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400757 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500758 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400759 switch (res->sr_status) {
760 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400761 /* If previous op on slot was interrupted and we reused
762 * the seq# and got a reply from the cache, then retry
763 */
764 if (task->tk_status == -EREMOTEIO && interrupted) {
765 ++slot->seq_nr;
766 goto retry_nowait;
767 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400768 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400769 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500770 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500771 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500772 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400773 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
774 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500775 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500777 case 1:
778 /*
779 * sr_status remains 1 if an RPC level error occurred.
780 * The server may or may not have processed the sequence
781 * operation..
782 * Mark the slot as having hosted an interrupted RPC call.
783 */
784 slot->interrupted = 1;
785 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400786 case -NFS4ERR_DELAY:
787 /* The server detected a resend of the RPC call and
788 * returned NFS4ERR_DELAY as per Section 2.10.6.2
789 * of RFC5661.
790 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500791 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400792 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500793 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500794 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400795 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500796 case -NFS4ERR_BADSLOT:
797 /*
798 * The slot id we used was probably retired. Try again
799 * using a different slot id.
800 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500801 goto retry_nowait;
802 case -NFS4ERR_SEQ_MISORDERED:
803 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500804 * Was the last operation on this sequence interrupted?
805 * If so, retry after bumping the sequence number.
806 */
807 if (interrupted) {
808 ++slot->seq_nr;
809 goto retry_nowait;
810 }
811 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500812 * Could this slot have been previously retired?
813 * If so, then the server may be expecting seq_nr = 1!
814 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500815 if (slot->seq_nr != 1) {
816 slot->seq_nr = 1;
817 goto retry_nowait;
818 }
819 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500820 case -NFS4ERR_SEQ_FALSE_RETRY:
821 ++slot->seq_nr;
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168a2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168a2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001112 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1115 struct iattr attrs;
1116 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001118 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001119 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int cancelled;
1122};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001124struct nfs4_open_createattrs {
1125 struct nfs4_label *label;
1126 struct iattr *sattr;
1127 const __u32 verf[2];
1128};
1129
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001130static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1131 int err, struct nfs4_exception *exception)
1132{
1133 if (err != -EINVAL)
1134 return false;
1135 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1136 return false;
1137 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1138 exception->retry = 1;
1139 return true;
1140}
1141
Trond Myklebust6ae37332015-01-30 14:21:14 -05001142static u32
1143nfs4_map_atomic_open_share(struct nfs_server *server,
1144 fmode_t fmode, int openflags)
1145{
1146 u32 res = 0;
1147
1148 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1149 case FMODE_READ:
1150 res = NFS4_SHARE_ACCESS_READ;
1151 break;
1152 case FMODE_WRITE:
1153 res = NFS4_SHARE_ACCESS_WRITE;
1154 break;
1155 case FMODE_READ|FMODE_WRITE:
1156 res = NFS4_SHARE_ACCESS_BOTH;
1157 }
1158 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1159 goto out;
1160 /* Want no delegation if we're using O_DIRECT */
1161 if (openflags & O_DIRECT)
1162 res |= NFS4_SHARE_WANT_NO_DELEG;
1163out:
1164 return res;
1165}
1166
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001167static enum open_claim_type4
1168nfs4_map_atomic_open_claim(struct nfs_server *server,
1169 enum open_claim_type4 claim)
1170{
1171 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1172 return claim;
1173 switch (claim) {
1174 default:
1175 return claim;
1176 case NFS4_OPEN_CLAIM_FH:
1177 return NFS4_OPEN_CLAIM_NULL;
1178 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1179 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1180 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1182 }
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186{
1187 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001188 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->o_res.seqid = p->o_arg.seqid;
1190 p->c_res.seqid = p->c_arg.seqid;
1191 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001192 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001194 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195}
1196
Al Viro82a2c1b2011-06-22 18:30:55 -04001197static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001199 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001200 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001201 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202{
Al Viro82a2c1b2011-06-22 18:30:55 -04001203 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001204 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001207 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208 struct nfs4_opendata *p;
1209
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 if (p == NULL)
1212 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001213
1214 p->f_label = nfs4_label_alloc(server, gfp_mask);
1215 if (IS_ERR(p->f_label))
1216 goto err_free_p;
1217
Kinglong Meea49c2692015-07-27 15:31:38 +08001218 p->a_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->a_label))
1220 goto err_free_f;
1221
Trond Myklebust63f5f792015-01-23 19:19:25 -05001222 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1223 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001224 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001225 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001226 nfs_sb_active(dentry->d_sb);
1227 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 p->dir = parent;
1229 p->owner = sp;
1230 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001231 p->o_arg.open_flags = flags;
1232 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001233 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1234 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001235 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1236 * will return permission denied for all bits until close */
1237 if (!(flags & O_EXCL)) {
1238 /* ask server to check for all possible rights as results
1239 * are cached */
1240 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1241 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001242 }
David Howells7539bba2006-08-22 20:06:09 -04001243 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001244 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1245 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001246 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001248 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001249 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001250 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001251 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1252 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001253 case NFS4_OPEN_CLAIM_NULL:
1254 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1255 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1256 p->o_arg.fh = NFS_FH(dir);
1257 break;
1258 case NFS4_OPEN_CLAIM_PREVIOUS:
1259 case NFS4_OPEN_CLAIM_FH:
1260 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1261 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001262 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001263 }
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001264 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001266 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001267
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001268 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001269 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001271 p->c_arg.fh = &p->o_res.fh;
1272 p->c_arg.stateid = &p->o_res.stateid;
1273 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001274 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001277
1278err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001279 nfs4_label_free(p->a_label);
1280err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001281 nfs4_label_free(p->f_label);
1282err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 kfree(p);
1284err:
1285 dput(parent);
1286 return NULL;
1287}
1288
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001291 struct nfs4_opendata *p = container_of(kref,
1292 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001293 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001294
1295 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001296 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001297 if (p->state != NULL)
1298 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001299 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001300
Kinglong Meea49c2692015-07-27 15:31:38 +08001301 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001302 nfs4_label_free(p->f_label);
1303
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001305 dput(p->dentry);
1306 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001307 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001308 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 kfree(p);
1310}
1311
1312static void nfs4_opendata_put(struct nfs4_opendata *p)
1313{
1314 if (p != NULL)
1315 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316}
1317
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1319{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001320 int ret;
1321
Trond Myklebust06f814a2006-01-03 09:55:07 +01001322 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001323 return ret;
1324}
1325
Trond Myklebust24311f82015-09-20 10:50:17 -04001326static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1327 fmode_t fmode)
1328{
1329 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1330 case FMODE_READ|FMODE_WRITE:
1331 return state->n_rdwr != 0;
1332 case FMODE_WRITE:
1333 return state->n_wronly != 0;
1334 case FMODE_READ:
1335 return state->n_rdonly != 0;
1336 }
1337 WARN_ON_ONCE(1);
1338 return false;
1339}
1340
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001341static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001342{
1343 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001344
Trond Myklebust536e43d2012-01-17 22:04:26 -05001345 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 goto out;
1347 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001348 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001349 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1350 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001351 break;
1352 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001353 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1354 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001355 break;
1356 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001357 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1358 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 return ret;
1362}
1363
Trond Myklebust2a606182015-08-19 22:30:00 -05001364static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1365 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001367 if (delegation == NULL)
1368 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001369 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001371 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1372 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001373 switch (claim) {
1374 case NFS4_OPEN_CLAIM_NULL:
1375 case NFS4_OPEN_CLAIM_FH:
1376 break;
1377 case NFS4_OPEN_CLAIM_PREVIOUS:
1378 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1379 break;
1380 default:
1381 return 0;
1382 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001383 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384 return 1;
1385}
1386
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001388{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001389 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001390 case FMODE_WRITE:
1391 state->n_wronly++;
1392 break;
1393 case FMODE_READ:
1394 state->n_rdonly++;
1395 break;
1396 case FMODE_READ|FMODE_WRITE:
1397 state->n_rdwr++;
1398 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001399 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001400}
1401
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001402#ifdef CONFIG_NFS_V4_1
1403static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1404{
1405 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1406 return true;
1407 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1408 return true;
1409 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1410 return true;
1411 return false;
1412}
1413#endif /* CONFIG_NFS_V4_1 */
1414
Trond Myklebust4f14c192014-02-12 19:15:06 -05001415static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001416{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001417 struct nfs_client *clp = state->owner->so_server->nfs_client;
1418 bool need_recover = false;
1419
1420 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1421 need_recover = true;
1422 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1423 need_recover = true;
1424 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1425 need_recover = true;
1426 if (need_recover)
1427 nfs4_state_mark_reclaim_nograce(clp, state);
1428}
1429
Trond Myklebuste999e802014-02-10 18:20:47 -05001430static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001431 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001432{
1433 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1434 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001436 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001438 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001439 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001440 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1441 return true;
1442 return false;
1443}
1444
Trond Myklebustf95549c2015-01-23 18:06:09 -05001445static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1446{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001447 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1448 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001449 if (state->n_wronly)
1450 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1451 if (state->n_rdonly)
1452 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1453 if (state->n_rdwr)
1454 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001455 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001456}
1457
Trond Myklebust226056c2014-02-11 10:41:07 -05001458static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1459 nfs4_stateid *stateid, fmode_t fmode)
1460{
1461 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1462 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1463 case FMODE_WRITE:
1464 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1465 break;
1466 case FMODE_READ:
1467 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1468 break;
1469 case 0:
1470 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1471 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1472 clear_bit(NFS_OPEN_STATE, &state->flags);
1473 }
1474 if (stateid == NULL)
1475 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001476 /* Handle OPEN+OPEN_DOWNGRADE races */
1477 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1478 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001479 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001480 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001481 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001482 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001483 nfs4_stateid_copy(&state->stateid, stateid);
1484 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001485}
1486
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001487static void nfs_clear_open_stateid(struct nfs4_state *state,
1488 nfs4_stateid *arg_stateid,
1489 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001490{
1491 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001492 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1493 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1494 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001495 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001496 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1497 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001498}
1499
Trond Myklebust1393d962016-09-22 13:39:13 -04001500static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1501 const nfs4_stateid *stateid, fmode_t fmode,
1502 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001503{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001504 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001505 case FMODE_READ:
1506 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1507 break;
1508 case FMODE_WRITE:
1509 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1510 break;
1511 case FMODE_READ|FMODE_WRITE:
1512 set_bit(NFS_O_RDWR_STATE, &state->flags);
1513 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001514 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001515 return;
1516 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1517 nfs4_stateid_copy(&state->stateid, stateid);
1518 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001519}
1520
Trond Myklebust1393d962016-09-22 13:39:13 -04001521static void __update_open_stateid(struct nfs4_state *state,
1522 const nfs4_stateid *open_stateid,
1523 const nfs4_stateid *deleg_stateid,
1524 fmode_t fmode,
1525 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001527 /*
1528 * Protect the call to nfs4_state_set_mode_locked and
1529 * serialise the stateid update
1530 */
Andrew Elble361cad32015-12-02 09:20:57 -05001531 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001532 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001533 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001534 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001535 set_bit(NFS_DELEGATED_STATE, &state->flags);
1536 }
1537 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001538 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001539 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001540 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001541 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Trond Myklebust1393d962016-09-22 13:39:13 -04001544static int update_open_stateid(struct nfs4_state *state,
1545 const nfs4_stateid *open_stateid,
1546 const nfs4_stateid *delegation,
1547 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001548{
Trond Myklebust1393d962016-09-22 13:39:13 -04001549 struct nfs_server *server = NFS_SERVER(state->inode);
1550 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001551 struct nfs_inode *nfsi = NFS_I(state->inode);
1552 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001553 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001554 int ret = 0;
1555
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001556 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001557
1558 rcu_read_lock();
1559 deleg_cur = rcu_dereference(nfsi->delegation);
1560 if (deleg_cur == NULL)
1561 goto no_delegation;
1562
1563 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001564 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001565 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001566 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001567 goto no_delegation_unlock;
1568
1569 if (delegation == NULL)
1570 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001571 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001572 goto no_delegation_unlock;
1573
Trond Myklebustb7391f42008-12-23 15:21:52 -05001574 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001575 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1576 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001577 ret = 1;
1578no_delegation_unlock:
1579 spin_unlock(&deleg_cur->lock);
1580no_delegation:
1581 rcu_read_unlock();
1582
1583 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001584 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001585 ret = 1;
1586 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001587 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001588 nfs4_schedule_state_manager(clp);
1589 if (freeme.type != 0)
1590 nfs4_test_and_free_stateid(server, &freeme,
1591 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001592
1593 return ret;
1594}
1595
Trond Myklebust39071e62015-01-24 15:07:56 -05001596static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1597 const nfs4_stateid *stateid)
1598{
1599 struct nfs4_state *state = lsp->ls_state;
1600 bool ret = false;
1601
1602 spin_lock(&state->state_lock);
1603 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1604 goto out_noupdate;
1605 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1606 goto out_noupdate;
1607 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1608 ret = true;
1609out_noupdate:
1610 spin_unlock(&state->state_lock);
1611 return ret;
1612}
Trond Myklebust34310432008-12-23 15:21:38 -05001613
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001614static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001615{
1616 struct nfs_delegation *delegation;
1617
1618 rcu_read_lock();
1619 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001620 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001621 rcu_read_unlock();
1622 return;
1623 }
1624 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001625 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001626}
1627
Trond Myklebust6ee41262007-07-08 14:11:36 -04001628static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001629{
1630 struct nfs4_state *state = opendata->state;
1631 struct nfs_inode *nfsi = NFS_I(state->inode);
1632 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001633 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001635 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636 nfs4_stateid stateid;
1637 int ret = -EAGAIN;
1638
Trond Myklebustaac00a82007-07-05 19:02:21 -04001639 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001640 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001641 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001642 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001643 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001644 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001645 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001646 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001647 rcu_read_lock();
1648 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001649 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001650 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001651 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001652 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001654 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001656 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001657 if (!opendata->is_recover) {
1658 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1659 if (ret != 0)
1660 goto out;
1661 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001662 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001663
1664 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001665 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001666 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001667 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668out:
1669 return ERR_PTR(ret);
1670out_return_state:
1671 atomic_inc(&state->count);
1672 return state;
1673}
1674
Andy Adamsone23008e2012-10-02 21:07:32 -04001675static void
1676nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1677{
1678 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1679 struct nfs_delegation *delegation;
1680 int delegation_flags = 0;
1681
1682 rcu_read_lock();
1683 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1684 if (delegation)
1685 delegation_flags = delegation->flags;
1686 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001687 switch (data->o_arg.claim) {
1688 default:
1689 break;
1690 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1691 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001692 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1693 "returning a delegation for "
1694 "OPEN(CLAIM_DELEGATE_CUR)\n",
1695 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001696 return;
1697 }
1698 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001699 nfs_inode_set_delegation(state->inode,
1700 data->owner->so_cred,
1701 &data->o_res);
1702 else
1703 nfs_inode_reclaim_delegation(state->inode,
1704 data->owner->so_cred,
1705 &data->o_res);
1706}
1707
1708/*
1709 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1710 * and update the nfs4_state.
1711 */
1712static struct nfs4_state *
1713_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1714{
1715 struct inode *inode = data->state->inode;
1716 struct nfs4_state *state = data->state;
1717 int ret;
1718
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001719 if (!data->rpc_done) {
1720 if (data->rpc_status) {
1721 ret = data->rpc_status;
1722 goto err;
1723 }
1724 /* cached opens have already been processed */
1725 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001726 }
1727
Andy Adamsone23008e2012-10-02 21:07:32 -04001728 ret = nfs_refresh_inode(inode, &data->f_attr);
1729 if (ret)
1730 goto err;
1731
1732 if (data->o_res.delegation_type != 0)
1733 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001734update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001735 update_open_stateid(state, &data->o_res.stateid, NULL,
1736 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001737 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001738
1739 return state;
1740err:
1741 return ERR_PTR(ret);
1742
1743}
1744
1745static struct nfs4_state *
1746_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747{
1748 struct inode *inode;
1749 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001750 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001751
Trond Myklebustaac00a82007-07-05 19:02:21 -04001752 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001753 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001754 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001755 goto out;
1756 }
1757
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001758 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001761 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001762 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001763 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 goto err;
1765 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 state = nfs4_get_open_state(inode, data->owner);
1767 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001768 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001769 if (data->o_res.delegation_type != 0)
1770 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001771 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001772 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001774out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001775 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001776 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001777err_put_inode:
1778 iput(inode);
1779err:
1780 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781}
1782
Andy Adamsone23008e2012-10-02 21:07:32 -04001783static struct nfs4_state *
1784nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1785{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001786 struct nfs4_state *ret;
1787
Andy Adamsone23008e2012-10-02 21:07:32 -04001788 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001789 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1790 else
1791 ret = _nfs4_opendata_to_nfs4_state(data);
1792 nfs4_sequence_free_slot(&data->o_res.seq_res);
1793 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001794}
1795
Trond Myklebust864472e2006-01-03 09:55:15 +01001796static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1797{
1798 struct nfs_inode *nfsi = NFS_I(state->inode);
1799 struct nfs_open_context *ctx;
1800
1801 spin_lock(&state->inode->i_lock);
1802 list_for_each_entry(ctx, &nfsi->open_files, list) {
1803 if (ctx->state != state)
1804 continue;
1805 get_nfs_open_context(ctx);
1806 spin_unlock(&state->inode->i_lock);
1807 return ctx;
1808 }
1809 spin_unlock(&state->inode->i_lock);
1810 return ERR_PTR(-ENOENT);
1811}
1812
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001813static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1814 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001815{
1816 struct nfs4_opendata *opendata;
1817
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001818 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05001819 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001820 if (opendata == NULL)
1821 return ERR_PTR(-ENOMEM);
1822 opendata->state = state;
1823 atomic_inc(&state->count);
1824 return opendata;
1825}
1826
Trond Myklebust24311f82015-09-20 10:50:17 -04001827static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1828 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001829{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001830 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001831 int ret;
1832
Trond Myklebust24311f82015-09-20 10:50:17 -04001833 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001834 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 opendata->o_arg.open_flags = 0;
1836 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001837 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1838 NFS_SB(opendata->dentry->d_sb),
1839 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001840 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1841 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1842 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001843 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001844 if (ret != 0)
1845 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001846 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001847 if (IS_ERR(newstate))
1848 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001849 if (newstate != opendata->state)
1850 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001851 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001852 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001853}
1854
1855static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1856{
Trond Myklebust864472e2006-01-03 09:55:15 +01001857 int ret;
1858
Trond Myklebust4f14c192014-02-12 19:15:06 -05001859 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1860 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1861 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1862 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001864 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001865 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001866 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001867 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1868 if (ret != 0)
1869 return ret;
1870 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1871 if (ret != 0)
1872 return ret;
1873 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1874 if (ret != 0)
1875 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001876 /*
1877 * We may have performed cached opens for all three recoveries.
1878 * Check if we need to update the current stateid.
1879 */
1880 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001881 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001882 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001883 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001884 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001885 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001886 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001887 return 0;
1888}
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/*
1891 * OPEN_RECLAIM:
1892 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001894static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001896 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001897 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001898 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 int status;
1900
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001901 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1902 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001903 if (IS_ERR(opendata))
1904 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001905 rcu_read_lock();
1906 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001907 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001908 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001909 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001910 opendata->o_arg.u.delegation_type = delegation_type;
1911 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001912 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return status;
1914}
1915
Trond Myklebust539cd032007-06-05 11:46:42 -04001916static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 struct nfs_server *server = NFS_SERVER(state->inode);
1919 struct nfs4_exception exception = { };
1920 int err;
1921 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001922 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001923 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001924 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1925 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001926 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001927 break;
1928 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 } while (exception.retry);
1930 return err;
1931}
1932
Trond Myklebust864472e2006-01-03 09:55:15 +01001933static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1934{
1935 struct nfs_open_context *ctx;
1936 int ret;
1937
1938 ctx = nfs4_state_find_open_context(state);
1939 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001940 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001941 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001942 put_nfs_open_context(ctx);
1943 return ret;
1944}
1945
NeilBrown70f254a2017-12-13 09:57:09 +11001946static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001948 switch (err) {
1949 default:
1950 printk(KERN_ERR "NFS: %s: unhandled error "
1951 "%d.\n", __func__, err);
1952 case 0:
1953 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001954 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001955 case -ESTALE:
1956 break;
1957 case -NFS4ERR_BADSESSION:
1958 case -NFS4ERR_BADSLOT:
1959 case -NFS4ERR_BAD_HIGH_SLOT:
1960 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1961 case -NFS4ERR_DEADSESSION:
1962 set_bit(NFS_DELEGATED_STATE, &state->flags);
1963 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1964 return -EAGAIN;
1965 case -NFS4ERR_STALE_CLIENTID:
1966 case -NFS4ERR_STALE_STATEID:
1967 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001968 /* Don't recall a delegation if it was lost */
1969 nfs4_schedule_lease_recovery(server->nfs_client);
1970 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001971 case -NFS4ERR_MOVED:
1972 nfs4_schedule_migration_recovery(server);
1973 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001974 case -NFS4ERR_LEASE_MOVED:
1975 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1976 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001977 case -NFS4ERR_DELEG_REVOKED:
1978 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001979 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001980 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001981 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001982 nfs_inode_find_state_and_recover(state->inode,
1983 stateid);
1984 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001985 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001986 case -NFS4ERR_DELAY:
1987 case -NFS4ERR_GRACE:
1988 set_bit(NFS_DELEGATED_STATE, &state->flags);
1989 ssleep(1);
1990 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001991 case -ENOMEM:
1992 case -NFS4ERR_DENIED:
NeilBrown70f254a2017-12-13 09:57:09 +11001993 if (fl) {
1994 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
1995 if (lsp)
1996 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1997 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001998 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return err;
2001}
2002
Trond Myklebust24311f82015-09-20 10:50:17 -04002003int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2004 struct nfs4_state *state, const nfs4_stateid *stateid,
2005 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002006{
2007 struct nfs_server *server = NFS_SERVER(state->inode);
2008 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002009 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002010
2011 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2012 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2013 if (IS_ERR(opendata))
2014 return PTR_ERR(opendata);
2015 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002016 write_seqlock(&state->seqlock);
2017 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2018 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002019 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2020 switch (type & (FMODE_READ|FMODE_WRITE)) {
2021 case FMODE_READ|FMODE_WRITE:
2022 case FMODE_WRITE:
2023 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2024 if (err)
2025 break;
2026 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2027 if (err)
2028 break;
2029 case FMODE_READ:
2030 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2031 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002032 nfs4_opendata_put(opendata);
NeilBrown70f254a2017-12-13 09:57:09 +11002033 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04002034}
2035
Chuck Leverbe05c862013-08-09 12:49:47 -04002036static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2037{
2038 struct nfs4_opendata *data = calldata;
2039
Peng Taocb04ad22014-06-11 05:24:15 +08002040 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2041 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002042}
2043
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002044static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2045{
2046 struct nfs4_opendata *data = calldata;
2047
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002048 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002049
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002050 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002051 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002052 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002053 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002054 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002055 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002056 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002057}
2058
2059static void nfs4_open_confirm_release(void *calldata)
2060{
2061 struct nfs4_opendata *data = calldata;
2062 struct nfs4_state *state = NULL;
2063
2064 /* If this request hasn't been cancelled, do nothing */
2065 if (data->cancelled == 0)
2066 goto out_free;
2067 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002068 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002069 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002070 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002071 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002072 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002074 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002075}
2076
2077static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002078 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002079 .rpc_call_done = nfs4_open_confirm_done,
2080 .rpc_release = nfs4_open_confirm_release,
2081};
2082
2083/*
2084 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2085 */
2086static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2087{
David Howells2b0143b2015-03-17 22:25:59 +00002088 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002089 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002090 struct rpc_message msg = {
2091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2092 .rpc_argp = &data->c_arg,
2093 .rpc_resp = &data->c_res,
2094 .rpc_cred = data->owner->so_cred,
2095 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002096 struct rpc_task_setup task_setup_data = {
2097 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002098 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002099 .callback_ops = &nfs4_open_confirm_ops,
2100 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002101 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002102 .flags = RPC_TASK_ASYNC,
2103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int status;
2105
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002106 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002107 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002108 data->rpc_done = 0;
2109 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002110 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002111 if (data->is_recover)
2112 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002113 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002114 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002115 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002116 status = nfs4_wait_for_completion_rpc_task(task);
2117 if (status != 0) {
2118 data->cancelled = 1;
2119 smp_wmb();
2120 } else
2121 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002122 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return status;
2124}
2125
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002126static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002128 struct nfs4_opendata *data = calldata;
2129 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002130 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002131 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002132
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002133 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002134 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002135 /*
2136 * Check if we still need to send an OPEN call, or if we can use
2137 * a delegation instead.
2138 */
2139 if (data->state != NULL) {
2140 struct nfs_delegation *delegation;
2141
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002142 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002143 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002144 rcu_read_lock();
2145 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002146 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002147 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002148 rcu_read_unlock();
2149 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002150 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002151 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002152 switch (claim) {
2153 default:
2154 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002155 case NFS4_OPEN_CLAIM_PREVIOUS:
2156 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2157 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002158 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002159 case NFS4_OPEN_CLAIM_FH:
2160 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002161 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2162 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002163 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002164 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002165 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002166 &data->o_res.seq_res,
2167 task) != 0)
2168 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002169
2170 /* Set the create mode (note dependency on the session type) */
2171 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2172 if (data->o_arg.open_flags & O_EXCL) {
2173 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2174 if (nfs4_has_persistent_session(clp))
2175 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2176 else if (clp->cl_mvops->minor_version > 0)
2177 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2178 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002179 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002180unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002181 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002182 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002183out_no_action:
2184 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002185out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002186 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002187}
2188
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189static void nfs4_open_done(struct rpc_task *task, void *calldata)
2190{
2191 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002193 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002194
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002195 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002196 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002197
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002199 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2200 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002201 case S_IFREG:
2202 break;
2203 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002205 break;
2206 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002208 break;
2209 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002211 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002212 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002213 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002214 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2215 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002216 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002217 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002219
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002220static void nfs4_open_release(void *calldata)
2221{
2222 struct nfs4_opendata *data = calldata;
2223 struct nfs4_state *state = NULL;
2224
2225 /* If this request hasn't been cancelled, do nothing */
2226 if (data->cancelled == 0)
2227 goto out_free;
2228 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002229 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002230 goto out_free;
2231 /* In case we need an open_confirm, no cleanup! */
2232 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2233 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002234 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002235 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002236 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002238 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239}
2240
2241static const struct rpc_call_ops nfs4_open_ops = {
2242 .rpc_call_prepare = nfs4_open_prepare,
2243 .rpc_call_done = nfs4_open_done,
2244 .rpc_release = nfs4_open_release,
2245};
2246
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002247static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248{
David Howells2b0143b2015-03-17 22:25:59 +00002249 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002250 struct nfs_server *server = NFS_SERVER(dir);
2251 struct nfs_openargs *o_arg = &data->o_arg;
2252 struct nfs_openres *o_res = &data->o_res;
2253 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002254 struct rpc_message msg = {
2255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2256 .rpc_argp = o_arg,
2257 .rpc_resp = o_res,
2258 .rpc_cred = data->owner->so_cred,
2259 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002260 struct rpc_task_setup task_setup_data = {
2261 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002262 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 .callback_ops = &nfs4_open_ops,
2264 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002265 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002266 .flags = RPC_TASK_ASYNC,
2267 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002268 int status;
2269
Chuck Levera9c92d62013-08-09 12:48:18 -04002270 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002271 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002272 data->rpc_done = 0;
2273 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002274 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002275 data->is_recover = 0;
2276 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002277 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002278 data->is_recover = 1;
2279 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002280 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002281 if (IS_ERR(task))
2282 return PTR_ERR(task);
2283 status = nfs4_wait_for_completion_rpc_task(task);
2284 if (status != 0) {
2285 data->cancelled = 1;
2286 smp_wmb();
2287 } else
2288 status = data->rpc_status;
2289 rpc_put_task(task);
2290
2291 return status;
2292}
2293
2294static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2295{
David Howells2b0143b2015-03-17 22:25:59 +00002296 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002297 struct nfs_openres *o_res = &data->o_res;
2298 int status;
2299
2300 status = nfs4_run_open_task(data, 1);
2301 if (status != 0 || !data->rpc_done)
2302 return status;
2303
Trond Myklebust6926afd2012-01-07 13:22:46 -05002304 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2305
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002306 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2307 status = _nfs4_proc_open_confirm(data);
2308 if (status != 0)
2309 return status;
2310 }
2311
2312 return status;
2313}
2314
Trond Myklebustf3792d62014-07-10 08:54:32 -04002315/*
2316 * Additional permission checks in order to distinguish between an
2317 * open for read, and an open for execute. This works around the
2318 * fact that NFSv4 OPEN treats read and execute permissions as being
2319 * the same.
2320 * Note that in the non-execute case, we want to turn off permission
2321 * checking if we just created a new file (POSIX open() semantics).
2322 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002323static int nfs4_opendata_access(struct rpc_cred *cred,
2324 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002325 struct nfs4_state *state, fmode_t fmode,
2326 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002327{
2328 struct nfs_access_entry cache;
2329 u32 mask;
2330
2331 /* access call failed or for some reason the server doesn't
2332 * support any access modes -- defer access call until later */
2333 if (opendata->o_res.access_supported == 0)
2334 return 0;
2335
2336 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002337 /*
2338 * Use openflags to check for exec, because fmode won't
2339 * always have FMODE_EXEC set when file open for exec.
2340 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002341 if (openflags & __FMODE_EXEC) {
2342 /* ONLY check for exec rights */
2343 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002344 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002345 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002346
2347 cache.cred = cred;
2348 cache.jiffies = jiffies;
2349 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2350 nfs_access_add_cache(state->inode, &cache);
2351
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002352 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002353 return 0;
2354
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002355 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002356}
2357
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002358/*
2359 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2360 */
2361static int _nfs4_proc_open(struct nfs4_opendata *data)
2362{
David Howells2b0143b2015-03-17 22:25:59 +00002363 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002364 struct nfs_server *server = NFS_SERVER(dir);
2365 struct nfs_openargs *o_arg = &data->o_arg;
2366 struct nfs_openres *o_res = &data->o_res;
2367 int status;
2368
2369 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002370 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002371 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002372 if (status != 0) {
2373 if (status == -NFS4ERR_BADNAME &&
2374 !(o_arg->open_flags & O_CREAT))
2375 return -ENOENT;
2376 return status;
2377 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002378
Trond Myklebust6926afd2012-01-07 13:22:46 -05002379 nfs_fattr_map_and_free_names(server, &data->f_attr);
2380
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002381 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002382 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002383 if (o_arg->open_flags & O_EXCL)
2384 data->file_created = 1;
2385 else if (o_res->cinfo.before != o_res->cinfo.after)
2386 data->file_created = 1;
2387 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002388 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2389 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002391 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002393 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002395 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2396 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002397 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust88ac6b72017-04-15 19:20:01 -04002398 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Andy Adamsond83217c2011-03-01 01:34:17 +00002402static int nfs4_recover_expired_lease(struct nfs_server *server)
2403{
2404 return nfs4_client_recover_expired_lease(server->nfs_client);
2405}
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407/*
2408 * OPEN_EXPIRED:
2409 * reclaim state on the server after a network partition.
2410 * Assumes caller holds the appropriate lock
2411 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002412static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002414 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002415 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002417 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002418 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002419 if (IS_ERR(opendata))
2420 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002421 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002422 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002423 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002424 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002425 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002428static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002429{
Trond Myklebust539cd032007-06-05 11:46:42 -04002430 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002431 struct nfs4_exception exception = { };
2432 int err;
2433
2434 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002435 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002436 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002437 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2438 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002439 switch (err) {
2440 default:
2441 goto out;
2442 case -NFS4ERR_GRACE:
2443 case -NFS4ERR_DELAY:
2444 nfs4_handle_exception(server, err, &exception);
2445 err = 0;
2446 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002447 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002448out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002449 return err;
2450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2453{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002455 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Trond Myklebust864472e2006-01-03 09:55:15 +01002457 ctx = nfs4_state_find_open_context(state);
2458 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002459 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002460 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002461 put_nfs_open_context(ctx);
2462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
Trond Myklebust41020b62016-09-22 13:38:58 -04002465static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2466 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002467{
Trond Myklebust41020b62016-09-22 13:38:58 -04002468 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002469 write_seqlock(&state->seqlock);
2470 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2471 write_sequnlock(&state->seqlock);
2472 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2473}
2474
2475static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2476{
2477 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002478 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002479}
2480
2481static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2482{
2483 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2484 nfs40_clear_delegation_stateid(state);
2485 return nfs4_open_expired(sp, state);
2486}
2487
Trond Myklebust45870d62016-09-22 13:38:59 -04002488static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2489 nfs4_stateid *stateid,
2490 struct rpc_cred *cred)
2491{
2492 return -NFS4ERR_BAD_STATEID;
2493}
2494
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002495#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002496static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2497 nfs4_stateid *stateid,
2498 struct rpc_cred *cred)
2499{
2500 int status;
2501
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002502 switch (stateid->type) {
2503 default:
2504 break;
2505 case NFS4_INVALID_STATEID_TYPE:
2506 case NFS4_SPECIAL_STATEID_TYPE:
2507 return -NFS4ERR_BAD_STATEID;
2508 case NFS4_REVOKED_STATEID_TYPE:
2509 goto out_free;
2510 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002511
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002512 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002513 switch (status) {
2514 case -NFS4ERR_EXPIRED:
2515 case -NFS4ERR_ADMIN_REVOKED:
2516 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002517 break;
2518 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002519 return status;
2520 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002521out_free:
2522 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002523 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002524 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002525}
2526
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002527static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002528{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002529 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002530 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002531 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002532 struct rpc_cred *cred;
2533 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002534
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002535 /* Get the delegation credential for use by test/free_stateid */
2536 rcu_read_lock();
2537 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002538 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002539 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540 return;
2541 }
2542
2543 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaf412412018-09-05 14:07:14 -04002544 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002545 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002546 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002547 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002548 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002549
Trond Myklebustaf412412018-09-05 14:07:14 -04002550 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2551 &delegation->flags)) {
2552 rcu_read_unlock();
2553 return;
2554 }
2555
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002556 cred = get_rpccred(delegation->cred);
2557 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002558 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002559 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002560 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002561 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002562
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002563 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002564}
2565
2566/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002567 * nfs41_check_expired_locks - possibly free a lock stateid
2568 *
2569 * @state: NFSv4 state for an inode
2570 *
2571 * Returns NFS_OK if recovery for this stateid is now finished.
2572 * Otherwise a negative NFS4ERR value is returned.
2573 */
2574static int nfs41_check_expired_locks(struct nfs4_state *state)
2575{
2576 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002577 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002578 struct nfs_server *server = NFS_SERVER(state->inode);
2579
2580 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2581 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002582
2583 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002584 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2585 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2586 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2587
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002588 atomic_inc(&lsp->ls_count);
2589 spin_unlock(&state->state_lock);
2590
2591 nfs4_put_lock_state(prev);
2592 prev = lsp;
2593
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002594 status = nfs41_test_and_free_expired_stateid(server,
2595 &lsp->ls_stateid,
2596 cred);
2597 trace_nfs4_test_lock_stateid(state, lsp, status);
2598 if (status == -NFS4ERR_EXPIRED ||
2599 status == -NFS4ERR_BAD_STATEID) {
2600 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002601 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002602 if (!recover_lost_locks)
2603 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2604 } else if (status != NFS_OK) {
2605 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002606 nfs4_put_lock_state(prev);
2607 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002608 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002609 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002610 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002611 }
2612 spin_unlock(&state->state_lock);
2613 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002614out:
2615 return ret;
2616}
2617
2618/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002619 * nfs41_check_open_stateid - possibly free an open stateid
2620 *
2621 * @state: NFSv4 state for an inode
2622 *
2623 * Returns NFS_OK if recovery for this stateid is now finished.
2624 * Otherwise a negative NFS4ERR value is returned.
2625 */
2626static int nfs41_check_open_stateid(struct nfs4_state *state)
2627{
2628 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002629 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002630 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002631 int status;
2632
Trond Myklebustb134fc42016-09-22 13:39:16 -04002633 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2635 if (nfs4_have_delegation(state->inode, state->state))
2636 return NFS_OK;
2637 return -NFS4ERR_OPENMODE;
2638 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002639 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002640 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002641 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002642 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002643 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002644 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2645 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2646 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002647 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002648 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002649 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002650 if (status != NFS_OK)
2651 return status;
2652 if (nfs_open_stateid_recover_openmode(state))
2653 return -NFS4ERR_OPENMODE;
2654 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002655}
2656
2657static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2658{
Chuck Levereb64cf92012-07-11 16:30:05 -04002659 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002660
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002661 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002662 status = nfs41_check_expired_locks(state);
2663 if (status != NFS_OK)
2664 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002665 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002666 if (status != NFS_OK)
2667 status = nfs4_open_expired(sp, state);
2668 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002669}
2670#endif
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002673 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2674 * fields corresponding to attributes that were used to store the verifier.
2675 * Make sure we clobber those fields in the later setattr call
2676 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002677static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2678 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002679{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002680 const u32 *attrset = opendata->o_res.attrset;
2681
2682 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002683 !(sattr->ia_valid & ATTR_ATIME_SET))
2684 sattr->ia_valid |= ATTR_ATIME;
2685
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002686 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002687 !(sattr->ia_valid & ATTR_MTIME_SET))
2688 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002689
2690 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddington56379492017-01-24 11:34:20 -05002691 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2692 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002693 sattr->ia_valid &= ~ATTR_MODE;
2694
2695 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2696 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002697}
2698
Trond Myklebustc21443c2013-02-07 14:26:21 -05002699static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2700 fmode_t fmode,
2701 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002702 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002703{
2704 struct nfs4_state_owner *sp = opendata->owner;
2705 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002706 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002707 struct nfs4_state *state;
2708 unsigned int seq;
2709 int ret;
2710
2711 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2712
2713 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002714 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715 goto out;
2716
Trond Myklebustb05c4602018-05-22 11:17:16 -04002717 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002718 ret = PTR_ERR(state);
2719 if (IS_ERR(state))
2720 goto out;
Trond Myklebust04653392017-02-08 11:29:46 -05002721 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002722 if (server->caps & NFS_CAP_POSIX_LOCK)
2723 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002724 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2725 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002726
Trond Myklebust275bb302013-05-29 13:11:28 -04002727 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002728 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002729 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002730 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002731 alias = d_exact_alias(dentry, state->inode);
2732 if (!alias)
2733 alias = d_splice_alias(igrab(state->inode), dentry);
2734 /* d_splice_alias() can't fail here - it's a non-directory */
2735 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002736 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002737 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002738 }
2739 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002740 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002741 }
2742
Trond Myklebustc21443c2013-02-07 14:26:21 -05002743 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2744 if (ret != 0)
2745 goto out;
2746
David Howells2b0143b2015-03-17 22:25:59 +00002747 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002748 nfs_inode_attach_open_context(ctx);
2749 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2750 nfs4_schedule_stateid_recovery(server, state);
2751 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002752out:
Olga Kornievskaia22a87112019-03-19 12:12:13 -04002753 if (!opendata->cancelled)
2754 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002755 return ret;
2756}
2757
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002758/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002759 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 */
Andy Adamson82be4172012-05-23 05:02:35 -04002761static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002762 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002763 int flags,
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002764 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002765 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 struct nfs4_state_owner *sp;
2768 struct nfs4_state *state = NULL;
2769 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002770 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002771 struct dentry *dentry = ctx->dentry;
2772 struct rpc_cred *cred = ctx->cred;
2773 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2774 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002775 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002776 struct iattr *sattr = c->sattr;
2777 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002778 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002779 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002783 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2784 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2786 goto out_err;
2787 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002788 status = nfs4_recover_expired_lease(server);
2789 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002790 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002791 if (d_really_is_positive(dentry))
2792 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002793 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002794 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002795 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002796 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2797 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002798 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002799 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
David Quigley14c43f72013-05-22 12:50:43 -04002801 if (label) {
2802 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2803 if (IS_ERR(olabel)) {
2804 status = PTR_ERR(olabel);
2805 goto err_opendata_put;
2806 }
2807 }
2808
Trond Myklebuste911b812014-03-26 13:24:37 -07002809 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2810 if (!opendata->f_attr.mdsthreshold) {
2811 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2812 if (!opendata->f_attr.mdsthreshold)
2813 goto err_free_label;
2814 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002815 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002816 }
David Howells2b0143b2015-03-17 22:25:59 +00002817 if (d_really_is_positive(dentry))
2818 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002819
Trond Myklebust3efb9722013-05-29 13:17:04 -04002820 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002821 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002822 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002823 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002824
NeilBrownefcbc042015-07-30 13:00:56 +10002825 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002826 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002827 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002828 /*
2829 * send create attributes which was not set by open
2830 * with an extra setattr.
2831 */
2832 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2833 nfs_fattr_init(opendata->o_res.f_attr);
2834 status = nfs4_do_setattr(state->inode, cred,
2835 opendata->o_res.f_attr, sattr,
2836 state, label, olabel);
2837 if (status == 0) {
2838 nfs_setattr_update_inode(state->inode, sattr,
2839 opendata->o_res.f_attr);
2840 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2841 }
David Quigley1775fd32013-05-22 12:50:42 -04002842 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002843 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002844 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002845 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002846
Trond Myklebuste911b812014-03-26 13:24:37 -07002847 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002848 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002849 opendata->f_attr.mdsthreshold = NULL;
2850 }
Andy Adamson82be4172012-05-23 05:02:35 -04002851
David Quigley14c43f72013-05-22 12:50:43 -04002852 nfs4_label_free(olabel);
2853
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002854 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002857err_free_label:
2858 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002859err_opendata_put:
2860 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002861err_put_state_owner:
2862 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return status;
2865}
2866
2867
Andy Adamson82be4172012-05-23 05:02:35 -04002868static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002869 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002870 int flags,
2871 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002872 struct nfs4_label *label,
2873 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002875 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 struct nfs4_exception exception = { };
2877 struct nfs4_state *res;
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002878 struct nfs4_open_createattrs c = {
2879 .label = label,
2880 .sattr = sattr,
2881 .verf = {
2882 [0] = (__u32)jiffies,
2883 [1] = (__u32)current->pid,
2884 },
2885 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 int status;
2887
2888 do {
Trond Myklebust2d9b39d2017-11-06 15:28:03 -05002889 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002890 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002891 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (status == 0)
2893 break;
2894 /* NOTE: BAD_SEQID means the server and client disagree about the
2895 * book-keeping w.r.t. state-changing operations
2896 * (OPEN/CLOSE/LOCK/LOCKU...)
2897 * It is actually a sign of a bug on the client or on the server.
2898 *
2899 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002900 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 * have unhashed the old state_owner for us, and that we can
2902 * therefore safely retry using a new one. We should still warn
2903 * the user though...
2904 */
2905 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002906 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002907 " returned a bad sequence-id error!\n",
2908 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 exception.retry = 1;
2910 continue;
2911 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002912 /*
2913 * BAD_STATEID on OPEN means that the server cancelled our
2914 * state before it received the OPEN_CONFIRM.
2915 * Recover by retrying the request as per the discussion
2916 * on Page 181 of RFC3530.
2917 */
2918 if (status == -NFS4ERR_BAD_STATEID) {
2919 exception.retry = 1;
2920 continue;
2921 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002922 if (status == -EAGAIN) {
2923 /* We must have found a delegation */
2924 exception.retry = 1;
2925 continue;
2926 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002927 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2928 continue;
2929 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 status, &exception));
2931 } while (exception.retry);
2932 return res;
2933}
2934
Trond Myklebust8487c472016-06-26 08:44:35 -04002935static int _nfs4_do_setattr(struct inode *inode,
2936 struct nfs_setattrargs *arg,
2937 struct nfs_setattrres *res,
2938 struct rpc_cred *cred,
2939 struct nfs4_state *state)
2940{
2941 struct nfs_server *server = NFS_SERVER(inode);
2942 struct rpc_message msg = {
2943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2944 .rpc_argp = arg,
2945 .rpc_resp = res,
2946 .rpc_cred = cred,
2947 };
2948 struct rpc_cred *delegation_cred = NULL;
2949 unsigned long timestamp = jiffies;
2950 fmode_t fmode;
2951 bool truncate;
2952 int status;
2953
2954 nfs_fattr_init(res->fattr);
2955
2956 /* Servers should only apply open mode checks for file size changes */
2957 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2958 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2959
2960 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2961 /* Use that stateid */
2962 } else if (truncate && state != NULL) {
2963 struct nfs_lockowner lockowner = {
2964 .l_owner = current->files,
2965 .l_pid = current->tgid,
2966 };
2967 if (!nfs4_valid_open_stateid(state))
2968 return -EBADF;
2969 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2970 &arg->stateid, &delegation_cred) == -EIO)
2971 return -EBADF;
2972 } else
2973 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2974 if (delegation_cred)
2975 msg.rpc_cred = delegation_cred;
2976
2977 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2978
2979 put_rpccred(delegation_cred);
2980 if (status == 0 && state != NULL)
2981 renew_lease(server, timestamp);
2982 trace_nfs4_setattr(inode, &arg->stateid, status);
2983 return status;
2984}
2985
2986static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2987 struct nfs_fattr *fattr, struct iattr *sattr,
2988 struct nfs4_state *state, struct nfs4_label *ilabel,
2989 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002991 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002993 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .iap = sattr,
2995 .server = server,
2996 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002997 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
2999 struct nfs_setattrres res = {
3000 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003001 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 .server = server,
3003 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003004 struct nfs4_exception exception = {
3005 .state = state,
3006 .inode = inode,
3007 .stateid = &arg.stateid,
3008 };
3009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
David Quigleyaa9c2662013-05-22 12:50:44 -04003011 arg.bitmask = nfs4_bitmask(server, ilabel);
3012 if (ilabel)
3013 arg.bitmask = nfs4_bitmask(server, olabel);
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003016 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003017 switch (err) {
3018 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003019 if (!(sattr->ia_valid & ATTR_SIZE)) {
3020 pr_warn_once("NFSv4: server %s is incorrectly "
3021 "applying open mode checks to "
3022 "a SETATTR that is not "
3023 "changing file size.\n",
3024 server->nfs_client->cl_hostname);
3025 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003026 if (state && !(state->state & FMODE_WRITE)) {
3027 err = -EBADF;
3028 if (sattr->ia_valid & ATTR_OPEN)
3029 err = -EACCES;
3030 goto out;
3031 }
3032 }
3033 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003035out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return err;
3037}
3038
Peng Tao500d7012015-09-22 11:35:22 +08003039static bool
3040nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3041{
3042 if (inode == NULL || !nfs_have_layout(inode))
3043 return false;
3044
3045 return pnfs_wait_on_layoutreturn(inode, task);
3046}
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048struct nfs4_closedata {
3049 struct inode *inode;
3050 struct nfs4_state *state;
3051 struct nfs_closeargs arg;
3052 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003053 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003054 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00003055 bool roc;
3056 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057};
3058
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003059static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003060{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003061 struct nfs4_closedata *calldata = data;
3062 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003063 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003064
Fred Isamanf7e89172011-01-06 11:36:32 +00003065 if (calldata->roc)
3066 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07003067 nfs4_put_open_state(calldata->state);
3068 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003069 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003070 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003071 kfree(calldata);
3072}
3073
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003074static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003076 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003079 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Chuck Levera3ca5652012-03-01 17:00:40 -05003081 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003082 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3083 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003084 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 /* hmm. we are done with the inode, and in the process of freeing
3086 * the state_owner. we keep this around to process errors
3087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 switch (task->tk_status) {
3089 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003090 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003091 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00003092 pnfs_roc_set_barrier(state->inode,
3093 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003094 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003095 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003096 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003098 case -NFS4ERR_EXPIRED:
3099 nfs4_free_revoked_stateid(server,
3100 &calldata->arg.stateid,
3101 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003102 case -NFS4ERR_OLD_STATEID:
3103 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003104 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003105 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003106 rpc_restart_call_prepare(task);
3107 goto out_release;
3108 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003109 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003112 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003113 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003114 goto out_release;
3115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003117 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3118 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003119out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003120 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003121 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003122 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003125static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003127 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003128 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003129 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003130 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003131 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003132
Chuck Levera3ca5652012-03-01 17:00:40 -05003133 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003134 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003135 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003136
Trond Myklebust88069f72009-12-08 08:33:16 -05003137 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003138 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003139 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3140 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3141 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003142 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003143 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003144 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003145 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003146 if (state->n_rdonly == 0)
3147 call_close |= is_rdonly;
3148 else if (is_rdonly)
3149 calldata->arg.fmode |= FMODE_READ;
3150 if (state->n_wronly == 0)
3151 call_close |= is_wronly;
3152 else if (is_wronly)
3153 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003154 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3155 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003156 } else if (is_rdwr)
3157 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3158
Trond Myklebust5cc78612016-11-14 11:19:56 -05003159 if (!nfs4_valid_open_stateid(state) ||
3160 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003161 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003162 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003163
3164 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003165 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003166 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003167 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003168
Peng Tao500d7012015-09-22 11:35:22 +08003169 if (nfs4_wait_on_layoutreturn(inode, task)) {
3170 nfs_release_seqid(calldata->arg.seqid);
3171 goto out_wait;
3172 }
3173
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003174 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003175 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003176 if (calldata->roc)
3177 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3178
Trond Myklebust6ae37332015-01-30 14:21:14 -05003179 calldata->arg.share_access =
3180 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3181 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003182
Trond Myklebust516a6af2005-10-27 22:12:41 -04003183 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003184 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003185 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003186 &calldata->arg.seq_args,
3187 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003188 task) != 0)
3189 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003190 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003191 return;
3192out_no_action:
3193 task->tk_action = NULL;
3194out_wait:
3195 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003198static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003199 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003200 .rpc_call_done = nfs4_close_done,
3201 .rpc_release = nfs4_free_closedata,
3202};
3203
Peng Taofe08c542014-07-03 13:05:01 +08003204static bool nfs4_roc(struct inode *inode)
3205{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05003206 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08003207 return false;
Peng Taofe08c542014-07-03 13:05:01 +08003208 return pnfs_roc(inode);
3209}
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211/*
3212 * It is possible for data to be read/written from a mem-mapped file
3213 * after the sys_close call (which hits the vfs layer as a flush).
3214 * This means that we can't safely call nfsv4 close on a file until
3215 * the inode is cleared. This in turn means that we are not good
3216 * NFSv4 citizens - we do not indicate to the server to update the file's
3217 * share state even when we are done with one of the three share
3218 * stateid's in the inode.
3219 *
3220 * NOTE: Caller must be holding the sp->so_owner semaphore!
3221 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003222int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003224 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003225 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003227 struct nfs4_state_owner *sp = state->owner;
3228 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003229 struct rpc_message msg = {
3230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3231 .rpc_cred = state->owner->so_cred,
3232 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003233 struct rpc_task_setup task_setup_data = {
3234 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003235 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003236 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003237 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003238 .flags = RPC_TASK_ASYNC,
3239 };
Trond Myklebust95121352005-10-18 14:20:12 -07003240 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003242 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3243 &task_setup_data.rpc_client, &msg);
3244
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003245 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003247 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003248 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003249 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003251 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003253 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3254 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003255 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003256 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003257 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04003258 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003259 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003260 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003261 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08003262 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04003263 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003264
Trond Myklebust1174dd12010-12-21 10:52:24 -05003265 msg.rpc_argp = &calldata->arg;
3266 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003267 task_setup_data.callback_data = calldata;
3268 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003269 if (IS_ERR(task))
3270 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003271 status = 0;
3272 if (wait)
3273 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003274 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003275 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003276out_free_calldata:
3277 kfree(calldata);
3278out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003279 nfs4_put_open_state(state);
3280 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
Trond Myklebust2b484292010-09-17 10:56:51 -04003284static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003285nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3286 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003289 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3290
3291 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Trond Myklebust565277f2007-10-15 18:17:53 -04003293 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003294 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003295
3296 nfs4_label_release_security(label);
3297
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003298 if (IS_ERR(state))
3299 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003300 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Trond Myklebust1185a552009-12-03 15:54:02 -05003303static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003304{
3305 if (ctx->state == NULL)
3306 return;
3307 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003308 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003309 else
Al Viro643168c2011-06-22 18:20:23 -04003310 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003311}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Trond Myklebustb944dba2013-11-04 15:20:20 -05003313#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3314#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003315#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3318{
Kinglong Mee8c612822015-08-26 21:12:58 +08003319 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003320 struct nfs4_server_caps_arg args = {
3321 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003322 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003323 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 struct nfs4_server_caps_res res = {};
3325 struct rpc_message msg = {
3326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003327 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 .rpc_resp = &res,
3329 };
3330 int status;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003331 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Kinglong Mee8c612822015-08-26 21:12:58 +08003333 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3334 FATTR4_WORD0_FH_EXPIRE_TYPE |
3335 FATTR4_WORD0_LINK_SUPPORT |
3336 FATTR4_WORD0_SYMLINK_SUPPORT |
3337 FATTR4_WORD0_ACLSUPPORT;
3338 if (minorversion)
3339 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3340
Bryan Schumaker7c513052011-03-24 17:12:24 +00003341 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003343 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003344 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003345 case 0:
3346 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3347 res.attr_bitmask[2] = 0;
3348 break;
3349 case 1:
3350 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3351 break;
3352 case 2:
3353 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003356 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3357 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3358 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3359 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003360 NFS_CAP_CTIME|NFS_CAP_MTIME|
3361 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003362 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3363 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 server->caps |= NFS_CAP_ACLS;
3365 if (res.has_links != 0)
3366 server->caps |= NFS_CAP_HARDLINKS;
3367 if (res.has_symlinks != 0)
3368 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003369 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3370 server->caps |= NFS_CAP_FILEID;
3371 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3372 server->caps |= NFS_CAP_MODE;
3373 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3374 server->caps |= NFS_CAP_NLINK;
3375 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3376 server->caps |= NFS_CAP_OWNER;
3377 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3378 server->caps |= NFS_CAP_OWNER_GROUP;
3379 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3380 server->caps |= NFS_CAP_ATIME;
3381 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3382 server->caps |= NFS_CAP_CTIME;
3383 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3384 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003385#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3386 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3387 server->caps |= NFS_CAP_SECURITY_LABEL;
3388#endif
3389 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3390 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003391 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003392
Trond Myklebusta65318b2009-03-11 14:10:28 -04003393 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3394 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3395 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003396 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustfca22d52017-05-09 15:47:15 -04003397
3398 /* Avoid a regression due to buggy server */
3399 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3400 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003401 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3402 sizeof(server->exclcreat_bitmask));
Trond Myklebustfca22d52017-05-09 15:47:15 -04003403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003405 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return status;
3409}
3410
Trond Myklebust55a97592006-06-09 09:34:19 -04003411int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412{
3413 struct nfs4_exception exception = { };
3414 int err;
3415 do {
3416 err = nfs4_handle_exception(server,
3417 _nfs4_server_capabilities(server, fhandle),
3418 &exception);
3419 } while (exception.retry);
3420 return err;
3421}
3422
3423static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3424 struct nfs_fsinfo *info)
3425{
David Quigleyaa9c2662013-05-22 12:50:44 -04003426 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003428 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 };
3430 struct nfs4_lookup_res res = {
3431 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003432 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 .fh = fhandle,
3434 };
3435 struct rpc_message msg = {
3436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3437 .rpc_argp = &args,
3438 .rpc_resp = &res,
3439 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003440
David Quigleyaa9c2662013-05-22 12:50:44 -04003441 bitmask[0] = nfs4_fattr_bitmap[0];
3442 bitmask[1] = nfs4_fattr_bitmap[1];
3443 /*
3444 * Process the label in the upcoming getfattr
3445 */
3446 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3447
Trond Myklebust0e574af2005-10-27 22:12:38 -04003448 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003449 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450}
3451
3452static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3453 struct nfs_fsinfo *info)
3454{
3455 struct nfs4_exception exception = { };
3456 int err;
3457 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003458 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003459 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003460 switch (err) {
3461 case 0:
3462 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003463 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003464 default:
3465 err = nfs4_handle_exception(server, err, &exception);
3466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return err;
3470}
3471
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003472static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3473 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3474{
Trond Myklebustc2190662013-08-26 19:23:04 -04003475 struct rpc_auth_create_args auth_args = {
3476 .pseudoflavor = flavor,
3477 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003478 struct rpc_auth *auth;
3479 int ret;
3480
Trond Myklebustc2190662013-08-26 19:23:04 -04003481 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003482 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003483 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003484 goto out;
3485 }
3486 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003487out:
3488 return ret;
3489}
3490
Chuck Lever9a744ba2013-03-16 15:56:02 -04003491/*
3492 * Retry pseudoroot lookup with various security flavors. We do this when:
3493 *
3494 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3495 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3496 *
3497 * Returns zero on success, or a negative NFS4ERR value, or a
3498 * negative errno value.
3499 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003500static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003501 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003503 /* Per 3530bis 15.33.5 */
3504 static const rpc_authflavor_t flav_array[] = {
3505 RPC_AUTH_GSS_KRB5P,
3506 RPC_AUTH_GSS_KRB5I,
3507 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003508 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003509 RPC_AUTH_NULL,
3510 };
3511 int status = -EPERM;
3512 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003514 if (server->auth_info.flavor_len > 0) {
3515 /* try each flavor specified by user */
3516 for (i = 0; i < server->auth_info.flavor_len; i++) {
3517 status = nfs4_lookup_root_sec(server, fhandle, info,
3518 server->auth_info.flavors[i]);
3519 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3520 continue;
3521 break;
3522 }
3523 } else {
3524 /* no flavors specified by user, try default list */
3525 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3526 status = nfs4_lookup_root_sec(server, fhandle, info,
3527 flav_array[i]);
3528 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3529 continue;
3530 break;
3531 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003532 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003533
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003534 /*
3535 * -EACCESS could mean that the user doesn't have correct permissions
3536 * to access the mount. It could also mean that we tried to mount
3537 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3538 * existing mount programs don't handle -EACCES very well so it should
3539 * be mapped to -EPERM instead.
3540 */
3541 if (status == -EACCES)
3542 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003543 return status;
3544}
3545
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003546/**
3547 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3548 * @server: initialized nfs_server handle
3549 * @fhandle: we fill in the pseudo-fs root file handle
3550 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003551 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003552 *
3553 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003554 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003555int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003556 struct nfs_fsinfo *info,
3557 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003558{
Andre Przywarac7757072015-04-23 17:17:40 +01003559 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003560
Andre Przywarac7757072015-04-23 17:17:40 +01003561 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003562 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003563
3564 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003565 status = server->nfs_client->cl_mvops->find_root_sec(server,
3566 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if (status == 0)
3569 status = nfs4_server_capabilities(server, fhandle);
3570 if (status == 0)
3571 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003572
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003573 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Bryan Schumakerbae36242012-05-10 15:07:31 -04003576static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3577 struct nfs_fsinfo *info)
3578{
3579 int error;
3580 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003581 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003582
3583 error = nfs4_server_capabilities(server, mntfh);
3584 if (error < 0) {
3585 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3586 return error;
3587 }
3588
David Quigley14c43f72013-05-22 12:50:43 -04003589 label = nfs4_label_alloc(server, GFP_KERNEL);
3590 if (IS_ERR(label))
3591 return PTR_ERR(label);
3592
David Quigley1775fd32013-05-22 12:50:42 -04003593 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003594 if (error < 0) {
3595 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003596 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003597 }
3598
3599 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3600 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3601 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3602
David Quigley14c43f72013-05-22 12:50:43 -04003603err_free_label:
3604 nfs4_label_free(label);
3605
Bryan Schumakerbae36242012-05-10 15:07:31 -04003606 return error;
3607}
3608
Manoj Naik6b97fd32006-06-09 09:34:29 -04003609/*
3610 * Get locations and (maybe) other attributes of a referral.
3611 * Note that we'll actually follow the referral later when
3612 * we detect fsid mismatch in inode revalidation
3613 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003614static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3615 const struct qstr *name, struct nfs_fattr *fattr,
3616 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003617{
3618 int status = -ENOMEM;
3619 struct page *page = NULL;
3620 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003621
3622 page = alloc_page(GFP_KERNEL);
3623 if (page == NULL)
3624 goto out;
3625 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3626 if (locations == NULL)
3627 goto out;
3628
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003629 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003630 if (status != 0)
3631 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003632
3633 /*
3634 * If the fsid didn't change, this is a migration event, not a
3635 * referral. Cause us to drop into the exception handler, which
3636 * will kick off migration recovery.
3637 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003638 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003639 dprintk("%s: server did not return a different fsid for"
3640 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003641 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003642 goto out;
3643 }
Andy Adamson533eb462011-06-13 18:25:56 -04003644 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3645 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003646
Andy Adamson533eb462011-06-13 18:25:56 -04003647 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003648 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003649 memset(fhandle, 0, sizeof(struct nfs_fh));
3650out:
3651 if (page)
3652 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003653 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003654 return status;
3655}
3656
David Quigley1775fd32013-05-22 12:50:42 -04003657static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3658 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659{
3660 struct nfs4_getattr_arg args = {
3661 .fh = fhandle,
3662 .bitmask = server->attr_bitmask,
3663 };
3664 struct nfs4_getattr_res res = {
3665 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003666 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 .server = server,
3668 };
3669 struct rpc_message msg = {
3670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3671 .rpc_argp = &args,
3672 .rpc_resp = &res,
3673 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003674
3675 args.bitmask = nfs4_bitmask(server, label);
3676
Trond Myklebust0e574af2005-10-27 22:12:38 -04003677 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003678 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
David Quigley1775fd32013-05-22 12:50:42 -04003681static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3682 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
3684 struct nfs4_exception exception = { };
3685 int err;
3686 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003687 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3688 trace_nfs4_getattr(server, fhandle, fattr, err);
3689 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 &exception);
3691 } while (exception.retry);
3692 return err;
3693}
3694
3695/*
3696 * The file is not closed if it is opened due to the a request to change
3697 * the size of the file. The open call will not be needed once the
3698 * VFS layer lookup-intents are implemented.
3699 *
3700 * Close is called when the inode is destroyed.
3701 * If we haven't opened the file for O_WRONLY, we
3702 * need to in the size_change case to obtain a stateid.
3703 *
3704 * Got race?
3705 * Because OPEN is always done by name in nfsv4, it is
3706 * possible that we opened a different file by the same
3707 * name. We can recognize this race condition, but we
3708 * can't do anything about it besides returning an error.
3709 *
3710 * This will be fixed with VFS changes (lookup-intent).
3711 */
3712static int
3713nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3714 struct iattr *sattr)
3715{
David Howells2b0143b2015-03-17 22:25:59 +00003716 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003717 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003718 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003719 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 int status;
3721
Peng Tao88ac8152014-09-12 11:04:10 +08003722 if (pnfs_ld_layoutret_on_setattr(inode) &&
3723 sattr->ia_valid & ATTR_SIZE &&
3724 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003725 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003726
Trond Myklebust0e574af2005-10-27 22:12:38 -04003727 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Andy Adamson26699402012-05-30 16:12:24 -04003729 /* Deal with open(O_TRUNC) */
3730 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003731 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003732
3733 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003734 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003735 return 0;
3736
Trond Myklebustd5308382005-11-04 15:33:38 -05003737 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003738 if (sattr->ia_valid & ATTR_FILE) {
3739 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003740
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003741 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003742 if (ctx) {
3743 cred = ctx->cred;
3744 state = ctx->state;
3745 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003746 }
3747
David Quigley14c43f72013-05-22 12:50:43 -04003748 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3749 if (IS_ERR(label))
3750 return PTR_ERR(label);
3751
3752 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003753 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003754 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003755 nfs_setsecurity(inode, fattr, label);
3756 }
David Quigley14c43f72013-05-22 12:50:43 -04003757 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 return status;
3759}
3760
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003761static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3762 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003763 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003764{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003765 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003766 int status;
3767 struct nfs4_lookup_arg args = {
3768 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003769 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003770 .name = name,
3771 };
3772 struct nfs4_lookup_res res = {
3773 .server = server,
3774 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003775 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003776 .fh = fhandle,
3777 };
3778 struct rpc_message msg = {
3779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3780 .rpc_argp = &args,
3781 .rpc_resp = &res,
3782 };
3783
David Quigleyaa9c2662013-05-22 12:50:44 -04003784 args.bitmask = nfs4_bitmask(server, label);
3785
David Howells2b3de442006-08-22 20:06:09 -04003786 nfs_fattr_init(fattr);
3787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003789 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 dprintk("NFS reply lookup: %d\n", status);
3791 return status;
3792}
3793
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003794static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003795{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003796 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003797 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003798 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3799 fattr->nlink = 2;
3800}
3801
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003802static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003803 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003804 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003805{
3806 struct nfs4_exception exception = { };
3807 struct rpc_clnt *client = *clnt;
3808 int err;
3809 do {
David Quigley1775fd32013-05-22 12:50:42 -04003810 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003811 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003812 switch (err) {
3813 case -NFS4ERR_BADNAME:
3814 err = -ENOENT;
3815 goto out;
3816 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003817 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003818 if (err == -NFS4ERR_MOVED)
3819 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003820 goto out;
3821 case -NFS4ERR_WRONGSEC:
3822 err = -EPERM;
3823 if (client != *clnt)
3824 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003825 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003826 if (IS_ERR(client))
3827 return PTR_ERR(client);
3828
3829 exception.retry = 1;
3830 break;
3831 default:
3832 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3833 }
3834 } while (exception.retry);
3835
3836out:
3837 if (err == 0)
3838 *clnt = client;
3839 else if (client != *clnt)
3840 rpc_shutdown_client(client);
3841
3842 return err;
3843}
3844
Al Virobeffb8f2016-07-20 16:34:42 -04003845static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003846 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3847 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003849 int status;
3850 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003851
David Quigley1775fd32013-05-22 12:50:42 -04003852 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003853 if (client != NFS_CLIENT(dir)) {
3854 rpc_shutdown_client(client);
3855 nfs_fixup_secinfo_attributes(fattr);
3856 }
3857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
3859
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003860struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003861nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003862 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3863{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003864 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003865 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003866
David Quigley1775fd32013-05-22 12:50:42 -04003867 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003868 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003869 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003870 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003871}
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3874{
Trond Myklebust76b32992007-08-10 17:45:11 -04003875 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 struct nfs4_accessargs args = {
3877 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003878 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003880 struct nfs4_accessres res = {
3881 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003882 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct rpc_message msg = {
3884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3885 .rpc_argp = &args,
3886 .rpc_resp = &res,
3887 .rpc_cred = entry->cred,
3888 };
3889 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003890 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 /*
3893 * Determine which access bits we want to ask for...
3894 */
3895 if (mode & MAY_READ)
3896 args.access |= NFS4_ACCESS_READ;
3897 if (S_ISDIR(inode->i_mode)) {
3898 if (mode & MAY_WRITE)
3899 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3900 if (mode & MAY_EXEC)
3901 args.access |= NFS4_ACCESS_LOOKUP;
3902 } else {
3903 if (mode & MAY_WRITE)
3904 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3905 if (mode & MAY_EXEC)
3906 args.access |= NFS4_ACCESS_EXECUTE;
3907 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003908
3909 res.fattr = nfs_alloc_fattr();
3910 if (res.fattr == NULL)
3911 return -ENOMEM;
3912
Bryan Schumaker7c513052011-03-24 17:12:24 +00003913 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003915 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003916 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003918 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return status;
3920}
3921
3922static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3923{
3924 struct nfs4_exception exception = { };
3925 int err;
3926 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003927 err = _nfs4_proc_access(inode, entry);
3928 trace_nfs4_access(inode, err);
3929 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 &exception);
3931 } while (exception.retry);
3932 return err;
3933}
3934
3935/*
3936 * TODO: For the time being, we don't try to get any attributes
3937 * along with any of the zero-copy operations READ, READDIR,
3938 * READLINK, WRITE.
3939 *
3940 * In the case of the first three, we want to put the GETATTR
3941 * after the read-type operation -- this is because it is hard
3942 * to predict the length of a GETATTR response in v4, and thus
3943 * align the READ data correctly. This means that the GETATTR
3944 * may end up partially falling into the page cache, and we should
3945 * shift it into the 'tail' of the xdr_buf before processing.
3946 * To do this efficiently, we need to know the total length
3947 * of data received, which doesn't seem to be available outside
3948 * of the RPC layer.
3949 *
3950 * In the case of WRITE, we also want to put the GETATTR after
3951 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003952 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 *
3954 * Both of these changes to the XDR layer would in fact be quite
3955 * minor, but I decided to leave them for a subsequent patch.
3956 */
3957static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3958 unsigned int pgbase, unsigned int pglen)
3959{
3960 struct nfs4_readlink args = {
3961 .fh = NFS_FH(inode),
3962 .pgbase = pgbase,
3963 .pglen = pglen,
3964 .pages = &page,
3965 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003966 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 struct rpc_message msg = {
3968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3969 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003970 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 };
3972
Bryan Schumaker7c513052011-03-24 17:12:24 +00003973 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974}
3975
3976static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3977 unsigned int pgbase, unsigned int pglen)
3978{
3979 struct nfs4_exception exception = { };
3980 int err;
3981 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003982 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3983 trace_nfs4_readlink(inode, err);
3984 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 &exception);
3986 } while (exception.retry);
3987 return err;
3988}
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003991 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993static int
3994nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003995 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
David Quigleyaa9c2662013-05-22 12:50:44 -04003997 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003998 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 int status = 0;
4001
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004002 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
4003 if (IS_ERR(ctx))
4004 return PTR_ERR(ctx);
4005
David Quigleyaa9c2662013-05-22 12:50:44 -04004006 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4007
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004008 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004009 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (IS_ERR(state)) {
4011 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004012 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004015 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004016 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 return status;
4018}
4019
Al Virobeffb8f2016-07-20 16:34:42 -04004020static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
Trond Myklebust16e42952005-10-27 22:12:44 -04004022 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004023 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004025 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004027 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004028 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004029 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4032 .rpc_argp = &args,
4033 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004035 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004036
Bryan Schumaker7c513052011-03-24 17:12:24 +00004037 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004038 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004039 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return status;
4041}
4042
Al Virobeffb8f2016-07-20 16:34:42 -04004043static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
4045 struct nfs4_exception exception = { };
4046 int err;
4047 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004048 err = _nfs4_proc_remove(dir, name);
4049 trace_nfs4_remove(dir, name, err);
4050 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 &exception);
4052 } while (exception.retry);
4053 return err;
4054}
4055
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004056static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004058 struct nfs_server *server = NFS_SERVER(dir);
4059 struct nfs_removeargs *args = msg->rpc_argp;
4060 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004062 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004064 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004065
4066 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004069static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4070{
Al Viro884be172016-04-28 23:56:31 -04004071 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004072 &data->args.seq_args,
4073 &data->res.seq_res,
4074 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004077static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004079 struct nfs_unlinkdata *data = task->tk_calldata;
4080 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004081
Trond Myklebust14516c32010-07-31 14:29:06 -04004082 if (!nfs4_sequence_done(task, &res->seq_res))
4083 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004084 if (nfs4_async_handle_error(task, res->server, NULL,
4085 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004086 return 0;
4087 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004088 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
Jeff Laytond3d41522010-09-17 17:31:57 -04004091static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4092{
4093 struct nfs_server *server = NFS_SERVER(dir);
4094 struct nfs_renameargs *arg = msg->rpc_argp;
4095 struct nfs_renameres *res = msg->rpc_resp;
4096
4097 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004098 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004099 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004100}
4101
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004102static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4103{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004104 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4105 &data->args.seq_args,
4106 &data->res.seq_res,
4107 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004108}
4109
4110static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4111 struct inode *new_dir)
4112{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004113 struct nfs_renamedata *data = task->tk_calldata;
4114 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004115
4116 if (!nfs4_sequence_done(task, &res->seq_res))
4117 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004118 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004119 return 0;
4120
4121 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004122 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004123 return 1;
4124}
4125
Al Virobeffb8f2016-07-20 16:34:42 -04004126static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004128 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 struct nfs4_link_arg arg = {
4130 .fh = NFS_FH(inode),
4131 .dir_fh = NFS_FH(dir),
4132 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004133 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004135 struct nfs4_link_res res = {
4136 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004137 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 struct rpc_message msg = {
4140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4141 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004142 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004144 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Trond Myklebust136f2622010-04-16 16:22:49 -04004146 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004147 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004148 goto out;
4149
David Quigley14c43f72013-05-22 12:50:43 -04004150 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4151 if (IS_ERR(res.label)) {
4152 status = PTR_ERR(res.label);
4153 goto out;
4154 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004155 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004156
Bryan Schumaker7c513052011-03-24 17:12:24 +00004157 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004158 if (!status) {
4159 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004160 status = nfs_post_op_update_inode(inode, res.fattr);
4161 if (!status)
4162 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004163 }
David Quigley14c43f72013-05-22 12:50:43 -04004164
4165
4166 nfs4_label_free(res.label);
4167
Trond Myklebust136f2622010-04-16 16:22:49 -04004168out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004169 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return status;
4171}
4172
Al Virobeffb8f2016-07-20 16:34:42 -04004173static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
4175 struct nfs4_exception exception = { };
4176 int err;
4177 do {
4178 err = nfs4_handle_exception(NFS_SERVER(inode),
4179 _nfs4_proc_link(inode, dir, name),
4180 &exception);
4181 } while (exception.retry);
4182 return err;
4183}
4184
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004185struct nfs4_createdata {
4186 struct rpc_message msg;
4187 struct nfs4_create_arg arg;
4188 struct nfs4_create_res res;
4189 struct nfs_fh fh;
4190 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004191 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004192};
4193
4194static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004195 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004196{
4197 struct nfs4_createdata *data;
4198
4199 data = kzalloc(sizeof(*data), GFP_KERNEL);
4200 if (data != NULL) {
4201 struct nfs_server *server = NFS_SERVER(dir);
4202
David Quigley14c43f72013-05-22 12:50:43 -04004203 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4204 if (IS_ERR(data->label))
4205 goto out_free;
4206
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004207 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4208 data->msg.rpc_argp = &data->arg;
4209 data->msg.rpc_resp = &data->res;
4210 data->arg.dir_fh = NFS_FH(dir);
4211 data->arg.server = server;
4212 data->arg.name = name;
4213 data->arg.attrs = sattr;
4214 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004215 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216 data->res.server = server;
4217 data->res.fh = &data->fh;
4218 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004219 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004220 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221 }
4222 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004223out_free:
4224 kfree(data);
4225 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004226}
4227
4228static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4229{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004230 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004231 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232 if (status == 0) {
4233 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004234 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004235 }
4236 return status;
4237}
4238
4239static void nfs4_free_createdata(struct nfs4_createdata *data)
4240{
David Quigley14c43f72013-05-22 12:50:43 -04004241 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242 kfree(data);
4243}
4244
Chuck Lever4f390c12006-08-22 20:06:22 -04004245static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004246 struct page *page, unsigned int len, struct iattr *sattr,
4247 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 struct nfs4_createdata *data;
4250 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Chuck Lever94a6d752006-08-22 20:06:23 -04004252 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004253 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004254
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004255 status = -ENOMEM;
4256 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4257 if (data == NULL)
4258 goto out;
4259
4260 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4261 data->arg.u.symlink.pages = &page;
4262 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004263 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004265 status = nfs4_do_create(dir, dentry, data);
4266
4267 nfs4_free_createdata(data);
4268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return status;
4270}
4271
Chuck Lever4f390c12006-08-22 20:06:22 -04004272static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004273 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
4275 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004276 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004278
4279 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004282 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4283 trace_nfs4_symlink(dir, &dentry->d_name, err);
4284 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 &exception);
4286 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004287
4288 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return err;
4290}
4291
4292static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004293 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004295 struct nfs4_createdata *data;
4296 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004298 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4299 if (data == NULL)
4300 goto out;
4301
David Quigley1775fd32013-05-22 12:50:42 -04004302 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004303 status = nfs4_do_create(dir, dentry, data);
4304
4305 nfs4_free_createdata(data);
4306out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return status;
4308}
4309
4310static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4311 struct iattr *sattr)
4312{
4313 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004314 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004316
David Quigleyaa9c2662013-05-22 12:50:44 -04004317 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4318
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004319 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004321 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4322 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4323 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 &exception);
4325 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004326 nfs4_label_release_security(label);
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 return err;
4329}
4330
4331static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004332 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
David Howells2b0143b2015-03-17 22:25:59 +00004334 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 struct nfs4_readdir_arg args = {
4336 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004337 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 .pgbase = 0,
4339 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004340 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004341 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 };
4343 struct nfs4_readdir_res res;
4344 struct rpc_message msg = {
4345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4346 .rpc_argp = &args,
4347 .rpc_resp = &res,
4348 .rpc_cred = cred,
4349 };
4350 int status;
4351
Al Viro6de14722013-09-16 10:53:17 -04004352 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4353 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004354 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004355 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004357 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004358 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004359 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004360 status += args.pgbase;
4361 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004362
4363 nfs_invalidate_atime(dir);
4364
Harvey Harrison3110ff82008-05-02 13:42:44 -07004365 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 return status;
4367}
4368
4369static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004370 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
4372 struct nfs4_exception exception = { };
4373 int err;
4374 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004375 err = _nfs4_proc_readdir(dentry, cred, cookie,
4376 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004377 trace_nfs4_readdir(d_inode(dentry), err);
4378 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 &exception);
4380 } while (exception.retry);
4381 return err;
4382}
4383
4384static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004385 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004387 struct nfs4_createdata *data;
4388 int mode = sattr->ia_mode;
4389 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004391 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4392 if (data == NULL)
4393 goto out;
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004398 data->arg.ftype = NF4BLK;
4399 data->arg.u.device.specdata1 = MAJOR(rdev);
4400 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004403 data->arg.ftype = NF4CHR;
4404 data->arg.u.device.specdata1 = MAJOR(rdev);
4405 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004406 } else if (!S_ISSOCK(mode)) {
4407 status = -EINVAL;
4408 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
David Quigley1775fd32013-05-22 12:50:42 -04004410
David Quigleyaa9c2662013-05-22 12:50:44 -04004411 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004413out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414 nfs4_free_createdata(data);
4415out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return status;
4417}
4418
4419static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4420 struct iattr *sattr, dev_t rdev)
4421{
4422 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004423 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004425
David Quigleyaa9c2662013-05-22 12:50:44 -04004426 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4427
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004428 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004430 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4431 trace_nfs4_mknod(dir, &dentry->d_name, err);
4432 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 &exception);
4434 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004435
4436 nfs4_label_release_security(label);
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return err;
4439}
4440
4441static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4442 struct nfs_fsstat *fsstat)
4443{
4444 struct nfs4_statfs_arg args = {
4445 .fh = fhandle,
4446 .bitmask = server->attr_bitmask,
4447 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004448 struct nfs4_statfs_res res = {
4449 .fsstat = fsstat,
4450 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4453 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004454 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 };
4456
Trond Myklebust0e574af2005-10-27 22:12:38 -04004457 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004458 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
4461static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4462{
4463 struct nfs4_exception exception = { };
4464 int err;
4465 do {
4466 err = nfs4_handle_exception(server,
4467 _nfs4_proc_statfs(server, fhandle, fsstat),
4468 &exception);
4469 } while (exception.retry);
4470 return err;
4471}
4472
4473static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4474 struct nfs_fsinfo *fsinfo)
4475{
4476 struct nfs4_fsinfo_arg args = {
4477 .fh = fhandle,
4478 .bitmask = server->attr_bitmask,
4479 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004480 struct nfs4_fsinfo_res res = {
4481 .fsinfo = fsinfo,
4482 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 struct rpc_message msg = {
4484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4485 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004486 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 };
4488
Bryan Schumaker7c513052011-03-24 17:12:24 +00004489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490}
4491
4492static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4493{
4494 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004495 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 int err;
4497
4498 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004499 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004500 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004501 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004502 nfs4_set_lease_period(server->nfs_client,
4503 fsinfo->lease_time * HZ,
4504 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004505 break;
4506 }
4507 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 } while (exception.retry);
4509 return err;
4510}
4511
4512static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4513{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004514 int error;
4515
Trond Myklebust0e574af2005-10-27 22:12:38 -04004516 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004517 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004518 if (error == 0) {
4519 /* block layout checks this! */
4520 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004521 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004522 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004523
4524 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525}
4526
4527static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4528 struct nfs_pathconf *pathconf)
4529{
4530 struct nfs4_pathconf_arg args = {
4531 .fh = fhandle,
4532 .bitmask = server->attr_bitmask,
4533 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004534 struct nfs4_pathconf_res res = {
4535 .pathconf = pathconf,
4536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 struct rpc_message msg = {
4538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4539 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004540 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 };
4542
4543 /* None of the pathconf attributes are mandatory to implement */
4544 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4545 memset(pathconf, 0, sizeof(*pathconf));
4546 return 0;
4547 }
4548
Trond Myklebust0e574af2005-10-27 22:12:38 -04004549 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004550 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
4553static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4554 struct nfs_pathconf *pathconf)
4555{
4556 struct nfs4_exception exception = { };
4557 int err;
4558
4559 do {
4560 err = nfs4_handle_exception(server,
4561 _nfs4_proc_pathconf(server, fhandle, pathconf),
4562 &exception);
4563 } while (exception.retry);
4564 return err;
4565}
4566
Trond Myklebust5521abf2013-03-16 20:54:34 -04004567int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004568 const struct nfs_open_context *ctx,
4569 const struct nfs_lock_context *l_ctx,
4570 fmode_t fmode)
4571{
4572 const struct nfs_lockowner *lockowner = NULL;
4573
4574 if (l_ctx != NULL)
4575 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004576 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004577}
4578EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4579
Trond Myklebust5521abf2013-03-16 20:54:34 -04004580static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4581 const struct nfs_open_context *ctx,
4582 const struct nfs_lock_context *l_ctx,
4583 fmode_t fmode)
4584{
4585 nfs4_stateid current_stateid;
4586
Trond Myklebuste1253be2014-03-05 08:44:23 -05004587 /* If the current stateid represents a lost lock, then exit */
4588 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4589 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004590 return nfs4_stateid_match(stateid, &current_stateid);
4591}
4592
4593static bool nfs4_error_stateid_expired(int err)
4594{
4595 switch (err) {
4596 case -NFS4ERR_DELEG_REVOKED:
4597 case -NFS4ERR_ADMIN_REVOKED:
4598 case -NFS4ERR_BAD_STATEID:
4599 case -NFS4ERR_STALE_STATEID:
4600 case -NFS4ERR_OLD_STATEID:
4601 case -NFS4ERR_OPENMODE:
4602 case -NFS4ERR_EXPIRED:
4603 return true;
4604 }
4605 return false;
4606}
4607
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004608static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004612 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004613 if (task->tk_status < 0) {
4614 struct nfs4_exception exception = {
4615 .inode = hdr->inode,
4616 .state = hdr->args.context->state,
4617 .stateid = &hdr->args.stateid,
4618 };
4619 task->tk_status = nfs4_async_handle_exception(task,
4620 server, task->tk_status, &exception);
4621 if (exception.retry) {
4622 rpc_restart_call_prepare(task);
4623 return -EAGAIN;
4624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004628 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
Trond Myklebust5521abf2013-03-16 20:54:34 -04004632static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004633 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004634{
4635
4636 if (!nfs4_error_stateid_expired(task->tk_status) ||
4637 nfs4_stateid_is_current(&args->stateid,
4638 args->context,
4639 args->lock_context,
4640 FMODE_READ))
4641 return false;
4642 rpc_restart_call_prepare(task);
4643 return true;
4644}
4645
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004646static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004647{
4648
4649 dprintk("--> %s\n", __func__);
4650
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004651 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004652 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004653 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004654 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004655 if (task->tk_status > 0)
4656 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004657 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4658 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004659}
4660
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4662 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004665 if (!hdr->pgio_done_cb)
4666 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004667 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4672 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004673{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4675 &hdr->args.seq_args,
4676 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004677 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004678 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4680 hdr->args.lock_context,
4681 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004682 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004684 return -EIO;
4685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686}
4687
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688static int nfs4_write_done_cb(struct rpc_task *task,
4689 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004692
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004694 if (task->tk_status < 0) {
4695 struct nfs4_exception exception = {
4696 .inode = hdr->inode,
4697 .state = hdr->args.context->state,
4698 .stateid = &hdr->args.stateid,
4699 };
4700 task->tk_status = nfs4_async_handle_exception(task,
4701 NFS_SERVER(inode), task->tk_status,
4702 &exception);
4703 if (exception.retry) {
4704 rpc_restart_call_prepare(task);
4705 return -EAGAIN;
4706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004708 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004710 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004711 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
Trond Myklebust5521abf2013-03-16 20:54:34 -04004715static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004716 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004717{
4718
4719 if (!nfs4_error_stateid_expired(task->tk_status) ||
4720 nfs4_stateid_is_current(&args->stateid,
4721 args->context,
4722 args->lock_context,
4723 FMODE_WRITE))
4724 return false;
4725 rpc_restart_call_prepare(task);
4726 return true;
4727}
4728
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004729static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004730{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004731 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004732 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004733 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004734 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004735 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4736 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004737}
4738
Trond Myklebust5a37f852012-04-28 14:55:16 -04004739static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004740bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004741{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004742 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004743 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004744 return false;
4745 /* Otherwise, request attributes if and only if we don't hold
4746 * a delegation
4747 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004748 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004749}
Fred Isamana69aef12011-03-03 15:13:47 +00004750
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004751static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4752 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004754 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004755
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004756 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4757 hdr->args.bitmask = NULL;
4758 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004759 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004760 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004761
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762 if (!hdr->pgio_done_cb)
4763 hdr->pgio_done_cb = nfs4_write_done_cb;
4764 hdr->res.server = server;
4765 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004767 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004768 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}
4770
Fred Isaman0b7c0152012-04-20 14:47:39 -04004771static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4772{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004773 nfs4_setup_sequence(NFS_SERVER(data->inode),
4774 &data->args.seq_args,
4775 &data->res.seq_res,
4776 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
Fred Isaman0b7c0152012-04-20 14:47:39 -04004779static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004782
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004783 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004784 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4785 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004786 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004787 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790}
4791
Fred Isaman0b7c0152012-04-20 14:47:39 -04004792static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004793{
4794 if (!nfs4_sequence_done(task, &data->res.seq_res))
4795 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004796 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004797}
4798
Fred Isaman0b7c0152012-04-20 14:47:39 -04004799static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004801 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004802
Fred Isaman0b7c0152012-04-20 14:47:39 -04004803 if (data->commit_done_cb == NULL)
4804 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004805 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004806 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004807 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808}
4809
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004810struct nfs4_renewdata {
4811 struct nfs_client *client;
4812 unsigned long timestamp;
4813};
4814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815/*
4816 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4817 * standalone procedure for queueing an asynchronous RENEW.
4818 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004819static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004820{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004821 struct nfs4_renewdata *data = calldata;
4822 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004823
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004824 if (atomic_read(&clp->cl_count) > 1)
4825 nfs4_schedule_state_renewal(clp);
4826 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004827 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004828}
4829
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004830static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004832 struct nfs4_renewdata *data = calldata;
4833 struct nfs_client *clp = data->client;
4834 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004836 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004837 switch (task->tk_status) {
4838 case 0:
4839 break;
4840 case -NFS4ERR_LEASE_MOVED:
4841 nfs4_schedule_lease_moved_recovery(clp);
4842 break;
4843 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004844 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004845 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4846 return;
4847 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004848 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004849 return;
4850 }
4851 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004853 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004856static const struct rpc_call_ops nfs4_renew_ops = {
4857 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004858 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004859};
4860
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004861static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862{
4863 struct rpc_message msg = {
4864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4865 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004866 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004868 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004870 if (renew_flags == 0)
4871 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004872 if (!atomic_inc_not_zero(&clp->cl_count))
4873 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004874 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004875 if (data == NULL)
4876 return -ENOMEM;
4877 data->client = clp;
4878 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004879 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004880 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004883static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
4885 struct rpc_message msg = {
4886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4887 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004888 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 };
4890 unsigned long now = jiffies;
4891 int status;
4892
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004893 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 if (status < 0)
4895 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004896 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 return 0;
4898}
4899
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004900static inline int nfs4_server_supports_acls(struct nfs_server *server)
4901{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004902 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004903}
4904
Trond Myklebust21f498c2012-08-24 10:59:25 -04004905/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4906 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004907 * the stack.
4908 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004909#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004910
Neil Hormane9e3d722011-03-04 19:26:03 -05004911static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004912 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004913{
4914 struct page *newpage, **spages;
4915 int rc = 0;
4916 size_t len;
4917 spages = pages;
4918
4919 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004920 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004921 newpage = alloc_page(GFP_KERNEL);
4922
4923 if (newpage == NULL)
4924 goto unwind;
4925 memcpy(page_address(newpage), buf, len);
4926 buf += len;
4927 buflen -= len;
4928 *pages++ = newpage;
4929 rc++;
4930 } while (buflen != 0);
4931
4932 return rc;
4933
4934unwind:
4935 for(; rc > 0; rc--)
4936 __free_page(spages[rc-1]);
4937 return -ENOMEM;
4938}
4939
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004940struct nfs4_cached_acl {
4941 int cached;
4942 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004943 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004944};
4945
4946static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004947{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004948 struct nfs_inode *nfsi = NFS_I(inode);
4949
4950 spin_lock(&inode->i_lock);
4951 kfree(nfsi->nfs4_acl);
4952 nfsi->nfs4_acl = acl;
4953 spin_unlock(&inode->i_lock);
4954}
4955
4956static void nfs4_zap_acl_attr(struct inode *inode)
4957{
4958 nfs4_set_cached_acl(inode, NULL);
4959}
4960
4961static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4962{
4963 struct nfs_inode *nfsi = NFS_I(inode);
4964 struct nfs4_cached_acl *acl;
4965 int ret = -ENOENT;
4966
4967 spin_lock(&inode->i_lock);
4968 acl = nfsi->nfs4_acl;
4969 if (acl == NULL)
4970 goto out;
4971 if (buf == NULL) /* user is just asking for length */
4972 goto out_len;
4973 if (acl->cached == 0)
4974 goto out;
4975 ret = -ERANGE; /* see getxattr(2) man page */
4976 if (acl->len > buflen)
4977 goto out;
4978 memcpy(buf, acl->data, acl->len);
4979out_len:
4980 ret = acl->len;
4981out:
4982 spin_unlock(&inode->i_lock);
4983 return ret;
4984}
4985
Sachin Prabhu5794d212012-04-17 14:36:40 +01004986static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004987{
4988 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004989 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004990
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004991 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004992 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004993 if (acl == NULL)
4994 goto out;
4995 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004996 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004997 } else {
4998 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4999 if (acl == NULL)
5000 goto out;
5001 acl->cached = 0;
5002 }
5003 acl->len = acl_len;
5004out:
5005 nfs4_set_cached_acl(inode, acl);
5006}
5007
Andy Adamsonbf118a32011-12-07 11:55:27 -05005008/*
5009 * The getxattr API returns the required buffer length when called with a
5010 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5011 * the required buf. On a NULL buf, we send a page of data to the server
5012 * guessing that the ACL request can be serviced by a page. If so, we cache
5013 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5014 * the cache. If not so, we throw away the page, and cache the required
5015 * length. The next getxattr call will then produce another round trip to
5016 * the server, this time with the input buf of the required size.
5017 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005018static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005019{
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005020 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005021 struct nfs_getaclargs args = {
5022 .fh = NFS_FH(inode),
5023 .acl_pages = pages,
5024 .acl_len = buflen,
5025 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005026 struct nfs_getaclres res = {
5027 .acl_len = buflen,
5028 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005029 struct rpc_message msg = {
5030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5031 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005032 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005033 };
Weston Andros Adamsond78f9332017-02-23 14:54:21 -05005034 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005035 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005036
Trond Myklebust21f498c2012-08-24 10:59:25 -04005037 if (npages > ARRAY_SIZE(pages))
5038 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005039
Andy Adamsonbf118a32011-12-07 11:55:27 -05005040 for (i = 0; i < npages; i++) {
5041 pages[i] = alloc_page(GFP_KERNEL);
5042 if (!pages[i])
5043 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005044 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005045
5046 /* for decoding across pages */
5047 res.acl_scratch = alloc_page(GFP_KERNEL);
5048 if (!res.acl_scratch)
5049 goto out_free;
5050
Andy Adamsonbf118a32011-12-07 11:55:27 -05005051 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005052
Peng Taode040be2012-01-10 22:42:47 +08005053 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005054 __func__, buf, buflen, npages, args.acl_len);
5055 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5056 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005057 if (ret)
5058 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005059
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005060 /* Handle the case where the passed-in buffer is too short */
5061 if (res.acl_flags & NFS4_ACL_TRUNC) {
5062 /* Did the user only issue a request for the acl length? */
5063 if (buf == NULL)
5064 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005065 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005066 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005067 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005068 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005069 if (buf) {
5070 if (res.acl_len > buflen) {
5071 ret = -ERANGE;
5072 goto out_free;
5073 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005074 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005075 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005076out_ok:
5077 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005078out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005079 for (i = 0; i < npages; i++)
5080 if (pages[i])
5081 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005082 if (res.acl_scratch)
5083 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005084 return ret;
5085}
5086
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005087static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5088{
5089 struct nfs4_exception exception = { };
5090 ssize_t ret;
5091 do {
5092 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005093 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005094 if (ret >= 0)
5095 break;
5096 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5097 } while (exception.retry);
5098 return ret;
5099}
5100
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005101static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5102{
5103 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005104 int ret;
5105
5106 if (!nfs4_server_supports_acls(server))
5107 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005108 ret = nfs_revalidate_inode(server, inode);
5109 if (ret < 0)
5110 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005111 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5112 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005113 ret = nfs4_read_cached_acl(inode, buf, buflen);
5114 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005115 /* -ENOENT is returned if there is no ACL or if there is an ACL
5116 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005117 return ret;
5118 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005119}
5120
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005121static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005122{
5123 struct nfs_server *server = NFS_SERVER(inode);
5124 struct page *pages[NFS4ACL_MAXPAGES];
5125 struct nfs_setaclargs arg = {
5126 .fh = NFS_FH(inode),
5127 .acl_pages = pages,
5128 .acl_len = buflen,
5129 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005130 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005131 struct rpc_message msg = {
5132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5133 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005134 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005135 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005136 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005137 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005138
5139 if (!nfs4_server_supports_acls(server))
5140 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005141 if (npages > ARRAY_SIZE(pages))
5142 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005143 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005144 if (i < 0)
5145 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005146 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005147 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005148
5149 /*
5150 * Free each page after tx, so the only ref left is
5151 * held by the network stack
5152 */
5153 for (; i > 0; i--)
5154 put_page(pages[i-1]);
5155
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005156 /*
5157 * Acl update can result in inode attribute update.
5158 * so mark the attribute cache invalid.
5159 */
5160 spin_lock(&inode->i_lock);
5161 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5162 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005163 nfs_access_zap_cache(inode);
5164 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005165 return ret;
5166}
5167
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005168static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5169{
5170 struct nfs4_exception exception = { };
5171 int err;
5172 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005173 err = __nfs4_proc_set_acl(inode, buf, buflen);
5174 trace_nfs4_set_acl(inode, err);
5175 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04005176 &exception);
5177 } while (exception.retry);
5178 return err;
5179}
5180
David Quigleyaa9c2662013-05-22 12:50:44 -04005181#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5182static int _nfs4_get_security_label(struct inode *inode, void *buf,
5183 size_t buflen)
5184{
5185 struct nfs_server *server = NFS_SERVER(inode);
5186 struct nfs_fattr fattr;
5187 struct nfs4_label label = {0, 0, buflen, buf};
5188
5189 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005190 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005191 .fh = NFS_FH(inode),
5192 .bitmask = bitmask,
5193 };
5194 struct nfs4_getattr_res res = {
5195 .fattr = &fattr,
5196 .label = &label,
5197 .server = server,
5198 };
5199 struct rpc_message msg = {
5200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005201 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005202 .rpc_resp = &res,
5203 };
5204 int ret;
5205
5206 nfs_fattr_init(&fattr);
5207
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005208 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005209 if (ret)
5210 return ret;
5211 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5212 return -ENOENT;
5213 if (buflen < label.len)
5214 return -ERANGE;
5215 return 0;
5216}
5217
5218static int nfs4_get_security_label(struct inode *inode, void *buf,
5219 size_t buflen)
5220{
5221 struct nfs4_exception exception = { };
5222 int err;
5223
5224 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5225 return -EOPNOTSUPP;
5226
5227 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005228 err = _nfs4_get_security_label(inode, buf, buflen);
5229 trace_nfs4_get_security_label(inode, err);
5230 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005231 &exception);
5232 } while (exception.retry);
5233 return err;
5234}
5235
5236static int _nfs4_do_set_security_label(struct inode *inode,
5237 struct nfs4_label *ilabel,
5238 struct nfs_fattr *fattr,
5239 struct nfs4_label *olabel)
5240{
5241
5242 struct iattr sattr = {0};
5243 struct nfs_server *server = NFS_SERVER(inode);
5244 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005245 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005246 .fh = NFS_FH(inode),
5247 .iap = &sattr,
5248 .server = server,
5249 .bitmask = bitmask,
5250 .label = ilabel,
5251 };
5252 struct nfs_setattrres res = {
5253 .fattr = fattr,
5254 .label = olabel,
5255 .server = server,
5256 };
5257 struct rpc_message msg = {
5258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005259 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005260 .rpc_resp = &res,
5261 };
5262 int status;
5263
Jeff Layton12207f62013-11-01 10:49:32 -04005264 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005265
Jeff Layton12207f62013-11-01 10:49:32 -04005266 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005267 if (status)
5268 dprintk("%s failed: %d\n", __func__, status);
5269
5270 return status;
5271}
5272
5273static int nfs4_do_set_security_label(struct inode *inode,
5274 struct nfs4_label *ilabel,
5275 struct nfs_fattr *fattr,
5276 struct nfs4_label *olabel)
5277{
5278 struct nfs4_exception exception = { };
5279 int err;
5280
5281 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005282 err = _nfs4_do_set_security_label(inode, ilabel,
5283 fattr, olabel);
5284 trace_nfs4_set_security_label(inode, err);
5285 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005286 &exception);
5287 } while (exception.retry);
5288 return err;
5289}
5290
5291static int
Al Viro59301222016-05-27 10:19:30 -04005292nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005293{
5294 struct nfs4_label ilabel, *olabel = NULL;
5295 struct nfs_fattr fattr;
5296 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005297 int status;
5298
5299 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5300 return -EOPNOTSUPP;
5301
5302 nfs_fattr_init(&fattr);
5303
5304 ilabel.pi = 0;
5305 ilabel.lfs = 0;
5306 ilabel.label = (char *)buf;
5307 ilabel.len = buflen;
5308
5309 cred = rpc_lookup_cred();
5310 if (IS_ERR(cred))
5311 return PTR_ERR(cred);
5312
5313 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5314 if (IS_ERR(olabel)) {
5315 status = -PTR_ERR(olabel);
5316 goto out;
5317 }
5318
5319 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5320 if (status == 0)
5321 nfs_setsecurity(inode, &fattr, olabel);
5322
5323 nfs4_label_free(olabel);
5324out:
5325 put_rpccred(cred);
5326 return status;
5327}
5328#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5329
5330
Chuck Leverf0920752012-05-21 22:45:41 -04005331static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5332 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005333{
5334 __be32 verf[2];
5335
Chuck Lever2c820d92012-05-21 22:45:33 -04005336 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5337 /* An impossible timestamp guarantees this value
5338 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005339 verf[0] = cpu_to_be32(U32_MAX);
5340 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005341 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005342 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005343 u64 ns = ktime_to_ns(nn->boot_time);
5344
5345 verf[0] = cpu_to_be32(ns >> 32);
5346 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005347 }
Chuck Levercd937102012-03-02 17:14:31 -05005348 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5349}
5350
Jeff Laytona3192682015-06-09 19:43:59 -04005351static int
5352nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005353{
Jeff Laytona3192682015-06-09 19:43:59 -04005354 size_t len;
5355 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005356
Trond Myklebustceb3a162015-01-03 15:16:04 -05005357 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005358 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005359
Jeff Laytona3192682015-06-09 19:43:59 -04005360 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005361 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005362 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5363 1 +
5364 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5365 1;
5366 rcu_read_unlock();
5367
5368 if (len > NFS4_OPAQUE_LIMIT + 1)
5369 return -EINVAL;
5370
5371 /*
5372 * Since this string is allocated at mount time, and held until the
5373 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5374 * about a memory-reclaim deadlock.
5375 */
5376 str = kmalloc(len, GFP_KERNEL);
5377 if (!str)
5378 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005379
Chuck Levere984a552012-09-14 17:24:21 -04005380 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005381 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005382 clp->cl_ipaddr,
5383 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5384 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005385 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005386
Jeff Laytona3192682015-06-09 19:43:59 -04005387 clp->cl_owner_id = str;
5388 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005389}
5390
Jeff Layton873e3852015-06-09 19:44:00 -04005391static int
5392nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005393{
Jeff Layton873e3852015-06-09 19:44:00 -04005394 size_t len;
5395 char *str;
5396
5397 len = 10 + 10 + 1 + 10 + 1 +
5398 strlen(nfs4_client_id_uniquifier) + 1 +
5399 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5400
5401 if (len > NFS4_OPAQUE_LIMIT + 1)
5402 return -EINVAL;
5403
5404 /*
5405 * Since this string is allocated at mount time, and held until the
5406 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5407 * about a memory-reclaim deadlock.
5408 */
5409 str = kmalloc(len, GFP_KERNEL);
5410 if (!str)
5411 return -ENOMEM;
5412
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005413 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005414 clp->rpc_ops->version, clp->cl_minorversion,
5415 nfs4_client_id_uniquifier,
5416 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005417 clp->cl_owner_id = str;
5418 return 0;
5419}
5420
5421static int
5422nfs4_init_uniform_client_string(struct nfs_client *clp)
5423{
Jeff Layton873e3852015-06-09 19:44:00 -04005424 size_t len;
5425 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005426
5427 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005428 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005429
5430 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005431 return nfs4_init_uniquifier_client_string(clp);
5432
5433 len = 10 + 10 + 1 + 10 + 1 +
5434 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5435
5436 if (len > NFS4_OPAQUE_LIMIT + 1)
5437 return -EINVAL;
5438
5439 /*
5440 * Since this string is allocated at mount time, and held until the
5441 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5442 * about a memory-reclaim deadlock.
5443 */
5444 str = kmalloc(len, GFP_KERNEL);
5445 if (!str)
5446 return -ENOMEM;
5447
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005448 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005449 clp->rpc_ops->version, clp->cl_minorversion,
5450 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005451 clp->cl_owner_id = str;
5452 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005453}
5454
Chuck Lever706cb8d2014-03-12 12:51:47 -04005455/*
5456 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5457 * services. Advertise one based on the address family of the
5458 * clientaddr.
5459 */
5460static unsigned int
5461nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5462{
5463 if (strchr(clp->cl_ipaddr, ':') != NULL)
5464 return scnprintf(buf, len, "tcp6");
5465 else
5466 return scnprintf(buf, len, "tcp");
5467}
5468
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005469static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5470{
5471 struct nfs4_setclientid *sc = calldata;
5472
5473 if (task->tk_status == 0)
5474 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5475}
5476
5477static const struct rpc_call_ops nfs4_setclientid_ops = {
5478 .rpc_call_done = nfs4_setclientid_done,
5479};
5480
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005481/**
5482 * nfs4_proc_setclientid - Negotiate client ID
5483 * @clp: state data structure
5484 * @program: RPC program for NFSv4 callback service
5485 * @port: IP port number for NFS4 callback service
5486 * @cred: RPC credential to use for this call
5487 * @res: where to place the result
5488 *
5489 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5490 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005491int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5492 unsigned short port, struct rpc_cred *cred,
5493 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494{
5495 nfs4_verifier sc_verifier;
5496 struct nfs4_setclientid setclientid = {
5497 .sc_verifier = &sc_verifier,
5498 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005499 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 };
5501 struct rpc_message msg = {
5502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5503 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005504 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005505 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005507 struct rpc_task *task;
5508 struct rpc_task_setup task_setup_data = {
5509 .rpc_client = clp->cl_rpcclient,
5510 .rpc_message = &msg,
5511 .callback_ops = &nfs4_setclientid_ops,
5512 .callback_data = &setclientid,
5513 .flags = RPC_TASK_TIMEOUT,
5514 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005515 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Chuck Leverde734832012-07-11 16:30:50 -04005517 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005518 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005519
5520 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5521 status = nfs4_init_uniform_client_string(clp);
5522 else
Jeff Laytona3192682015-06-09 19:43:59 -04005523 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005524
5525 if (status)
5526 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005527
Chuck Leverde734832012-07-11 16:30:50 -04005528 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005529 setclientid.sc_netid_len =
5530 nfs4_init_callback_netid(clp,
5531 setclientid.sc_netid,
5532 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005533 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005534 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 clp->cl_ipaddr, port >> 8, port & 255);
5536
Jeff Layton3a6bb732015-06-09 19:43:57 -04005537 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005538 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005539 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005540 task = rpc_run_task(&task_setup_data);
5541 if (IS_ERR(task)) {
5542 status = PTR_ERR(task);
5543 goto out;
5544 }
5545 status = task->tk_status;
5546 if (setclientid.sc_cred) {
Chuck Lever05f0b032019-10-04 09:58:54 -04005547 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005548 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5549 put_rpccred(setclientid.sc_cred);
5550 }
5551 rpc_put_task(task);
5552out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005553 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005554 dprintk("NFS reply setclientid: %d\n", status);
5555 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556}
5557
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005558/**
5559 * nfs4_proc_setclientid_confirm - Confirm client ID
5560 * @clp: state data structure
5561 * @res: result of a previous SETCLIENTID
5562 * @cred: RPC credential to use for this call
5563 *
5564 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5565 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005566int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005567 struct nfs4_setclientid_res *arg,
5568 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 struct rpc_message msg = {
5571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005572 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005573 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 int status;
5576
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005577 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5578 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5579 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005580 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005581 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005582 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 return status;
5584}
5585
Trond Myklebustfe650402006-01-03 09:55:18 +01005586struct nfs4_delegreturndata {
5587 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005588 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005589 struct nfs_fh fh;
5590 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005591 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005592 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005593 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005594 struct inode *inode;
5595 bool roc;
5596 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005597};
5598
Trond Myklebustfe650402006-01-03 09:55:18 +01005599static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5600{
5601 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005602
Trond Myklebust14516c32010-07-31 14:29:06 -04005603 if (!nfs4_sequence_done(task, &data->res.seq_res))
5604 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005605
Trond Myklebustca8acf82013-08-13 10:36:56 -04005606 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005607 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005608 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005609 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005610 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005611 case -NFS4ERR_ADMIN_REVOKED:
5612 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005613 case -NFS4ERR_EXPIRED:
5614 nfs4_free_revoked_stateid(data->res.server,
5615 data->args.stateid,
5616 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005617 case -NFS4ERR_BAD_STATEID:
5618 case -NFS4ERR_OLD_STATEID:
5619 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005620 task->tk_status = 0;
5621 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005622 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005623 if (nfs4_async_handle_error(task, data->res.server,
5624 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005625 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005626 return;
5627 }
5628 }
5629 data->rpc_status = task->tk_status;
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005630 if (data->roc && data->rpc_status == 0)
5631 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustfe650402006-01-03 09:55:18 +01005632}
5633
5634static void nfs4_delegreturn_release(void *calldata)
5635{
Peng Tao039b7562014-07-03 13:05:02 +08005636 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005637 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005638
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005639 if (inode) {
5640 if (data->roc)
5641 pnfs_roc_release(inode);
5642 nfs_iput_and_deactive(inode);
5643 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005644 kfree(calldata);
5645}
5646
Andy Adamson938e1012009-04-01 09:22:28 -04005647static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5648{
5649 struct nfs4_delegreturndata *d_data;
5650
5651 d_data = (struct nfs4_delegreturndata *)data;
5652
Peng Tao500d7012015-09-22 11:35:22 +08005653 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5654 return;
5655
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005656 if (d_data->roc)
5657 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005658
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005659 nfs4_setup_sequence(d_data->res.server,
5660 &d_data->args.seq_args,
5661 &d_data->res.seq_res,
5662 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005663}
Andy Adamson938e1012009-04-01 09:22:28 -04005664
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005665static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005666 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005667 .rpc_call_done = nfs4_delegreturn_done,
5668 .rpc_release = nfs4_delegreturn_release,
5669};
5670
Trond Myklebuste6f81072008-01-24 18:14:34 -05005671static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005672{
5673 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005674 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005675 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005676 struct rpc_message msg = {
5677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5678 .rpc_cred = cred,
5679 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005680 struct rpc_task_setup task_setup_data = {
5681 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005682 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005683 .callback_ops = &nfs4_delegreturn_ops,
5684 .flags = RPC_TASK_ASYNC,
5685 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005686 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005687
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005688 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005689 if (data == NULL)
5690 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005691 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005692
5693 nfs4_state_protect(server->nfs_client,
5694 NFS_SP4_MACH_CRED_CLEANUP,
5695 &task_setup_data.rpc_client, &msg);
5696
Trond Myklebustfe650402006-01-03 09:55:18 +01005697 data->args.fhandle = &data->fh;
5698 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005699 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005700 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005701 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005702 data->res.fattr = &data->fattr;
5703 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005704 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005705 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005706 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005707 data->inode = nfs_igrab_and_active(inode);
5708 if (data->inode)
5709 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005710
Trond Myklebustc970aa82007-07-14 15:39:59 -04005711 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005712 msg.rpc_argp = &data->args;
5713 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005714 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005715 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005716 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005717 if (!issync)
5718 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005719 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005720 if (status != 0)
5721 goto out;
5722 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005723 if (status == 0)
5724 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5725 else
5726 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005727out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005728 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005729 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730}
5731
Trond Myklebuste6f81072008-01-24 18:14:34 -05005732int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733{
5734 struct nfs_server *server = NFS_SERVER(inode);
5735 struct nfs4_exception exception = { };
5736 int err;
5737 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005738 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005739 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 switch (err) {
5741 case -NFS4ERR_STALE_STATEID:
5742 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 case 0:
5744 return 0;
5745 }
5746 err = nfs4_handle_exception(server, err, &exception);
5747 } while (exception.retry);
5748 return err;
5749}
5750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5752{
5753 struct inode *inode = state->inode;
5754 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005755 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005756 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005758 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005760 struct nfs_lockt_res res = {
5761 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 };
5763 struct rpc_message msg = {
5764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5765 .rpc_argp = &arg,
5766 .rpc_resp = &res,
5767 .rpc_cred = state->owner->so_cred,
5768 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 struct nfs4_lock_state *lsp;
5770 int status;
5771
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005772 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005773 status = nfs4_set_lock_state(state, request);
5774 if (status != 0)
5775 goto out;
5776 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005777 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005778 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005779 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005780 switch (status) {
5781 case 0:
5782 request->fl_type = F_UNLCK;
5783 break;
5784 case -NFS4ERR_DENIED:
5785 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005787 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005788 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005789out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 return status;
5791}
5792
5793static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5794{
5795 struct nfs4_exception exception = { };
5796 int err;
5797
5798 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005799 err = _nfs4_proc_getlk(state, cmd, request);
5800 trace_nfs4_get_lock(request, state, cmd, err);
5801 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 &exception);
5803 } while (exception.retry);
5804 return err;
5805}
5806
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005807struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005808 struct nfs_locku_args arg;
5809 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005810 struct nfs4_lock_state *lsp;
5811 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005812 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005813 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005814 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005815};
5816
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005817static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5818 struct nfs_open_context *ctx,
5819 struct nfs4_lock_state *lsp,
5820 struct nfs_seqid *seqid)
5821{
5822 struct nfs4_unlockdata *p;
5823 struct inode *inode = lsp->ls_state->inode;
5824
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005825 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005826 if (p == NULL)
5827 return NULL;
5828 p->arg.fh = NFS_FH(inode);
5829 p->arg.fl = &p->fl;
5830 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005831 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005832 p->lsp = lsp;
5833 atomic_inc(&lsp->ls_count);
5834 /* Ensure we don't close file until we're done freeing locks! */
5835 p->ctx = get_nfs_open_context(ctx);
5836 memcpy(&p->fl, fl, sizeof(p->fl));
5837 p->server = NFS_SERVER(inode);
5838 return p;
5839}
5840
Trond Myklebust06f814a2006-01-03 09:55:07 +01005841static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005842{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005843 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005844 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005845 nfs4_put_lock_state(calldata->lsp);
5846 put_nfs_open_context(calldata->ctx);
5847 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005848}
5849
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005850static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005851{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005852 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005853
Trond Myklebust14516c32010-07-31 14:29:06 -04005854 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5855 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005856 switch (task->tk_status) {
5857 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005858 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005859 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005860 if (nfs4_update_lock_stateid(calldata->lsp,
5861 &calldata->res.stateid))
5862 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005863 case -NFS4ERR_ADMIN_REVOKED:
5864 case -NFS4ERR_EXPIRED:
5865 nfs4_free_revoked_stateid(calldata->server,
5866 &calldata->arg.stateid,
5867 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005868 case -NFS4ERR_BAD_STATEID:
5869 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005870 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005871 if (!nfs4_stateid_match(&calldata->arg.stateid,
5872 &calldata->lsp->ls_stateid))
5873 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005874 break;
5875 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005876 if (nfs4_async_handle_error(task, calldata->server,
5877 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005878 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005879 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005880 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005881}
5882
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005883static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005884{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005885 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005887 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005888 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005889 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005890 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005891 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005892 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005893 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005894 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005895 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005896 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005897 &calldata->res.seq_res,
5898 task) != 0)
5899 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005900 return;
5901out_no_action:
5902 task->tk_action = NULL;
5903out_wait:
5904 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905}
5906
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005907static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005908 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005909 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005910 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005911};
5912
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005913static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5914 struct nfs_open_context *ctx,
5915 struct nfs4_lock_state *lsp,
5916 struct nfs_seqid *seqid)
5917{
5918 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005919 struct rpc_message msg = {
5920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5921 .rpc_cred = ctx->cred,
5922 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005923 struct rpc_task_setup task_setup_data = {
5924 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005925 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005926 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005927 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005928 .flags = RPC_TASK_ASYNC,
5929 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005931 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5932 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5933
Frank Filz137d6ac2007-07-09 15:32:29 -07005934 /* Ensure this is an unlock - when canceling a lock, the
5935 * canceled lock is passed in, and it won't be an unlock.
5936 */
5937 fl->fl_type = F_UNLCK;
5938
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005939 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5940 if (data == NULL) {
5941 nfs_free_seqid(seqid);
5942 return ERR_PTR(-ENOMEM);
5943 }
5944
Chuck Levera9c92d62013-08-09 12:48:18 -04005945 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005946 msg.rpc_argp = &data->arg;
5947 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005948 task_setup_data.callback_data = data;
5949 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005950}
5951
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5953{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005954 struct inode *inode = state->inode;
5955 struct nfs4_state_owner *sp = state->owner;
5956 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005957 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005958 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005959 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005960 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005961 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005962 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
Trond Myklebust9b073572006-06-29 16:38:34 -04005964 status = nfs4_set_lock_state(state, request);
5965 /* Unlock _before_ we do the RPC call */
5966 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005967 /* Exclude nfs_delegation_claim_locks() */
5968 mutex_lock(&sp->so_delegreturn_mutex);
5969 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005970 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005971 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005972 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005973 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005974 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005975 }
5976 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005977 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005978 if (status != 0)
5979 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005980 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005981 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005982 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5983 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005984 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5985 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005986 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005987 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005988 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005989 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005990 status = PTR_ERR(task);
5991 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005992 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005993 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005994 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005995out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005996 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005997 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005998 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006001struct nfs4_lockdata {
6002 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006003 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004 struct nfs4_lock_state *lsp;
6005 struct nfs_open_context *ctx;
6006 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006007 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008 int rpc_status;
6009 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006010 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006011};
6012
6013static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006014 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6015 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006016{
6017 struct nfs4_lockdata *p;
6018 struct inode *inode = lsp->ls_state->inode;
6019 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006020 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006021
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006022 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006023 if (p == NULL)
6024 return NULL;
6025
6026 p->arg.fh = NFS_FH(inode);
6027 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006028 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006029 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006030 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006031 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6032 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006033 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006034 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006035 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006036 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006037 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006038 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006039 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006040 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006041 atomic_inc(&lsp->ls_count);
6042 p->ctx = get_nfs_open_context(ctx);
6043 memcpy(&p->fl, fl, sizeof(p->fl));
6044 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006045out_free_seqid:
6046 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006047out_free:
6048 kfree(p);
6049 return NULL;
6050}
6051
6052static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6053{
6054 struct nfs4_lockdata *data = calldata;
6055 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
Harvey Harrison3110ff82008-05-02 13:42:44 -07006057 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006058 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006059 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006060 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006061 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006062 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006063 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006064 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006065 nfs4_stateid_copy(&data->arg.open_stateid,
6066 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006067 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006068 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006069 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006070 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006071 nfs4_stateid_copy(&data->arg.lock_stateid,
6072 &data->lsp->ls_stateid);
6073 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006074 if (!nfs4_valid_open_stateid(state)) {
6075 data->rpc_status = -EBADF;
6076 task->tk_action = NULL;
6077 goto out_release_open_seqid;
6078 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006079 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006080 if (nfs4_setup_sequence(data->server,
6081 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006082 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006083 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006084 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006085out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006086 nfs_release_seqid(data->arg.open_seqid);
6087out_release_lock_seqid:
6088 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006089out_wait:
6090 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006091 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006092}
6093
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6095{
6096 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006097 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006098
Harvey Harrison3110ff82008-05-02 13:42:44 -07006099 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006100
Trond Myklebust14516c32010-07-31 14:29:06 -04006101 if (!nfs4_sequence_done(task, &data->res.seq_res))
6102 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006103
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006104 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006105 switch (task->tk_status) {
6106 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006107 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006108 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006109 if (data->arg.new_lock) {
6110 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006111 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006112 rpc_restart_call_prepare(task);
6113 break;
6114 }
6115 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006116 if (data->arg.new_lock_owner != 0) {
6117 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6118 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6119 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6120 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6121 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006122 break;
6123 case -NFS4ERR_BAD_STATEID:
6124 case -NFS4ERR_OLD_STATEID:
6125 case -NFS4ERR_STALE_STATEID:
6126 case -NFS4ERR_EXPIRED:
6127 if (data->arg.new_lock_owner != 0) {
6128 if (!nfs4_stateid_match(&data->arg.open_stateid,
6129 &lsp->ls_state->open_stateid))
6130 rpc_restart_call_prepare(task);
6131 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6132 &lsp->ls_stateid))
6133 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006134 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006135 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006136}
6137
6138static void nfs4_lock_release(void *calldata)
6139{
6140 struct nfs4_lockdata *data = calldata;
6141
Harvey Harrison3110ff82008-05-02 13:42:44 -07006142 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006143 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006144 if (data->cancelled != 0) {
6145 struct rpc_task *task;
6146 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6147 data->arg.lock_seqid);
6148 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006149 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006150 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151 } else
6152 nfs_free_seqid(data->arg.lock_seqid);
6153 nfs4_put_lock_state(data->lsp);
6154 put_nfs_open_context(data->ctx);
6155 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006156 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006157}
6158
6159static const struct rpc_call_ops nfs4_lock_ops = {
6160 .rpc_call_prepare = nfs4_lock_prepare,
6161 .rpc_call_done = nfs4_lock_done,
6162 .rpc_release = nfs4_lock_release,
6163};
6164
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006165static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6166{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006167 switch (error) {
6168 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006169 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006170 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006171 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006172 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006173 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006174 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006175 break;
6176 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006177 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006178 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006179 };
6180}
6181
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006182static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006183{
6184 struct nfs4_lockdata *data;
6185 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006186 struct rpc_message msg = {
6187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6188 .rpc_cred = state->owner->so_cred,
6189 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006190 struct rpc_task_setup task_setup_data = {
6191 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006192 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006193 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006194 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006195 .flags = RPC_TASK_ASYNC,
6196 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006197 int ret;
6198
Harvey Harrison3110ff82008-05-02 13:42:44 -07006199 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006200 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006201 fl->fl_u.nfs4_fl.owner,
6202 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006203 if (data == NULL)
6204 return -ENOMEM;
6205 if (IS_SETLKW(cmd))
6206 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006207 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006208 msg.rpc_argp = &data->arg;
6209 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006210 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006211 if (recovery_type > NFS_LOCK_NEW) {
6212 if (recovery_type == NFS_LOCK_RECLAIM)
6213 data->arg.reclaim = NFS_LOCK_RECLAIM;
6214 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006215 } else
6216 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006217 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006218 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006220 ret = nfs4_wait_for_completion_rpc_task(task);
6221 if (ret == 0) {
6222 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006223 if (ret)
6224 nfs4_handle_setlk_error(data->server, data->lsp,
6225 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006226 } else
6227 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006228 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006229 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006230 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232}
6233
6234static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6235{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006236 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006237 struct nfs4_exception exception = {
6238 .inode = state->inode,
6239 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006240 int err;
6241
6242 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006243 /* Cache the lock if possible... */
6244 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6245 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006246 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006247 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006248 break;
6249 nfs4_handle_exception(server, err, &exception);
6250 } while (exception.retry);
6251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252}
6253
6254static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6255{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006256 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006257 struct nfs4_exception exception = {
6258 .inode = state->inode,
6259 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006260 int err;
6261
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006262 err = nfs4_set_lock_state(state, request);
6263 if (err != 0)
6264 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006265 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006266 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6267 return 0;
6268 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006269 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006270 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6271 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006272 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006273 switch (err) {
6274 default:
6275 goto out;
6276 case -NFS4ERR_GRACE:
6277 case -NFS4ERR_DELAY:
6278 nfs4_handle_exception(server, err, &exception);
6279 err = 0;
6280 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006281 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006282out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284}
6285
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006286#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006287static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6288{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006289 struct nfs4_lock_state *lsp;
6290 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006291
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006292 status = nfs4_set_lock_state(state, request);
6293 if (status != 0)
6294 return status;
6295 lsp = request->fl_u.nfs4_fl.owner;
6296 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6297 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6298 return 0;
6299 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006300 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006301}
6302#endif
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6305{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006306 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006307 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006308 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006309 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310
Trond Myklebust01c3b862006-06-29 16:38:39 -04006311 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006312 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006313 if (status < 0)
6314 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006315 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006316 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006317 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006318 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006319 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006320 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006321 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006322 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006323 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006324 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006325 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006326 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006327 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006328 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006329out:
6330 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 return status;
6332}
6333
6334static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6335{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006336 struct nfs4_exception exception = {
6337 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006338 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 int err;
6341
6342 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006343 err = _nfs4_proc_setlk(state, cmd, request);
6344 if (err == -NFS4ERR_DENIED)
6345 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006347 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 } while (exception.retry);
6349 return err;
6350}
6351
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006352#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6353#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6354
6355static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006356nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6357 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006358{
6359 int status = -ERESTARTSYS;
6360 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6361
6362 while(!signalled()) {
6363 status = nfs4_proc_setlk(state, cmd, request);
6364 if ((status != -EAGAIN) || IS_SETLK(cmd))
6365 break;
6366 freezable_schedule_timeout_interruptible(timeout);
6367 timeout *= 2;
6368 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6369 status = -ERESTARTSYS;
6370 }
6371 return status;
6372}
6373
Jeff Laytona1d617d82016-09-17 18:17:39 -04006374#ifdef CONFIG_NFS_V4_1
6375struct nfs4_lock_waiter {
6376 struct task_struct *task;
6377 struct inode *inode;
6378 struct nfs_lowner *owner;
6379 bool notified;
6380};
6381
6382static int
6383nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6384{
6385 int ret;
6386 struct cb_notify_lock_args *cbnl = key;
6387 struct nfs4_lock_waiter *waiter = wait->private;
6388 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6389 *wowner = waiter->owner;
6390
6391 /* Only wake if the callback was for the same owner */
6392 if (lowner->clientid != wowner->clientid ||
6393 lowner->id != wowner->id ||
6394 lowner->s_dev != wowner->s_dev)
6395 return 0;
6396
6397 /* Make sure it's for the right inode */
6398 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6399 return 0;
6400
6401 waiter->notified = true;
6402
6403 /* override "private" so we can use default_wake_function */
6404 wait->private = waiter->task;
6405 ret = autoremove_wake_function(wait, mode, flags, key);
6406 wait->private = waiter;
6407 return ret;
6408}
6409
6410static int
6411nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6412{
6413 int status = -ERESTARTSYS;
6414 unsigned long flags;
6415 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6416 struct nfs_server *server = NFS_SERVER(state->inode);
6417 struct nfs_client *clp = server->nfs_client;
6418 wait_queue_head_t *q = &clp->cl_lock_waitq;
6419 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6420 .id = lsp->ls_seqid.owner_id,
6421 .s_dev = server->s_dev };
6422 struct nfs4_lock_waiter waiter = { .task = current,
6423 .inode = state->inode,
6424 .owner = &owner,
6425 .notified = false };
6426 wait_queue_t wait;
6427
6428 /* Don't bother with waitqueue if we don't expect a callback */
6429 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6430 return nfs4_retry_setlk_simple(state, cmd, request);
6431
6432 init_wait(&wait);
6433 wait.private = &waiter;
6434 wait.func = nfs4_wake_lock_waiter;
6435 add_wait_queue(q, &wait);
6436
6437 while(!signalled()) {
6438 status = nfs4_proc_setlk(state, cmd, request);
6439 if ((status != -EAGAIN) || IS_SETLK(cmd))
6440 break;
6441
6442 status = -ERESTARTSYS;
6443 spin_lock_irqsave(&q->lock, flags);
6444 if (waiter.notified) {
6445 spin_unlock_irqrestore(&q->lock, flags);
6446 continue;
6447 }
6448 set_current_state(TASK_INTERRUPTIBLE);
6449 spin_unlock_irqrestore(&q->lock, flags);
6450
Benjamin Coddington7d2a3542017-07-28 12:33:54 -04006451 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006452 }
6453
6454 finish_wait(q, &wait);
6455 return status;
6456}
6457#else /* !CONFIG_NFS_V4_1 */
6458static inline int
6459nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6460{
6461 return nfs4_retry_setlk_simple(state, cmd, request);
6462}
6463#endif
6464
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465static int
6466nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6467{
6468 struct nfs_open_context *ctx;
6469 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 int status;
6471
6472 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006473 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 state = ctx->state;
6475
6476 if (request->fl_start < 0 || request->fl_end < 0)
6477 return -EINVAL;
6478
Trond Myklebustd9531262009-07-21 19:22:38 -04006479 if (IS_GETLK(cmd)) {
6480 if (state != NULL)
6481 return nfs4_proc_getlk(state, F_GETLK, request);
6482 return 0;
6483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484
6485 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6486 return -EINVAL;
6487
Trond Myklebustd9531262009-07-21 19:22:38 -04006488 if (request->fl_type == F_UNLCK) {
6489 if (state != NULL)
6490 return nfs4_proc_unlck(state, cmd, request);
6491 return 0;
6492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493
Trond Myklebustd9531262009-07-21 19:22:38 -04006494 if (state == NULL)
6495 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006496
6497 if ((request->fl_flags & FL_POSIX) &&
6498 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6499 return -ENOLCK;
6500
Trond Myklebust55725512012-04-18 12:48:35 -04006501 /*
6502 * Don't rely on the VFS having checked the file open mode,
6503 * since it won't do this for flock() locks.
6504 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006505 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006506 case F_RDLCK:
6507 if (!(filp->f_mode & FMODE_READ))
6508 return -EBADF;
6509 break;
6510 case F_WRLCK:
6511 if (!(filp->f_mode & FMODE_WRITE))
6512 return -EBADF;
6513 }
6514
Jeff Layton1ea67db2016-09-17 18:17:37 -04006515 status = nfs4_set_lock_state(state, request);
6516 if (status != 0)
6517 return status;
6518
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006519 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520}
6521
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006522int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006523{
6524 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006525 int err;
6526
6527 err = nfs4_set_lock_state(state, fl);
6528 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006529 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006530 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrown70f254a2017-12-13 09:57:09 +11006531 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006532}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006533
Trond Myklebustcf470c32012-03-07 13:49:12 -05006534struct nfs_release_lockowner_data {
6535 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006536 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006537 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006538 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006539 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006540};
6541
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006542static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6543{
6544 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006545 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006546 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6547 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006548 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006549 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006550}
6551
6552static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6553{
6554 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006555 struct nfs_server *server = data->server;
6556
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006557 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006558
6559 switch (task->tk_status) {
6560 case 0:
6561 renew_lease(server, data->timestamp);
6562 break;
6563 case -NFS4ERR_STALE_CLIENTID:
6564 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006565 nfs4_schedule_lease_recovery(server->nfs_client);
6566 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006567 case -NFS4ERR_LEASE_MOVED:
6568 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006569 if (nfs4_async_handle_error(task, server,
6570 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006571 rpc_restart_call_prepare(task);
6572 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006573}
6574
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006575static void nfs4_release_lockowner_release(void *calldata)
6576{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006577 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006578 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006579 kfree(calldata);
6580}
6581
Trond Myklebust17280172012-03-11 13:11:00 -04006582static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006583 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6584 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006585 .rpc_release = nfs4_release_lockowner_release,
6586};
6587
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006588static void
6589nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006590{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006591 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006592 struct rpc_message msg = {
6593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6594 };
6595
6596 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006597 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006598
Trond Myklebustcf470c32012-03-07 13:49:12 -05006599 data = kmalloc(sizeof(*data), GFP_NOFS);
6600 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006601 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006602 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006603 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006604 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6605 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6606 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006607
Trond Myklebustcf470c32012-03-07 13:49:12 -05006608 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006609 msg.rpc_resp = &data->res;
6610 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006611 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006612}
6613
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006614#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6615
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006616static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006617 struct dentry *unused, struct inode *inode,
6618 const char *key, const void *buf,
6619 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006620{
Al Viro59301222016-05-27 10:19:30 -04006621 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006622}
6623
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006624static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006625 struct dentry *unused, struct inode *inode,
6626 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006627{
Al Virob2968212016-04-10 20:48:24 -04006628 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006629}
6630
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006631static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006632{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006633 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006634}
6635
David Quigleyc9bccef2013-05-22 12:50:45 -04006636#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006637
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006638static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006639 struct dentry *unused, struct inode *inode,
6640 const char *key, const void *buf,
6641 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006642{
6643 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006644 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006645
6646 return -EOPNOTSUPP;
6647}
6648
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006649static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006650 struct dentry *unused, struct inode *inode,
6651 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006652{
6653 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006654 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006655 return -EOPNOTSUPP;
6656}
6657
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006658static ssize_t
6659nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006660{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006661 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006662
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006663 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6664 len = security_inode_listsecurity(inode, list, list_len);
6665 if (list_len && len > list_len)
6666 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006667 }
6668 return len;
6669}
6670
6671static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6672 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006673 .get = nfs4_xattr_get_nfs4_label,
6674 .set = nfs4_xattr_set_nfs4_label,
6675};
David Quigleyc9bccef2013-05-22 12:50:45 -04006676
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006677#else
6678
6679static ssize_t
6680nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6681{
6682 return 0;
6683}
6684
6685#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006686
Andy Adamson533eb462011-06-13 18:25:56 -04006687/*
6688 * nfs_fhget will use either the mounted_on_fileid or the fileid
6689 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006690static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6691{
Andy Adamson533eb462011-06-13 18:25:56 -04006692 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6693 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6694 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006695 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006696 return;
6697
6698 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006699 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006700 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6701 fattr->nlink = 2;
6702}
6703
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006704static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6705 const struct qstr *name,
6706 struct nfs4_fs_locations *fs_locations,
6707 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006708{
6709 struct nfs_server *server = NFS_SERVER(dir);
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006710 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006711 struct nfs4_fs_locations_arg args = {
6712 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006713 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006714 .page = page,
6715 .bitmask = bitmask,
6716 };
Benny Halevy22958462009-04-01 09:22:02 -04006717 struct nfs4_fs_locations_res res = {
6718 .fs_locations = fs_locations,
6719 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006720 struct rpc_message msg = {
6721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6722 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006723 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006724 };
6725 int status;
6726
Harvey Harrison3110ff82008-05-02 13:42:44 -07006727 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006728
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006729 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6730 bitmask[1] = nfs4_fattr_bitmap[1];
6731
Andy Adamson533eb462011-06-13 18:25:56 -04006732 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6733 * is not supported */
6734 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006735 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006736 else
Chuck Lever8ead2ac2017-11-05 15:45:22 -05006737 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006738
Trond Myklebustc228fd32007-01-13 02:28:11 -05006739 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006740 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006741 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006742 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006743 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006744 return status;
6745}
6746
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006747int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6748 const struct qstr *name,
6749 struct nfs4_fs_locations *fs_locations,
6750 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006751{
6752 struct nfs4_exception exception = { };
6753 int err;
6754 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006755 err = _nfs4_proc_fs_locations(client, dir, name,
6756 fs_locations, page);
6757 trace_nfs4_get_fs_locations(dir, name, err);
6758 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006759 &exception);
6760 } while (exception.retry);
6761 return err;
6762}
6763
Chuck Leverb03d7352013-10-17 14:12:50 -04006764/*
6765 * This operation also signals the server that this client is
6766 * performing migration recovery. The server can stop returning
6767 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6768 * appended to this compound to identify the client ID which is
6769 * performing recovery.
6770 */
6771static int _nfs40_proc_get_locations(struct inode *inode,
6772 struct nfs4_fs_locations *locations,
6773 struct page *page, struct rpc_cred *cred)
6774{
6775 struct nfs_server *server = NFS_SERVER(inode);
6776 struct rpc_clnt *clnt = server->client;
6777 u32 bitmask[2] = {
6778 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6779 };
6780 struct nfs4_fs_locations_arg args = {
6781 .clientid = server->nfs_client->cl_clientid,
6782 .fh = NFS_FH(inode),
6783 .page = page,
6784 .bitmask = bitmask,
6785 .migration = 1, /* skip LOOKUP */
6786 .renew = 1, /* append RENEW */
6787 };
6788 struct nfs4_fs_locations_res res = {
6789 .fs_locations = locations,
6790 .migration = 1,
6791 .renew = 1,
6792 };
6793 struct rpc_message msg = {
6794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6795 .rpc_argp = &args,
6796 .rpc_resp = &res,
6797 .rpc_cred = cred,
6798 };
6799 unsigned long now = jiffies;
6800 int status;
6801
6802 nfs_fattr_init(&locations->fattr);
6803 locations->server = server;
6804 locations->nlocations = 0;
6805
6806 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6807 nfs4_set_sequence_privileged(&args.seq_args);
6808 status = nfs4_call_sync_sequence(clnt, server, &msg,
6809 &args.seq_args, &res.seq_res);
6810 if (status)
6811 return status;
6812
6813 renew_lease(server, now);
6814 return 0;
6815}
6816
6817#ifdef CONFIG_NFS_V4_1
6818
6819/*
6820 * This operation also signals the server that this client is
6821 * performing migration recovery. The server can stop asserting
6822 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6823 * performing this operation is identified in the SEQUENCE
6824 * operation in this compound.
6825 *
6826 * When the client supports GETATTR(fs_locations_info), it can
6827 * be plumbed in here.
6828 */
6829static int _nfs41_proc_get_locations(struct inode *inode,
6830 struct nfs4_fs_locations *locations,
6831 struct page *page, struct rpc_cred *cred)
6832{
6833 struct nfs_server *server = NFS_SERVER(inode);
6834 struct rpc_clnt *clnt = server->client;
6835 u32 bitmask[2] = {
6836 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6837 };
6838 struct nfs4_fs_locations_arg args = {
6839 .fh = NFS_FH(inode),
6840 .page = page,
6841 .bitmask = bitmask,
6842 .migration = 1, /* skip LOOKUP */
6843 };
6844 struct nfs4_fs_locations_res res = {
6845 .fs_locations = locations,
6846 .migration = 1,
6847 };
6848 struct rpc_message msg = {
6849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6850 .rpc_argp = &args,
6851 .rpc_resp = &res,
6852 .rpc_cred = cred,
6853 };
6854 int status;
6855
6856 nfs_fattr_init(&locations->fattr);
6857 locations->server = server;
6858 locations->nlocations = 0;
6859
6860 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6861 nfs4_set_sequence_privileged(&args.seq_args);
6862 status = nfs4_call_sync_sequence(clnt, server, &msg,
6863 &args.seq_args, &res.seq_res);
6864 if (status == NFS4_OK &&
6865 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6866 status = -NFS4ERR_LEASE_MOVED;
6867 return status;
6868}
6869
6870#endif /* CONFIG_NFS_V4_1 */
6871
6872/**
6873 * nfs4_proc_get_locations - discover locations for a migrated FSID
6874 * @inode: inode on FSID that is migrating
6875 * @locations: result of query
6876 * @page: buffer
6877 * @cred: credential to use for this operation
6878 *
6879 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6880 * operation failed, or a negative errno if a local error occurred.
6881 *
6882 * On success, "locations" is filled in, but if the server has
6883 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6884 * asserted.
6885 *
6886 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6887 * from this client that require migration recovery.
6888 */
6889int nfs4_proc_get_locations(struct inode *inode,
6890 struct nfs4_fs_locations *locations,
6891 struct page *page, struct rpc_cred *cred)
6892{
6893 struct nfs_server *server = NFS_SERVER(inode);
6894 struct nfs_client *clp = server->nfs_client;
6895 const struct nfs4_mig_recovery_ops *ops =
6896 clp->cl_mvops->mig_recovery_ops;
6897 struct nfs4_exception exception = { };
6898 int status;
6899
6900 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6901 (unsigned long long)server->fsid.major,
6902 (unsigned long long)server->fsid.minor,
6903 clp->cl_hostname);
6904 nfs_display_fhandle(NFS_FH(inode), __func__);
6905
6906 do {
6907 status = ops->get_locations(inode, locations, page, cred);
6908 if (status != -NFS4ERR_DELAY)
6909 break;
6910 nfs4_handle_exception(server, status, &exception);
6911 } while (exception.retry);
6912 return status;
6913}
6914
Chuck Lever44c99932013-10-17 14:13:30 -04006915/*
6916 * This operation also signals the server that this client is
6917 * performing "lease moved" recovery. The server can stop
6918 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6919 * is appended to this compound to identify the client ID which is
6920 * performing recovery.
6921 */
6922static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6923{
6924 struct nfs_server *server = NFS_SERVER(inode);
6925 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6926 struct rpc_clnt *clnt = server->client;
6927 struct nfs4_fsid_present_arg args = {
6928 .fh = NFS_FH(inode),
6929 .clientid = clp->cl_clientid,
6930 .renew = 1, /* append RENEW */
6931 };
6932 struct nfs4_fsid_present_res res = {
6933 .renew = 1,
6934 };
6935 struct rpc_message msg = {
6936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6937 .rpc_argp = &args,
6938 .rpc_resp = &res,
6939 .rpc_cred = cred,
6940 };
6941 unsigned long now = jiffies;
6942 int status;
6943
6944 res.fh = nfs_alloc_fhandle();
6945 if (res.fh == NULL)
6946 return -ENOMEM;
6947
6948 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6949 nfs4_set_sequence_privileged(&args.seq_args);
6950 status = nfs4_call_sync_sequence(clnt, server, &msg,
6951 &args.seq_args, &res.seq_res);
6952 nfs_free_fhandle(res.fh);
6953 if (status)
6954 return status;
6955
6956 do_renew_lease(clp, now);
6957 return 0;
6958}
6959
6960#ifdef CONFIG_NFS_V4_1
6961
6962/*
6963 * This operation also signals the server that this client is
6964 * performing "lease moved" recovery. The server can stop asserting
6965 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6966 * this operation is identified in the SEQUENCE operation in this
6967 * compound.
6968 */
6969static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6970{
6971 struct nfs_server *server = NFS_SERVER(inode);
6972 struct rpc_clnt *clnt = server->client;
6973 struct nfs4_fsid_present_arg args = {
6974 .fh = NFS_FH(inode),
6975 };
6976 struct nfs4_fsid_present_res res = {
6977 };
6978 struct rpc_message msg = {
6979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6980 .rpc_argp = &args,
6981 .rpc_resp = &res,
6982 .rpc_cred = cred,
6983 };
6984 int status;
6985
6986 res.fh = nfs_alloc_fhandle();
6987 if (res.fh == NULL)
6988 return -ENOMEM;
6989
6990 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6991 nfs4_set_sequence_privileged(&args.seq_args);
6992 status = nfs4_call_sync_sequence(clnt, server, &msg,
6993 &args.seq_args, &res.seq_res);
6994 nfs_free_fhandle(res.fh);
6995 if (status == NFS4_OK &&
6996 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6997 status = -NFS4ERR_LEASE_MOVED;
6998 return status;
6999}
7000
7001#endif /* CONFIG_NFS_V4_1 */
7002
7003/**
7004 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7005 * @inode: inode on FSID to check
7006 * @cred: credential to use for this operation
7007 *
7008 * Server indicates whether the FSID is present, moved, or not
7009 * recognized. This operation is necessary to clear a LEASE_MOVED
7010 * condition for this client ID.
7011 *
7012 * Returns NFS4_OK if the FSID is present on this server,
7013 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7014 * NFS4ERR code if some error occurred on the server, or a
7015 * negative errno if a local failure occurred.
7016 */
7017int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7018{
7019 struct nfs_server *server = NFS_SERVER(inode);
7020 struct nfs_client *clp = server->nfs_client;
7021 const struct nfs4_mig_recovery_ops *ops =
7022 clp->cl_mvops->mig_recovery_ops;
7023 struct nfs4_exception exception = { };
7024 int status;
7025
7026 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7027 (unsigned long long)server->fsid.major,
7028 (unsigned long long)server->fsid.minor,
7029 clp->cl_hostname);
7030 nfs_display_fhandle(NFS_FH(inode), __func__);
7031
7032 do {
7033 status = ops->fsid_present(inode, cred);
7034 if (status != -NFS4ERR_DELAY)
7035 break;
7036 nfs4_handle_exception(server, status, &exception);
7037 } while (exception.retry);
7038 return status;
7039}
7040
Andy Adamson5ec16a82013-08-08 10:57:55 -04007041/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007042 * If 'use_integrity' is true and the state managment nfs_client
7043 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7044 * and the machine credential as per RFC3530bis and RFC5661 Security
7045 * Considerations sections. Otherwise, just use the user cred with the
7046 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007047 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007048static 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 +00007049{
7050 int status;
7051 struct nfs4_secinfo_arg args = {
7052 .dir_fh = NFS_FH(dir),
7053 .name = name,
7054 };
7055 struct nfs4_secinfo_res res = {
7056 .flavors = flavors,
7057 };
7058 struct rpc_message msg = {
7059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7060 .rpc_argp = &args,
7061 .rpc_resp = &res,
7062 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007063 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007064 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007065
7066 if (use_integrity) {
7067 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007068 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7069 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007070 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007071
7072 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007073
7074 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7075 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7076
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007077 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7078 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007079 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007080
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007081 if (cred)
7082 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007083
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007084 return status;
7085}
7086
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007087int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7088 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007089{
7090 struct nfs4_exception exception = { };
7091 int err;
7092 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007093 err = -NFS4ERR_WRONGSEC;
7094
7095 /* try to use integrity protection with machine cred */
7096 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7097 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7098
7099 /*
7100 * if unable to use integrity protection, or SECINFO with
7101 * integrity protection returns NFS4ERR_WRONGSEC (which is
7102 * disallowed by spec, but exists in deployed servers) use
7103 * the current filesystem's rpc_client and the user cred.
7104 */
7105 if (err == -NFS4ERR_WRONGSEC)
7106 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7107
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007108 trace_nfs4_secinfo(dir, name, err);
7109 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007110 &exception);
7111 } while (exception.retry);
7112 return err;
7113}
7114
Andy Adamson557134a2009-04-01 09:21:53 -04007115#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007116/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007117 * Check the exchange flags returned by the server for invalid flags, having
7118 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7119 * DS flags set.
7120 */
7121static int nfs4_check_cl_exchange_flags(u32 flags)
7122{
7123 if (flags & ~EXCHGID4_FLAG_MASK_R)
7124 goto out_inval;
7125 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7126 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7127 goto out_inval;
7128 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7129 goto out_inval;
7130 return NFS_OK;
7131out_inval:
7132 return -NFS4ERR_INVAL;
7133}
7134
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007135static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007136nfs41_same_server_scope(struct nfs41_server_scope *a,
7137 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007138{
7139 if (a->server_scope_sz == b->server_scope_sz &&
7140 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7141 return true;
7142
7143 return false;
7144}
7145
Andy Adamson02a95de2016-02-05 16:08:37 -05007146static void
7147nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7148{
7149}
7150
7151static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7152 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7153};
7154
Andy Adamson357f54d2010-12-14 10:11:57 -05007155/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007156 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007157 *
7158 * The 4.1 client currently uses the same TCP connection for the
7159 * fore and backchannel.
7160 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007161static
7162int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7163 struct rpc_xprt *xprt,
7164 struct nfs_client *clp,
7165 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007166{
7167 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007168 struct nfs41_bind_conn_to_session_args args = {
7169 .client = clp,
7170 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7171 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007172 struct nfs41_bind_conn_to_session_res res;
7173 struct rpc_message msg = {
7174 .rpc_proc =
7175 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007176 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007178 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007179 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007180 struct rpc_task_setup task_setup_data = {
7181 .rpc_client = clnt,
7182 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007183 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007184 .rpc_message = &msg,
7185 .flags = RPC_TASK_TIMEOUT,
7186 };
7187 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007188
7189 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007190
Trond Myklebust71a097c2015-02-18 09:27:18 -08007191 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7192 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7193 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007194
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007195 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7196 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7197 args.dir = NFS4_CDFC4_FORE;
7198
7199 task = rpc_run_task(&task_setup_data);
7200 if (!IS_ERR(task)) {
7201 status = task->tk_status;
7202 rpc_put_task(task);
7203 } else
7204 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007205 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007206 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007207 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007208 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7209 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7210 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007211 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007212 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007213 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007214 dprintk("NFS: %s: Unexpected direction from server\n",
7215 __func__);
7216 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007217 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007218 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007219 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007220 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7221 __func__);
7222 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007223 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007224 }
7225 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007226out:
7227 dprintk("<-- %s status= %d\n", __func__, status);
7228 return status;
7229}
7230
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007231struct rpc_bind_conn_calldata {
7232 struct nfs_client *clp;
7233 struct rpc_cred *cred;
7234};
7235
7236static int
7237nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7238 struct rpc_xprt *xprt,
7239 void *calldata)
7240{
7241 struct rpc_bind_conn_calldata *p = calldata;
7242
7243 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7244}
7245
7246int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7247{
7248 struct rpc_bind_conn_calldata data = {
7249 .clp = clp,
7250 .cred = cred,
7251 };
7252 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7253 nfs4_proc_bind_conn_to_session_callback, &data);
7254}
7255
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007256/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007257 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7258 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007259 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007260static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7261 .how = SP4_MACH_CRED,
7262 .enforce.u.words = {
7263 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7264 1 << (OP_EXCHANGE_ID - 32) |
7265 1 << (OP_CREATE_SESSION - 32) |
7266 1 << (OP_DESTROY_SESSION - 32) |
7267 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007268 },
7269 .allow.u.words = {
7270 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007271 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007272 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007273 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007274 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007275 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007276 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007277 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007278 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007279 1 << (OP_FREE_STATEID - 32) |
7280 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007281 }
7282};
7283
7284/*
7285 * Select the state protection mode for client `clp' given the server results
7286 * from exchange_id in `sp'.
7287 *
7288 * Returns 0 on success, negative errno otherwise.
7289 */
7290static int nfs4_sp4_select_mode(struct nfs_client *clp,
7291 struct nfs41_state_protection *sp)
7292{
7293 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7294 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7295 1 << (OP_EXCHANGE_ID - 32) |
7296 1 << (OP_CREATE_SESSION - 32) |
7297 1 << (OP_DESTROY_SESSION - 32) |
7298 1 << (OP_DESTROY_CLIENTID - 32)
7299 };
7300 unsigned int i;
7301
7302 if (sp->how == SP4_MACH_CRED) {
7303 /* Print state protect result */
7304 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7305 for (i = 0; i <= LAST_NFS4_OP; i++) {
7306 if (test_bit(i, sp->enforce.u.longs))
7307 dfprintk(MOUNT, " enforce op %d\n", i);
7308 if (test_bit(i, sp->allow.u.longs))
7309 dfprintk(MOUNT, " allow op %d\n", i);
7310 }
7311
7312 /* make sure nothing is on enforce list that isn't supported */
7313 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7314 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7315 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7316 return -EINVAL;
7317 }
7318 }
7319
7320 /*
7321 * Minimal mode - state operations are allowed to use machine
7322 * credential. Note this already happens by default, so the
7323 * client doesn't have to do anything more than the negotiation.
7324 *
7325 * NOTE: we don't care if EXCHANGE_ID is in the list -
7326 * we're already using the machine cred for exchange_id
7327 * and will never use a different cred.
7328 */
7329 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7330 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7331 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7332 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7333 dfprintk(MOUNT, "sp4_mach_cred:\n");
7334 dfprintk(MOUNT, " minimal mode enabled\n");
7335 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7336 } else {
7337 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7338 return -EINVAL;
7339 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007340
7341 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007342 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7343 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007344 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7345 dfprintk(MOUNT, " cleanup mode enabled\n");
7346 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7347 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007348
Andrew Elble99ade3c2015-12-02 09:39:51 -05007349 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7350 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7351 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7352 &clp->cl_sp4_flags);
7353 }
7354
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007355 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7356 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7357 dfprintk(MOUNT, " secinfo mode enabled\n");
7358 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7359 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007360
7361 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7362 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7363 dfprintk(MOUNT, " stateid mode enabled\n");
7364 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7365 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007366
7367 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7368 dfprintk(MOUNT, " write mode enabled\n");
7369 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7370 }
7371
7372 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7373 dfprintk(MOUNT, " commit mode enabled\n");
7374 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7375 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007376 }
7377
7378 return 0;
7379}
7380
Andy Adamson8d89bd72016-09-09 09:22:18 -04007381struct nfs41_exchange_id_data {
7382 struct nfs41_exchange_id_res res;
7383 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007384 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007385 int rpc_status;
7386};
7387
7388static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007389{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007390 struct nfs41_exchange_id_data *cdata =
7391 (struct nfs41_exchange_id_data *)data;
7392 struct nfs_client *clp = cdata->args.client;
7393 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007394
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007395 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007396
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007397 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007398 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007399
7400 if (cdata->xprt && status == 0) {
7401 status = nfs4_detect_session_trunking(clp, &cdata->res,
7402 cdata->xprt);
7403 goto out;
7404 }
7405
Andy Adamson8d89bd72016-09-09 09:22:18 -04007406 if (status == 0)
7407 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007408
Chuck Lever177313f2012-05-21 22:44:58 -04007409 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007410 clp->cl_clientid = cdata->res.clientid;
7411 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007412 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007413 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007414 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007415 &clp->cl_session->session_state);
7416 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007417 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007418
Chuck Leveracdeb692012-05-21 22:46:16 -04007419 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007420 clp->cl_serverowner = cdata->res.server_owner;
7421 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007422
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007423 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007424 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007425 clp->cl_implid = cdata->res.impl_id;
7426 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007427
Chuck Lever177313f2012-05-21 22:44:58 -04007428 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007429 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007430 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007431 dprintk("%s: server_scope mismatch detected\n",
7432 __func__);
7433 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007434 kfree(clp->cl_serverscope);
7435 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007436 }
7437
Chuck Lever177313f2012-05-21 22:44:58 -04007438 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007439 clp->cl_serverscope = cdata->res.server_scope;
7440 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007441 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007442 /* Save the EXCHANGE_ID verifier session trunk tests */
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007443 memcpy(clp->cl_confirm.data, cdata->args.verifier.data,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007444 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007445 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007446out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007447 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007448 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007449}
7450
7451static void nfs4_exchange_id_release(void *data)
7452{
7453 struct nfs41_exchange_id_data *cdata =
7454 (struct nfs41_exchange_id_data *)data;
7455
Andy Adamsonad0849a2016-09-09 09:22:28 -04007456 if (cdata->xprt) {
7457 xprt_put(cdata->xprt);
7458 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7459 }
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007460 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007461 kfree(cdata->res.impl_id);
7462 kfree(cdata->res.server_scope);
7463 kfree(cdata->res.server_owner);
7464 kfree(cdata);
7465}
7466
7467static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7468 .rpc_call_done = nfs4_exchange_id_done,
7469 .rpc_release = nfs4_exchange_id_release,
7470};
7471
Benny Halevy99fe60d2009-04-01 09:22:29 -04007472/*
7473 * _nfs4_proc_exchange_id()
7474 *
7475 * Wrapper for EXCHANGE_ID operation.
7476 */
7477static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007478 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007479{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007480 struct rpc_message msg = {
7481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007482 .rpc_cred = cred,
7483 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007484 struct rpc_task_setup task_setup_data = {
7485 .rpc_client = clp->cl_rpcclient,
7486 .callback_ops = &nfs4_exchange_id_call_ops,
7487 .rpc_message = &msg,
7488 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7489 };
7490 struct nfs41_exchange_id_data *calldata;
7491 struct rpc_task *task;
7492 int status = -EIO;
7493
7494 if (!atomic_inc_not_zero(&clp->cl_count))
7495 goto out;
7496
7497 status = -ENOMEM;
7498 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7499 if (!calldata)
7500 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007501
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007502 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007503
7504 status = nfs4_init_uniform_client_string(clp);
7505 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007506 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007507
7508 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7509 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7510 clp->cl_owner_id);
7511
Andy Adamson8d89bd72016-09-09 09:22:18 -04007512 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7513 GFP_NOFS);
7514 status = -ENOMEM;
7515 if (unlikely(calldata->res.server_owner == NULL))
7516 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007517
Andy Adamson8d89bd72016-09-09 09:22:18 -04007518 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007519 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007520 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007521 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007522
Andy Adamson8d89bd72016-09-09 09:22:18 -04007523 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7524 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007525 goto out_server_scope;
7526
7527 switch (sp4_how) {
7528 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007529 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007530 break;
7531
7532 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007533 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007534 break;
7535
7536 default:
7537 /* unsupported! */
7538 WARN_ON_ONCE(1);
7539 status = -EINVAL;
7540 goto out_impl_id;
7541 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007542 if (xprt) {
7543 calldata->xprt = xprt;
7544 task_setup_data.rpc_xprt = xprt;
7545 task_setup_data.flags =
7546 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
Trond Myklebustf7d3e542017-08-01 16:02:47 -04007547 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7548 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007549 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007550 calldata->args.client = clp;
7551#ifdef CONFIG_NFS_V4_1_MIGRATION
7552 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7553 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7554 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7555#else
7556 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7557 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7558#endif
7559 msg.rpc_argp = &calldata->args;
7560 msg.rpc_resp = &calldata->res;
7561 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007562
Andy Adamson8d89bd72016-09-09 09:22:18 -04007563 task = rpc_run_task(&task_setup_data);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007564 if (IS_ERR(task))
7565 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007566
Andy Adamsonad0849a2016-09-09 09:22:28 -04007567 if (!xprt) {
7568 status = rpc_wait_for_completion_task(task);
7569 if (!status)
7570 status = calldata->rpc_status;
7571 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007572 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007573
Andy Adamson8d89bd72016-09-09 09:22:18 -04007574 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007575out:
Chuck Lever177313f2012-05-21 22:44:58 -04007576 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007577 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007578 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007579 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007580 clp->cl_implid->date.seconds,
7581 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007582 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007583 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007584
7585out_impl_id:
7586 kfree(calldata->res.impl_id);
7587out_server_scope:
7588 kfree(calldata->res.server_scope);
7589out_server_owner:
7590 kfree(calldata->res.server_owner);
7591out_calldata:
7592 kfree(calldata);
Olga Kornievskaiab76d4fb2017-03-13 10:36:19 -04007593 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007594 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007595}
7596
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007597/*
7598 * nfs4_proc_exchange_id()
7599 *
7600 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7601 *
7602 * Since the clientid has expired, all compounds using sessions
7603 * associated with the stale clientid will be returning
7604 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7605 * be in some phase of session reset.
7606 *
7607 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7608 */
7609int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7610{
7611 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7612 int status;
7613
7614 /* try SP4_MACH_CRED if krb5i/p */
7615 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7616 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007617 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007618 if (!status)
7619 return 0;
7620 }
7621
7622 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007623 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007624}
7625
Andy Adamson04fa2c62016-09-09 09:22:29 -04007626/**
7627 * nfs4_test_session_trunk
7628 *
7629 * This is an add_xprt_test() test function called from
7630 * rpc_clnt_setup_test_and_add_xprt.
7631 *
7632 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7633 * and is dereferrenced in nfs4_exchange_id_release
7634 *
7635 * Upon success, add the new transport to the rpc_clnt
7636 *
7637 * @clnt: struct rpc_clnt to get new transport
7638 * @xprt: the rpc_xprt to test
7639 * @data: call data for _nfs4_proc_exchange_id.
7640 */
7641int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7642 void *data)
7643{
7644 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7645 u32 sp4_how;
7646
7647 dprintk("--> %s try %s\n", __func__,
7648 xprt->address_strings[RPC_DISPLAY_ADDR]);
7649
7650 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7651
7652 /* Test connection for session trunking. Async exchange_id call */
7653 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7654}
7655EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7656
Trond Myklebust66245532012-05-25 17:18:09 -04007657static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7658 struct rpc_cred *cred)
7659{
7660 struct rpc_message msg = {
7661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7662 .rpc_argp = clp,
7663 .rpc_cred = cred,
7664 };
7665 int status;
7666
7667 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007668 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007669 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007670 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007671 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7672 return status;
7673}
7674
7675static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7676 struct rpc_cred *cred)
7677{
7678 unsigned int loop;
7679 int ret;
7680
7681 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7682 ret = _nfs4_proc_destroy_clientid(clp, cred);
7683 switch (ret) {
7684 case -NFS4ERR_DELAY:
7685 case -NFS4ERR_CLIENTID_BUSY:
7686 ssleep(1);
7687 break;
7688 default:
7689 return ret;
7690 }
7691 }
7692 return 0;
7693}
7694
7695int nfs4_destroy_clientid(struct nfs_client *clp)
7696{
7697 struct rpc_cred *cred;
7698 int ret = 0;
7699
7700 if (clp->cl_mvops->minor_version < 1)
7701 goto out;
7702 if (clp->cl_exchange_flags == 0)
7703 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007704 if (clp->cl_preserve_clid)
7705 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007706 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007707 ret = nfs4_proc_destroy_clientid(clp, cred);
7708 if (cred)
7709 put_rpccred(cred);
7710 switch (ret) {
7711 case 0:
7712 case -NFS4ERR_STALE_CLIENTID:
7713 clp->cl_exchange_flags = 0;
7714 }
7715out:
7716 return ret;
7717}
7718
Andy Adamson2050f0c2009-04-01 09:22:30 -04007719struct nfs4_get_lease_time_data {
7720 struct nfs4_get_lease_time_args *args;
7721 struct nfs4_get_lease_time_res *res;
7722 struct nfs_client *clp;
7723};
7724
7725static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7726 void *calldata)
7727{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007728 struct nfs4_get_lease_time_data *data =
7729 (struct nfs4_get_lease_time_data *)calldata;
7730
7731 dprintk("--> %s\n", __func__);
7732 /* just setup sequence, do not trigger session recovery
7733 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007734 nfs41_setup_sequence(data->clp->cl_session,
7735 &data->args->la_seq_args,
7736 &data->res->lr_seq_res,
7737 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007738 dprintk("<-- %s\n", __func__);
7739}
7740
7741/*
7742 * Called from nfs4_state_manager thread for session setup, so don't recover
7743 * from sequence operation or clientid errors.
7744 */
7745static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7746{
7747 struct nfs4_get_lease_time_data *data =
7748 (struct nfs4_get_lease_time_data *)calldata;
7749
7750 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007751 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7752 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007753 switch (task->tk_status) {
7754 case -NFS4ERR_DELAY:
7755 case -NFS4ERR_GRACE:
7756 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7757 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7758 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007759 /* fall through */
7760 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007761 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007762 return;
7763 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007764 dprintk("<-- %s\n", __func__);
7765}
7766
Trond Myklebust17280172012-03-11 13:11:00 -04007767static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007768 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7769 .rpc_call_done = nfs4_get_lease_time_done,
7770};
7771
7772int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7773{
7774 struct rpc_task *task;
7775 struct nfs4_get_lease_time_args args;
7776 struct nfs4_get_lease_time_res res = {
7777 .lr_fsinfo = fsinfo,
7778 };
7779 struct nfs4_get_lease_time_data data = {
7780 .args = &args,
7781 .res = &res,
7782 .clp = clp,
7783 };
7784 struct rpc_message msg = {
7785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7786 .rpc_argp = &args,
7787 .rpc_resp = &res,
7788 };
7789 struct rpc_task_setup task_setup = {
7790 .rpc_client = clp->cl_rpcclient,
7791 .rpc_message = &msg,
7792 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007793 .callback_data = &data,
7794 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007795 };
7796 int status;
7797
Chuck Levera9c92d62013-08-09 12:48:18 -04007798 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007799 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007800 dprintk("--> %s\n", __func__);
7801 task = rpc_run_task(&task_setup);
7802
7803 if (IS_ERR(task))
7804 status = PTR_ERR(task);
7805 else {
7806 status = task->tk_status;
7807 rpc_put_task(task);
7808 }
7809 dprintk("<-- %s return %d\n", __func__, status);
7810
7811 return status;
7812}
7813
Andy Adamsonfc931582009-04-01 09:22:31 -04007814/*
7815 * Initialize the values to be used by the client in CREATE_SESSION
7816 * If nfs4_init_session set the fore channel request and response sizes,
7817 * use them.
7818 *
7819 * Set the back channel max_resp_sz_cached to zero to force the client to
7820 * always set csa_cachethis to FALSE because the current implementation
7821 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7822 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007823static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7824 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007825{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007826 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007827 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007828
Andy Adamson18aad3d2013-06-26 12:21:49 -04007829 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7830 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7831
Andy Adamsonfc931582009-04-01 09:22:31 -04007832 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007833 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7834 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007835 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007836 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007837
7838 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007839 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007840 __func__,
7841 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007842 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007843
7844 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007845 args->bc_attrs.max_rqst_sz = max_bc_payload;
7846 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007847 args->bc_attrs.max_resp_sz_cached = 0;
7848 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007849 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007850
7851 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7852 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7853 __func__,
7854 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7855 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7856 args->bc_attrs.max_reqs);
7857}
7858
Trond Myklebust79969dd2015-02-18 11:30:18 -08007859static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7860 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007861{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007862 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007863 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007864
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007865 if (rcvd->max_resp_sz > sent->max_resp_sz)
7866 return -EINVAL;
7867 /*
7868 * Our requested max_ops is the minimum we need; we're not
7869 * prepared to break up compounds into smaller pieces than that.
7870 * So, no point even trying to continue if the server won't
7871 * cooperate:
7872 */
7873 if (rcvd->max_ops < sent->max_ops)
7874 return -EINVAL;
7875 if (rcvd->max_reqs == 0)
7876 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007877 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7878 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007879 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007880}
7881
Trond Myklebust79969dd2015-02-18 11:30:18 -08007882static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7883 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007884{
7885 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007886 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007887
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007888 if (!(res->flags & SESSION4_BACK_CHAN))
7889 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007890 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7891 return -EINVAL;
7892 if (rcvd->max_resp_sz < sent->max_resp_sz)
7893 return -EINVAL;
7894 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7895 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007896 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007897 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007898 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007899 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007900out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007901 return 0;
7902}
Andy Adamson8d353012009-04-01 09:22:32 -04007903
Andy Adamson8d353012009-04-01 09:22:32 -04007904static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007905 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007906{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007907 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007908
Trond Myklebust79969dd2015-02-18 11:30:18 -08007909 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007910 if (ret)
7911 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007912 return nfs4_verify_back_channel_attrs(args, res);
7913}
7914
7915static void nfs4_update_session(struct nfs4_session *session,
7916 struct nfs41_create_session_res *res)
7917{
7918 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007919 /* Mark client id and session as being confirmed */
7920 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7921 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007922 session->flags = res->flags;
7923 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007924 if (res->flags & SESSION4_BACK_CHAN)
7925 memcpy(&session->bc_attrs, &res->bc_attrs,
7926 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007927}
7928
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007929static int _nfs4_proc_create_session(struct nfs_client *clp,
7930 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007931{
7932 struct nfs4_session *session = clp->cl_session;
7933 struct nfs41_create_session_args args = {
7934 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007935 .clientid = clp->cl_clientid,
7936 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007937 .cb_program = NFS4_CALLBACK,
7938 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007939 struct nfs41_create_session_res res;
7940
Andy Adamsonfc931582009-04-01 09:22:31 -04007941 struct rpc_message msg = {
7942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7943 .rpc_argp = &args,
7944 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007945 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007946 };
7947 int status;
7948
Chuck Lever6b26cc82016-05-02 14:40:40 -04007949 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007950 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007951
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007952 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007953 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007954
Trond Myklebustb519d402016-09-11 14:50:01 -04007955 switch (status) {
7956 case -NFS4ERR_STALE_CLIENTID:
7957 case -NFS4ERR_DELAY:
7958 case -ETIMEDOUT:
7959 case -EACCES:
7960 case -EAGAIN:
7961 goto out;
7962 };
7963
7964 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007965 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007966 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007967 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007968 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007969 if (status)
7970 goto out;
7971 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007972 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007973out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007974 return status;
7975}
7976
7977/*
7978 * Issues a CREATE_SESSION operation to the server.
7979 * It is the responsibility of the caller to verify the session is
7980 * expired before calling this routine.
7981 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007982int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007983{
7984 int status;
7985 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007986 struct nfs4_session *session = clp->cl_session;
7987
7988 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7989
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007990 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007991 if (status)
7992 goto out;
7993
Andy Adamsonaacd5532011-11-09 13:58:21 -05007994 /* Init or reset the session slot tables */
7995 status = nfs4_setup_session_slot_tables(session);
7996 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007997 if (status)
7998 goto out;
7999
8000 ptr = (unsigned *)&session->sess_id.data[0];
8001 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8002 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008003out:
8004 dprintk("<-- %s\n", __func__);
8005 return status;
8006}
8007
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008008/*
8009 * Issue the over-the-wire RPC DESTROY_SESSION.
8010 * The caller must serialize access to this routine.
8011 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008012int nfs4_proc_destroy_session(struct nfs4_session *session,
8013 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008014{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008015 struct rpc_message msg = {
8016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8017 .rpc_argp = session,
8018 .rpc_cred = cred,
8019 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008020 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008021
8022 dprintk("--> nfs4_proc_destroy_session\n");
8023
8024 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008025 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8026 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008027
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008028 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008029 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008030
8031 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008032 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008033 "Session has been destroyed regardless...\n", status);
8034
8035 dprintk("<-- nfs4_proc_destroy_session\n");
8036 return status;
8037}
8038
Trond Myklebust7b38c362012-05-23 13:23:31 -04008039/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008040 * Renew the cl_session lease.
8041 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008042struct nfs4_sequence_data {
8043 struct nfs_client *clp;
8044 struct nfs4_sequence_args args;
8045 struct nfs4_sequence_res res;
8046};
8047
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008048static void nfs41_sequence_release(void *data)
8049{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008050 struct nfs4_sequence_data *calldata = data;
8051 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008052
Alexandros Batsakis71358402010-02-05 03:45:05 -08008053 if (atomic_read(&clp->cl_count) > 1)
8054 nfs4_schedule_state_renewal(clp);
8055 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008056 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008057}
8058
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008059static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8060{
8061 switch(task->tk_status) {
8062 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008063 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8064 return -EAGAIN;
8065 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008066 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008067 }
8068 return 0;
8069}
8070
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008071static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008072{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008073 struct nfs4_sequence_data *calldata = data;
8074 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008075
Trond Myklebust14516c32010-07-31 14:29:06 -04008076 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8077 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008079 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008080 if (task->tk_status < 0) {
8081 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008082 if (atomic_read(&clp->cl_count) == 1)
8083 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008084
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008085 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8086 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008087 return;
8088 }
8089 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008090 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008091out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008092 dprintk("<-- %s\n", __func__);
8093}
8094
8095static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8096{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008097 struct nfs4_sequence_data *calldata = data;
8098 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008099 struct nfs4_sequence_args *args;
8100 struct nfs4_sequence_res *res;
8101
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008102 args = task->tk_msg.rpc_argp;
8103 res = task->tk_msg.rpc_resp;
8104
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008105 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008106}
8107
8108static const struct rpc_call_ops nfs41_sequence_ops = {
8109 .rpc_call_done = nfs41_sequence_call_done,
8110 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008111 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008112};
8113
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008114static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8115 struct rpc_cred *cred,
8116 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008117{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008118 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008119 struct rpc_message msg = {
8120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8121 .rpc_cred = cred,
8122 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008123 struct rpc_task_setup task_setup_data = {
8124 .rpc_client = clp->cl_rpcclient,
8125 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008126 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008127 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008128 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008129
Alexandros Batsakis71358402010-02-05 03:45:05 -08008130 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008131 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008132 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008133 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008134 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008135 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008136 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008137 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008138 if (is_privileged)
8139 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008140 msg.rpc_argp = &calldata->args;
8141 msg.rpc_resp = &calldata->res;
8142 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008143 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008144
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008145 return rpc_run_task(&task_setup_data);
8146}
8147
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008148static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008149{
8150 struct rpc_task *task;
8151 int ret = 0;
8152
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008153 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008154 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008155 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008156 if (IS_ERR(task))
8157 ret = PTR_ERR(task);
8158 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008159 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008160 dprintk("<-- %s status=%d\n", __func__, ret);
8161 return ret;
8162}
8163
8164static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8165{
8166 struct rpc_task *task;
8167 int ret;
8168
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008169 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008170 if (IS_ERR(task)) {
8171 ret = PTR_ERR(task);
8172 goto out;
8173 }
8174 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008175 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008176 ret = task->tk_status;
8177 rpc_put_task(task);
8178out:
8179 dprintk("<-- %s status=%d\n", __func__, ret);
8180 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008181}
8182
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008183struct nfs4_reclaim_complete_data {
8184 struct nfs_client *clp;
8185 struct nfs41_reclaim_complete_args arg;
8186 struct nfs41_reclaim_complete_res res;
8187};
8188
8189static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8190{
8191 struct nfs4_reclaim_complete_data *calldata = data;
8192
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008193 nfs41_setup_sequence(calldata->clp->cl_session,
8194 &calldata->arg.seq_args,
8195 &calldata->res.seq_res,
8196 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008197}
8198
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008199static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8200{
8201 switch(task->tk_status) {
8202 case 0:
8203 case -NFS4ERR_COMPLETE_ALREADY:
8204 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8205 break;
8206 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008207 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008208 /* fall through */
8209 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008210 return -EAGAIN;
Trond Myklebust3032bcc2017-05-04 13:44:04 -04008211 case -NFS4ERR_BADSESSION:
8212 case -NFS4ERR_DEADSESSION:
8213 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8214 nfs4_schedule_session_recovery(clp->cl_session,
8215 task->tk_status);
8216 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008217 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008218 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008219 }
8220 return 0;
8221}
8222
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008223static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8224{
8225 struct nfs4_reclaim_complete_data *calldata = data;
8226 struct nfs_client *clp = calldata->clp;
8227 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8228
8229 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008230 if (!nfs41_sequence_done(task, res))
8231 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008232
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008233 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008234 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8235 rpc_restart_call_prepare(task);
8236 return;
8237 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008238 dprintk("<-- %s\n", __func__);
8239}
8240
8241static void nfs4_free_reclaim_complete_data(void *data)
8242{
8243 struct nfs4_reclaim_complete_data *calldata = data;
8244
8245 kfree(calldata);
8246}
8247
8248static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8249 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8250 .rpc_call_done = nfs4_reclaim_complete_done,
8251 .rpc_release = nfs4_free_reclaim_complete_data,
8252};
8253
8254/*
8255 * Issue a global reclaim complete.
8256 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008257static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8258 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008259{
8260 struct nfs4_reclaim_complete_data *calldata;
8261 struct rpc_task *task;
8262 struct rpc_message msg = {
8263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008264 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008265 };
8266 struct rpc_task_setup task_setup_data = {
8267 .rpc_client = clp->cl_rpcclient,
8268 .rpc_message = &msg,
8269 .callback_ops = &nfs4_reclaim_complete_call_ops,
8270 .flags = RPC_TASK_ASYNC,
8271 };
8272 int status = -ENOMEM;
8273
8274 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008275 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008276 if (calldata == NULL)
8277 goto out;
8278 calldata->clp = clp;
8279 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008280
Chuck Levera9c92d62013-08-09 12:48:18 -04008281 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008282 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008283 msg.rpc_argp = &calldata->arg;
8284 msg.rpc_resp = &calldata->res;
8285 task_setup_data.callback_data = calldata;
8286 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008287 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008288 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008289 goto out;
8290 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008291 status = nfs4_wait_for_completion_rpc_task(task);
8292 if (status == 0)
8293 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008294 rpc_put_task(task);
8295out:
8296 dprintk("<-- %s status=%d\n", __func__, status);
8297 return status;
8298}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008299
8300static void
8301nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8302{
8303 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008304 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008305 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008306
8307 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008308 nfs41_setup_sequence(session, &lgp->args.seq_args,
8309 &lgp->res.seq_res, task);
8310 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008311}
8312
8313static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8314{
8315 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008316
8317 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008318 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008319 dprintk("<-- %s\n", __func__);
8320}
8321
8322static int
8323nfs4_layoutget_handle_exception(struct rpc_task *task,
8324 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8325{
Trond Myklebustee314c22012-10-01 17:25:48 -07008326 struct inode *inode = lgp->args.inode;
8327 struct nfs_server *server = NFS_SERVER(inode);
8328 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008329 int nfs4err = task->tk_status;
8330 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008331 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008332
Boaz Harroshed7e5422014-01-22 20:34:54 +02008333 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008334
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008335 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008336 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008337 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008338
8339 /*
8340 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8341 * on the file. set tk_status to -ENODATA to tell upper layer to
8342 * retry go inband.
8343 */
8344 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008345 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008346 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008347 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008348 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8349 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8350 */
8351 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008352 status = -EOVERFLOW;
8353 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008354 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008355 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008356 * (or clients) writing to the same RAID stripe except when
8357 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008358 *
8359 * Treat it like we would RECALLCONFLICT -- we retry for a little
8360 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008361 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008362 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008363 if (lgp->args.minlength == 0) {
8364 status = -EOVERFLOW;
8365 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008366 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008367 status = -EBUSY;
8368 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008369 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008370 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008371 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008372 case -NFS4ERR_DELEG_REVOKED:
8373 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008374 case -NFS4ERR_EXPIRED:
8375 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008376 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008377 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008378 lo = NFS_I(inode)->layout;
8379 /* If the open stateid was bad, then recover it. */
8380 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8381 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008382 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008383 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008384 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008385 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008386 break;
8387 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008388
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008389 /*
8390 * Mark the bad layout state as invalid, then retry
8391 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008392 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008393 spin_unlock(&inode->i_lock);
8394 pnfs_free_lseg_list(&head);
8395 status = -EAGAIN;
8396 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008397 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008398
Trond Myklebust0a702352017-01-23 22:44:12 -05008399 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008400 err = nfs4_handle_exception(server, nfs4err, exception);
8401 if (!status) {
8402 if (exception->retry)
8403 status = -EAGAIN;
8404 else
8405 status = err;
8406 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008407out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008408 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008409 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008410}
8411
Idan Kedar85541162012-08-02 11:47:10 +03008412static size_t max_response_pages(struct nfs_server *server)
8413{
8414 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8415 return nfs_page_array_len(0, max_resp_sz);
8416}
8417
8418static void nfs4_free_pages(struct page **pages, size_t size)
8419{
8420 int i;
8421
8422 if (!pages)
8423 return;
8424
8425 for (i = 0; i < size; i++) {
8426 if (!pages[i])
8427 break;
8428 __free_page(pages[i]);
8429 }
8430 kfree(pages);
8431}
8432
8433static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8434{
8435 struct page **pages;
8436 int i;
8437
8438 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8439 if (!pages) {
8440 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8441 return NULL;
8442 }
8443
8444 for (i = 0; i < size; i++) {
8445 pages[i] = alloc_page(gfp_flags);
8446 if (!pages[i]) {
8447 dprintk("%s: failed to allocate page\n", __func__);
8448 nfs4_free_pages(pages, size);
8449 return NULL;
8450 }
8451 }
8452
8453 return pages;
8454}
8455
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008456static void nfs4_layoutget_release(void *calldata)
8457{
8458 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008459 struct inode *inode = lgp->args.inode;
8460 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008461 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008462
8463 dprintk("--> %s\n", __func__);
Trond Myklebustcb2c6fd2017-06-27 17:33:38 -04008464 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008465 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008466 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008467 put_nfs_open_context(lgp->args.ctx);
8468 kfree(calldata);
8469 dprintk("<-- %s\n", __func__);
8470}
8471
8472static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8473 .rpc_call_prepare = nfs4_layoutget_prepare,
8474 .rpc_call_done = nfs4_layoutget_done,
8475 .rpc_release = nfs4_layoutget_release,
8476};
8477
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008478struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008479nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008480{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008481 struct inode *inode = lgp->args.inode;
8482 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008483 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008484 struct rpc_task *task;
8485 struct rpc_message msg = {
8486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8487 .rpc_argp = &lgp->args,
8488 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008489 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008490 };
8491 struct rpc_task_setup task_setup_data = {
8492 .rpc_client = server->client,
8493 .rpc_message = &msg,
8494 .callback_ops = &nfs4_layoutget_call_ops,
8495 .callback_data = lgp,
8496 .flags = RPC_TASK_ASYNC,
8497 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008498 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008499 struct nfs4_exception exception = {
8500 .inode = inode,
8501 .timeout = *timeout,
8502 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008503 int status = 0;
8504
8505 dprintk("--> %s\n", __func__);
8506
Peng Tao4bd5a982014-11-17 11:05:17 +08008507 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8508 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8509
Idan Kedar85541162012-08-02 11:47:10 +03008510 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8511 if (!lgp->args.layout.pages) {
8512 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008513 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008514 }
8515 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8516
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008517 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008518 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008519 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008520
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008521 task = rpc_run_task(&task_setup_data);
8522 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008523 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008524 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008525 if (status == 0) {
8526 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8527 *timeout = exception.timeout;
8528 }
8529
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008530 trace_nfs4_layoutget(lgp->args.ctx,
8531 &lgp->args.range,
8532 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008533 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008534 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008535
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008536 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8537 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008538 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008539 rpc_put_task(task);
8540 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008541 if (status)
8542 return ERR_PTR(status);
8543 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008544}
8545
Benny Halevycbe82602011-05-22 19:52:37 +03008546static void
8547nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8548{
8549 struct nfs4_layoutreturn *lrp = calldata;
8550
8551 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008552 nfs41_setup_sequence(lrp->clp->cl_session,
8553 &lrp->args.seq_args,
8554 &lrp->res.seq_res,
8555 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008556}
8557
8558static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8559{
8560 struct nfs4_layoutreturn *lrp = calldata;
8561 struct nfs_server *server;
8562
8563 dprintk("--> %s\n", __func__);
8564
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008565 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008566 return;
8567
8568 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008569 switch (task->tk_status) {
8570 default:
8571 task->tk_status = 0;
8572 case 0:
8573 break;
8574 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008575 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008576 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008577 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008578 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008579 return;
8580 }
Benny Halevycbe82602011-05-22 19:52:37 +03008581 dprintk("<-- %s\n", __func__);
8582}
8583
8584static void nfs4_layoutreturn_release(void *calldata)
8585{
8586 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008587 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008588 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008589
8590 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008591 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008592 if (lrp->res.lrs_present) {
8593 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8594 &lrp->args.range,
8595 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust849b2862012-09-24 14:18:39 -04008596 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Trond Myklebust52ec7be2016-09-03 11:05:28 -04008597 } else
8598 pnfs_mark_layout_stateid_invalid(lo, &freeme);
Tom Haynesd67ae822014-12-11 17:02:04 -05008599 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008600 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008601 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008602 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008603 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008604 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008605 kfree(calldata);
8606 dprintk("<-- %s\n", __func__);
8607}
8608
8609static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8610 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8611 .rpc_call_done = nfs4_layoutreturn_done,
8612 .rpc_release = nfs4_layoutreturn_release,
8613};
8614
Peng Tao6c166052014-11-17 09:30:40 +08008615int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008616{
8617 struct rpc_task *task;
8618 struct rpc_message msg = {
8619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8620 .rpc_argp = &lrp->args,
8621 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008622 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008623 };
8624 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008625 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008626 .rpc_message = &msg,
8627 .callback_ops = &nfs4_layoutreturn_call_ops,
8628 .callback_data = lrp,
8629 };
Peng Tao6c166052014-11-17 09:30:40 +08008630 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008631
Andrew Elble99ade3c2015-12-02 09:39:51 -05008632 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8633 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8634 &task_setup_data.rpc_client, &msg);
8635
Benny Halevycbe82602011-05-22 19:52:37 +03008636 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008637 if (!sync) {
8638 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8639 if (!lrp->inode) {
8640 nfs4_layoutreturn_release(lrp);
8641 return -EAGAIN;
8642 }
8643 task_setup_data.flags |= RPC_TASK_ASYNC;
8644 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008645 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008646 task = rpc_run_task(&task_setup_data);
8647 if (IS_ERR(task))
8648 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008649 if (sync)
8650 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008651 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008652 dprintk("<-- %s status=%d\n", __func__, status);
8653 rpc_put_task(task);
8654 return status;
8655}
8656
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008657static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008658_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8659 struct pnfs_device *pdev,
8660 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008661{
8662 struct nfs4_getdeviceinfo_args args = {
8663 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008664 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8665 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008666 };
8667 struct nfs4_getdeviceinfo_res res = {
8668 .pdev = pdev,
8669 };
8670 struct rpc_message msg = {
8671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8672 .rpc_argp = &args,
8673 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008674 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008675 };
8676 int status;
8677
8678 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008679 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008680 if (res.notification & ~args.notify_types)
8681 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008682 if (res.notification != args.notify_types)
8683 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008684
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008685 dprintk("<-- %s status=%d\n", __func__, status);
8686
8687 return status;
8688}
8689
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008690int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8691 struct pnfs_device *pdev,
8692 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008693{
8694 struct nfs4_exception exception = { };
8695 int err;
8696
8697 do {
8698 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008699 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008700 &exception);
8701 } while (exception.retry);
8702 return err;
8703}
8704EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8705
Andy Adamson863a3c62011-03-23 13:27:54 +00008706static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8707{
8708 struct nfs4_layoutcommit_data *data = calldata;
8709 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008710 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008711
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008712 nfs41_setup_sequence(session,
8713 &data->args.seq_args,
8714 &data->res.seq_res,
8715 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008716}
8717
8718static void
8719nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8720{
8721 struct nfs4_layoutcommit_data *data = calldata;
8722 struct nfs_server *server = NFS_SERVER(data->args.inode);
8723
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008724 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008725 return;
8726
8727 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008728 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8729 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8730 case -NFS4ERR_BADLAYOUT: /* no layout */
8731 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008732 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008733 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008734 break;
8735 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008736 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008737 rpc_restart_call_prepare(task);
8738 return;
8739 }
8740 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008741}
8742
8743static void nfs4_layoutcommit_release(void *calldata)
8744{
8745 struct nfs4_layoutcommit_data *data = calldata;
8746
Andy Adamsondb29c082011-07-30 20:52:38 -04008747 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008748 nfs_post_op_update_inode_force_wcc(data->args.inode,
8749 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008750 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008751 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008752 kfree(data);
8753}
8754
8755static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8756 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8757 .rpc_call_done = nfs4_layoutcommit_done,
8758 .rpc_release = nfs4_layoutcommit_release,
8759};
8760
8761int
Andy Adamsonef311532011-03-12 02:58:10 -05008762nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008763{
8764 struct rpc_message msg = {
8765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8766 .rpc_argp = &data->args,
8767 .rpc_resp = &data->res,
8768 .rpc_cred = data->cred,
8769 };
8770 struct rpc_task_setup task_setup_data = {
8771 .task = &data->task,
8772 .rpc_client = NFS_CLIENT(data->args.inode),
8773 .rpc_message = &msg,
8774 .callback_ops = &nfs4_layoutcommit_ops,
8775 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008776 };
8777 struct rpc_task *task;
8778 int status = 0;
8779
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008780 dprintk("NFS: initiating layoutcommit call. sync %d "
8781 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008782 data->args.lastbytewritten,
8783 data->args.inode->i_ino);
8784
Trond Myklebust472e2592015-02-05 16:50:30 -05008785 if (!sync) {
8786 data->inode = nfs_igrab_and_active(data->args.inode);
8787 if (data->inode == NULL) {
8788 nfs4_layoutcommit_release(data);
8789 return -EAGAIN;
8790 }
8791 task_setup_data.flags = RPC_TASK_ASYNC;
8792 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008793 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008794 task = rpc_run_task(&task_setup_data);
8795 if (IS_ERR(task))
8796 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008797 if (sync)
8798 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008799 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008800 dprintk("%s: status %d\n", __func__, status);
8801 rpc_put_task(task);
8802 return status;
8803}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008804
Andy Adamson97431202013-08-08 10:57:56 -04008805/**
8806 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8807 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8808 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008809static int
8810_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008811 struct nfs_fsinfo *info,
8812 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008813{
8814 struct nfs41_secinfo_no_name_args args = {
8815 .style = SECINFO_STYLE_CURRENT_FH,
8816 };
8817 struct nfs4_secinfo_res res = {
8818 .flavors = flavors,
8819 };
8820 struct rpc_message msg = {
8821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8822 .rpc_argp = &args,
8823 .rpc_resp = &res,
8824 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008825 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008826 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008827 int status;
8828
8829 if (use_integrity) {
8830 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008831 cred = nfs4_get_clid_cred(server->nfs_client);
8832 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008833 }
8834
8835 dprintk("--> %s\n", __func__);
8836 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8837 &res.seq_res, 0);
8838 dprintk("<-- %s status=%d\n", __func__, status);
8839
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008840 if (cred)
8841 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008842
8843 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008844}
8845
8846static int
8847nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8848 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8849{
8850 struct nfs4_exception exception = { };
8851 int err;
8852 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008853 /* first try using integrity protection */
8854 err = -NFS4ERR_WRONGSEC;
8855
8856 /* try to use integrity protection with machine cred */
8857 if (_nfs4_is_integrity_protected(server->nfs_client))
8858 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8859 flavors, true);
8860
8861 /*
8862 * if unable to use integrity protection, or SECINFO with
8863 * integrity protection returns NFS4ERR_WRONGSEC (which is
8864 * disallowed by spec, but exists in deployed servers) use
8865 * the current filesystem's rpc_client and the user cred.
8866 */
8867 if (err == -NFS4ERR_WRONGSEC)
8868 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8869 flavors, false);
8870
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008871 switch (err) {
8872 case 0:
8873 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008874 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008875 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008876 default:
8877 err = nfs4_handle_exception(server, err, &exception);
8878 }
8879 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008880out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008881 return err;
8882}
8883
8884static int
8885nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8886 struct nfs_fsinfo *info)
8887{
8888 int err;
8889 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008890 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008891 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008892 struct nfs4_secinfo4 *secinfo;
8893 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008894
8895 page = alloc_page(GFP_KERNEL);
8896 if (!page) {
8897 err = -ENOMEM;
8898 goto out;
8899 }
8900
8901 flavors = page_address(page);
8902 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8903
8904 /*
8905 * Fall back on "guess and check" method if
8906 * the server doesn't support SECINFO_NO_NAME
8907 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008908 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008909 err = nfs4_find_root_sec(server, fhandle, info);
8910 goto out_freepage;
8911 }
8912 if (err)
8913 goto out_freepage;
8914
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008915 for (i = 0; i < flavors->num_flavors; i++) {
8916 secinfo = &flavors->flavors[i];
8917
8918 switch (secinfo->flavor) {
8919 case RPC_AUTH_NULL:
8920 case RPC_AUTH_UNIX:
8921 case RPC_AUTH_GSS:
8922 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8923 &secinfo->flavor_info);
8924 break;
8925 default:
8926 flavor = RPC_AUTH_MAXFLAVOR;
8927 break;
8928 }
8929
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008930 if (!nfs_auth_info_match(&server->auth_info, flavor))
8931 flavor = RPC_AUTH_MAXFLAVOR;
8932
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008933 if (flavor != RPC_AUTH_MAXFLAVOR) {
8934 err = nfs4_lookup_root_sec(server, fhandle,
8935 info, flavor);
8936 if (!err)
8937 break;
8938 }
8939 }
8940
8941 if (flavor == RPC_AUTH_MAXFLAVOR)
8942 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008943
8944out_freepage:
8945 put_page(page);
8946 if (err == -EACCES)
8947 return -EPERM;
8948out:
8949 return err;
8950}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008951
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008952static int _nfs41_test_stateid(struct nfs_server *server,
8953 nfs4_stateid *stateid,
8954 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008955{
8956 int status;
8957 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008958 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008959 };
8960 struct nfs41_test_stateid_res res;
8961 struct rpc_message msg = {
8962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8963 .rpc_argp = &args,
8964 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008965 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008966 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008967 struct rpc_clnt *rpc_client = server->client;
8968
8969 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8970 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008971
Chuck Lever38527b12012-07-11 16:30:23 -04008972 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008973 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008974 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008975 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008976 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008977 if (status != NFS_OK) {
8978 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008979 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008980 }
8981 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008982 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008983}
8984
Trond Myklebust43912bb2016-09-22 13:38:56 -04008985static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8986 int err, struct nfs4_exception *exception)
8987{
8988 exception->retry = 0;
8989 switch(err) {
8990 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008991 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008992 nfs4_handle_exception(server, err, exception);
8993 break;
8994 case -NFS4ERR_BADSESSION:
8995 case -NFS4ERR_BADSLOT:
8996 case -NFS4ERR_BAD_HIGH_SLOT:
8997 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8998 case -NFS4ERR_DEADSESSION:
8999 nfs4_do_handle_exception(server, err, exception);
9000 }
9001}
9002
Chuck Lever38527b12012-07-11 16:30:23 -04009003/**
9004 * nfs41_test_stateid - perform a TEST_STATEID operation
9005 *
9006 * @server: server / transport on which to perform the operation
9007 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009008 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009009 *
9010 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9011 * Otherwise a negative NFS4ERR value is returned if the operation
9012 * failed or the state ID is not currently valid.
9013 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009014static int nfs41_test_stateid(struct nfs_server *server,
9015 nfs4_stateid *stateid,
9016 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009017{
9018 struct nfs4_exception exception = { };
9019 int err;
9020 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009021 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009022 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009023 } while (exception.retry);
9024 return err;
9025}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009026
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009027struct nfs_free_stateid_data {
9028 struct nfs_server *server;
9029 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009030 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009031};
9032
9033static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9034{
9035 struct nfs_free_stateid_data *data = calldata;
9036 nfs41_setup_sequence(nfs4_get_session(data->server),
9037 &data->args.seq_args,
9038 &data->res.seq_res,
9039 task);
9040}
9041
9042static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9043{
9044 struct nfs_free_stateid_data *data = calldata;
9045
9046 nfs41_sequence_done(task, &data->res.seq_res);
9047
9048 switch (task->tk_status) {
9049 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009050 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009051 rpc_restart_call_prepare(task);
9052 }
9053}
9054
9055static void nfs41_free_stateid_release(void *calldata)
9056{
9057 kfree(calldata);
9058}
9059
Trond Myklebust17f26b12013-08-21 15:48:42 -04009060static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009061 .rpc_call_prepare = nfs41_free_stateid_prepare,
9062 .rpc_call_done = nfs41_free_stateid_done,
9063 .rpc_release = nfs41_free_stateid_release,
9064};
9065
9066static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009067 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009068 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009069 bool privileged)
9070{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009071 struct rpc_message msg = {
9072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009073 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009074 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009075 struct rpc_task_setup task_setup = {
9076 .rpc_client = server->client,
9077 .rpc_message = &msg,
9078 .callback_ops = &nfs41_free_stateid_ops,
9079 .flags = RPC_TASK_ASYNC,
9080 };
9081 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009082
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009083 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9084 &task_setup.rpc_client, &msg);
9085
Chuck Lever38527b12012-07-11 16:30:23 -04009086 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009087 data = kmalloc(sizeof(*data), GFP_NOFS);
9088 if (!data)
9089 return ERR_PTR(-ENOMEM);
9090 data->server = server;
9091 nfs4_stateid_copy(&data->args.stateid, stateid);
9092
9093 task_setup.callback_data = data;
9094
9095 msg.rpc_argp = &data->args;
9096 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009097 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009098 if (privileged)
9099 nfs4_set_sequence_privileged(&data->args.seq_args);
9100
9101 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009102}
9103
Chuck Lever38527b12012-07-11 16:30:23 -04009104/**
9105 * nfs41_free_stateid - perform a FREE_STATEID operation
9106 *
9107 * @server: server / transport on which to perform the operation
9108 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009109 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009110 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009111 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009112 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009113 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009114static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009115 const nfs4_stateid *stateid,
9116 struct rpc_cred *cred,
9117 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009118{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009119 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009120
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009121 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009122 if (IS_ERR(task))
9123 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009124 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009125 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009126}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009127
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009128static void
9129nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009130{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009131 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009132
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009133 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009134 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009135}
9136
Trond Myklebust36281ca2012-03-04 18:13:56 -05009137static bool nfs41_match_stateid(const nfs4_stateid *s1,
9138 const nfs4_stateid *s2)
9139{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009140 if (s1->type != s2->type)
9141 return false;
9142
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009143 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009144 return false;
9145
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009146 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009147 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009148 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009149 return true;
9150
9151 return false;
9152}
9153
Andy Adamson557134a2009-04-01 09:21:53 -04009154#endif /* CONFIG_NFS_V4_1 */
9155
Trond Myklebust36281ca2012-03-04 18:13:56 -05009156static bool nfs4_match_stateid(const nfs4_stateid *s1,
9157 const nfs4_stateid *s2)
9158{
Trond Myklebustf597c532012-03-04 18:13:56 -05009159 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009160}
9161
9162
Trond Myklebust17280172012-03-11 13:11:00 -04009163static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009164 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009165 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009166 .recover_open = nfs4_open_reclaim,
9167 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009168 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009169 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009170};
9171
Andy Adamson591d71c2009-04-01 09:22:47 -04009172#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009173static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009174 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9175 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9176 .recover_open = nfs4_open_reclaim,
9177 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009178 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009179 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009180 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009181};
9182#endif /* CONFIG_NFS_V4_1 */
9183
Trond Myklebust17280172012-03-11 13:11:00 -04009184static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009185 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009186 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009187 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009189 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190};
9191
Andy Adamson591d71c2009-04-01 09:22:47 -04009192#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009193static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009194 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9195 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009196 .recover_open = nfs41_open_expired,
9197 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009198 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009199};
9200#endif /* CONFIG_NFS_V4_1 */
9201
Trond Myklebust17280172012-03-11 13:11:00 -04009202static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009203 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009204 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009205 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009206};
9207
9208#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009209static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009210 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009211 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009212 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009213};
9214#endif
9215
Chuck Leverec011fe2013-10-17 14:12:39 -04009216static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009217 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009218 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009219};
9220
9221#if defined(CONFIG_NFS_V4_1)
9222static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009223 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009224 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009225};
9226#endif /* CONFIG_NFS_V4_1 */
9227
Trond Myklebust97dc1352010-06-16 09:52:26 -04009228static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9229 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009230 .init_caps = NFS_CAP_READDIRPLUS
9231 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009232 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009233 .init_client = nfs40_init_client,
9234 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009235 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009236 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009237 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009238 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009239 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009240 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009241 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9242 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9243 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009244 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009245};
9246
9247#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009248static struct nfs_seqid *
9249nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9250{
9251 return NULL;
9252}
9253
Trond Myklebust97dc1352010-06-16 09:52:26 -04009254static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9255 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009256 .init_caps = NFS_CAP_READDIRPLUS
9257 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009258 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009259 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009260 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009261 .init_client = nfs41_init_client,
9262 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009263 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009264 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009265 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009266 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009267 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009268 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009269 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009270 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9271 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9272 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009273 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009274};
9275#endif
9276
Steve Dickson42c2c422013-05-22 12:50:38 -04009277#if defined(CONFIG_NFS_V4_2)
9278static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9279 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009280 .init_caps = NFS_CAP_READDIRPLUS
9281 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009282 | NFS_CAP_POSIX_LOCK
9283 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009284 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009285 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009286 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009287 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009288 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009289 | NFS_CAP_LAYOUTSTATS
9290 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009291 .init_client = nfs41_init_client,
9292 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009293 .match_stateid = nfs41_match_stateid,
9294 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009295 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009296 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009297 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009298 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009299 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009300 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9301 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9302 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009303 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009304};
9305#endif
9306
Trond Myklebust97dc1352010-06-16 09:52:26 -04009307const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9308 [0] = &nfs_v4_0_minor_ops,
9309#if defined(CONFIG_NFS_V4_1)
9310 [1] = &nfs_v4_1_minor_ops,
9311#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009312#if defined(CONFIG_NFS_V4_2)
9313 [2] = &nfs_v4_2_minor_ops,
9314#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009315};
9316
Trond Myklebust13997822016-07-24 17:10:52 -04009317static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009318{
9319 ssize_t error, error2;
9320
9321 error = generic_listxattr(dentry, list, size);
9322 if (error < 0)
9323 return error;
9324 if (list) {
9325 list += error;
9326 size -= error;
9327 }
9328
9329 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9330 if (error2 < 0)
9331 return error2;
9332 return error + error2;
9333}
9334
Trond Myklebust17f26b12013-08-21 15:48:42 -04009335static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009336 .create = nfs_create,
9337 .lookup = nfs_lookup,
9338 .atomic_open = nfs_atomic_open,
9339 .link = nfs_link,
9340 .unlink = nfs_unlink,
9341 .symlink = nfs_symlink,
9342 .mkdir = nfs_mkdir,
9343 .rmdir = nfs_rmdir,
9344 .mknod = nfs_mknod,
9345 .rename = nfs_rename,
9346 .permission = nfs_permission,
9347 .getattr = nfs_getattr,
9348 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009349 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009350};
9351
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009352static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009353 .permission = nfs_permission,
9354 .getattr = nfs_getattr,
9355 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009356 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009357};
9358
David Howells509de812006-08-22 20:06:11 -04009359const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 .version = 4, /* protocol version */
9361 .dentry_ops = &nfs4_dentry_operations,
9362 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009363 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009364 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009366 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009367 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368 .getattr = nfs4_proc_getattr,
9369 .setattr = nfs4_proc_setattr,
9370 .lookup = nfs4_proc_lookup,
9371 .access = nfs4_proc_access,
9372 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373 .create = nfs4_proc_create,
9374 .remove = nfs4_proc_remove,
9375 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009376 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009378 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009379 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009380 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381 .link = nfs4_proc_link,
9382 .symlink = nfs4_proc_symlink,
9383 .mkdir = nfs4_proc_mkdir,
9384 .rmdir = nfs4_proc_remove,
9385 .readdir = nfs4_proc_readdir,
9386 .mknod = nfs4_proc_mknod,
9387 .statfs = nfs4_proc_statfs,
9388 .fsinfo = nfs4_proc_fsinfo,
9389 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009390 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009392 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009394 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009396 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009397 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009398 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009399 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009401 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009402 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009403 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009404 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009405 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009406 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009407 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009408 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009409 .create_server = nfs4_create_server,
9410 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411};
9412
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009413static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009414 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009415 .list = nfs4_xattr_list_nfs4_acl,
9416 .get = nfs4_xattr_get_nfs4_acl,
9417 .set = nfs4_xattr_set_nfs4_acl,
9418};
9419
9420const struct xattr_handler *nfs4_xattr_handlers[] = {
9421 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009422#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9423 &nfs4_xattr_nfs4_label_handler,
9424#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009425 NULL
9426};
9427
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428/*
9429 * Local variables:
9430 * c-basic-offset: 8
9431 * End:
9432 */