blob: 9759d6f9e1a510d4d31535e598387a7e4be4f16f [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,
NeilBrown29b59f92016-10-13 15:26:47 +110096 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040097 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
Andy Adamsone23008e2012-10-02 21:07:32 -0400228 | FATTR4_WORD0_FILEID,
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
238};
239
David Quigleya09df2c2013-05-22 12:50:41 -0400240const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 FATTR4_WORD0_MAXLINK
242 | FATTR4_WORD0_MAXNAME,
243 0
244};
245
Fred Isamandae100c2011-07-30 20:52:37 -0400246const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700250 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400251 | FATTR4_WORD1_FS_LAYOUT_TYPES,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800253 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
David Quigleya09df2c2013-05-22 12:50:41 -0400256const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400257 FATTR4_WORD0_TYPE
258 | FATTR4_WORD0_CHANGE
259 | FATTR4_WORD0_SIZE
260 | FATTR4_WORD0_FSID
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS,
263 FATTR4_WORD1_MODE
264 | FATTR4_WORD1_NUMLINKS
265 | FATTR4_WORD1_OWNER
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400272 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400273};
274
Al Virobc4785c2006-10-19 23:28:51 -0700275static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct nfs4_readdir_arg *readdir)
277{
Al Viro0dbb4c62006-10-19 23:28:49 -0700278 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000281 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
283 return;
284 }
285
286 readdir->cookie = 0;
287 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
288 if (cookie == 2)
289 return;
290
291 /*
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
296 * instead of 1 or 2.
297 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800298 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (cookie == 0) {
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_one; /* cookie, second word */
304 *p++ = xdr_one; /* entry len */
305 memcpy(p, ".\0\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000310 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
313 *p++ = xdr_one; /* next */
314 *p++ = xdr_zero; /* cookie, first word */
315 *p++ = xdr_two; /* cookie, second word */
316 *p++ = xdr_two; /* entry len */
317 memcpy(p, "..\0\0", 4); /* entry */
318 p++;
319 *p++ = xdr_one; /* bitmap length */
320 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
321 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000322 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 readdir->pgbase = (char *)p - (char *)start;
325 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800326 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Trond Myklebust26d36302016-09-22 13:39:05 -0400329static void nfs4_test_and_free_stateid(struct nfs_server *server,
330 nfs4_stateid *stateid,
331 struct rpc_cred *cred)
332{
333 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
334
335 ops->test_and_free_expired(server, stateid, cred);
336}
337
338static void __nfs4_free_revoked_stateid(struct nfs_server *server,
339 nfs4_stateid *stateid,
340 struct rpc_cred *cred)
341{
342 stateid->type = NFS4_REVOKED_STATEID_TYPE;
343 nfs4_test_and_free_stateid(server, stateid, cred);
344}
345
346static void nfs4_free_revoked_stateid(struct nfs_server *server,
347 const nfs4_stateid *stateid,
348 struct rpc_cred *cred)
349{
350 nfs4_stateid tmp;
351
352 nfs4_stateid_copy(&tmp, stateid);
353 __nfs4_free_revoked_stateid(server, &tmp, cred);
354}
355
NeilBrown8478eaa2014-09-18 16:09:27 +1000356static long nfs4_update_delay(long *timeout)
357{
358 long ret;
359 if (!timeout)
360 return NFS4_POLL_RETRY_MAX;
361 if (*timeout <= 0)
362 *timeout = NFS4_POLL_RETRY_MIN;
363 if (*timeout > NFS4_POLL_RETRY_MAX)
364 *timeout = NFS4_POLL_RETRY_MAX;
365 ret = *timeout;
366 *timeout <<= 1;
367 return ret;
368}
369
Trond Myklebust65de8722008-12-23 15:21:44 -0500370static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
371{
372 int res = 0;
373
374 might_sleep();
375
NeilBrown8478eaa2014-09-18 16:09:27 +1000376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500378 if (fatal_signal_pending(current))
379 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 return res;
381}
382
383/* This is the error handling routine for processes that are allowed
384 * to sleep.
385 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400386static int nfs4_do_handle_exception(struct nfs_server *server,
387 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500388{
389 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500390 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400391 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500392 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500393 int ret = errorcode;
394
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400395 exception->delay = 0;
396 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400398
399 if (stateid == NULL && state != NULL)
400 stateid = &state->stateid;
401
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 switch(errorcode) {
403 case 0:
404 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400405 case -NFS4ERR_DELEG_REVOKED:
406 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400407 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400408 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400409 if (inode != NULL && stateid != NULL) {
410 nfs_inode_find_state_and_recover(inode,
411 stateid);
412 goto wait_on_recovery;
413 }
414 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400415 if (inode) {
416 int err;
417
418 err = nfs_async_inode_return_delegation(inode,
419 stateid);
420 if (err == 0)
421 goto wait_on_recovery;
422 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
423 exception->retry = 1;
424 break;
425 }
426 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500427 if (state == NULL)
428 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400429 ret = nfs4_schedule_stateid_recovery(server, state);
430 if (ret < 0)
431 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500433 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500434 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500435 nfs4_schedule_lease_recovery(clp);
436 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400437 case -NFS4ERR_MOVED:
438 ret = nfs4_schedule_migration_recovery(server);
439 if (ret < 0)
440 break;
441 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400442 case -NFS4ERR_LEASE_MOVED:
443 nfs4_schedule_lease_moved_recovery(clp);
444 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500445#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400446 case -NFS4ERR_BADSESSION:
447 case -NFS4ERR_BADSLOT:
448 case -NFS4ERR_BAD_HIGH_SLOT:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
450 case -NFS4ERR_DEADSESSION:
451 case -NFS4ERR_SEQ_FALSE_RETRY:
452 case -NFS4ERR_SEQ_MISORDERED:
453 dprintk("%s ERROR: %d Reset session\n", __func__,
454 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400455 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400456 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500457#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500458 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500459 if (exception->timeout > HZ) {
460 /* We have retried a decent amount, time to
461 * fail
462 */
463 ret = -EBUSY;
464 break;
465 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400467 nfs_inc_server_stats(server, NFSIOS_DELAY);
468 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400469 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400470 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400471 exception->delay = 1;
472 return 0;
473
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400474 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 case -NFS4ERR_OLD_STATEID:
476 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800477 break;
478 case -NFS4ERR_BADOWNER:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME:
481 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
482 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
483 exception->retry = 1;
484 printk(KERN_WARNING "NFS: v4 server %s "
485 "does not accept raw "
486 "uid/gids. "
487 "Reenabling the idmapper.\n",
488 server->nfs_client->cl_hostname);
489 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500490 }
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500493wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400494 exception->recovering = 1;
495 return 0;
496}
497
498/* This is the error handling routine for processes that are allowed
499 * to sleep.
500 */
501int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
502{
503 struct nfs_client *clp = server->nfs_client;
504 int ret;
505
506 ret = nfs4_do_handle_exception(server, errorcode, exception);
507 if (exception->delay) {
508 ret = nfs4_delay(server->client, &exception->timeout);
509 goto out_retry;
510 }
511 if (exception->recovering) {
512 ret = nfs4_wait_clnt_recover(clp);
513 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
514 return -EIO;
515 goto out_retry;
516 }
517 return ret;
518out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500519 if (ret == 0)
520 exception->retry = 1;
521 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500522}
523
Trond Myklebust037fc982015-09-20 15:51:00 -0400524static int
525nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
526 int errorcode, struct nfs4_exception *exception)
527{
528 struct nfs_client *clp = server->nfs_client;
529 int ret;
530
531 ret = nfs4_do_handle_exception(server, errorcode, exception);
532 if (exception->delay) {
533 rpc_delay(task, nfs4_update_delay(&exception->timeout));
534 goto out_retry;
535 }
536 if (exception->recovering) {
537 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
539 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
540 goto out_retry;
541 }
542 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
543 ret = -EIO;
544 return ret;
545out_retry:
546 if (ret == 0)
547 exception->retry = 1;
548 return ret;
549}
550
551static int
552nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
553 struct nfs4_state *state, long *timeout)
554{
555 struct nfs4_exception exception = {
556 .state = state,
557 };
558
559 if (task->tk_status >= 0)
560 return 0;
561 if (timeout)
562 exception.timeout = *timeout;
563 task->tk_status = nfs4_async_handle_exception(task, server,
564 task->tk_status,
565 &exception);
566 if (exception.delay && timeout)
567 *timeout = exception.timeout;
568 if (exception.retry)
569 return -EAGAIN;
570 return 0;
571}
572
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400573/*
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
576 */
577static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
578{
579 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
580
581 if (flavor == RPC_AUTH_GSS_KRB5I ||
582 flavor == RPC_AUTH_GSS_KRB5P)
583 return true;
584
585 return false;
586}
Trond Myklebust65de8722008-12-23 15:21:44 -0500587
Trond Myklebust452e9352010-07-31 14:29:06 -0400588static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
594}
595
Trond Myklebust452e9352010-07-31 14:29:06 -0400596static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597{
Trond Myklebustbe824162015-07-05 14:50:46 -0400598 struct nfs_client *clp = server->nfs_client;
599
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400602}
603
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400604struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
608};
609
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800610void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400612{
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
616
617 res->sr_slot = NULL;
618}
619
620static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621{
622 args->sa_privileged = 1;
623}
624
Anna Schumaker6de7e122017-01-09 16:51:52 -0500625static int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
626 struct nfs4_sequence_args *args,
627 struct nfs4_sequence_res *res,
628 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400629{
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 struct nfs4_slot *slot;
631
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500633 return -EAGAIN;
Chuck Lever3bd23842013-08-09 12:49:19 -0400634
635 slot = nfs4_alloc_slot(tbl);
636 if (IS_ERR(slot)) {
637 if (slot == ERR_PTR(-ENOMEM))
638 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500639 return -EAGAIN;
Chuck Lever3bd23842013-08-09 12:49:19 -0400640 }
Chuck Lever3bd23842013-08-09 12:49:19 -0400641
Trond Myklebust0a014a42016-09-22 13:38:51 -0400642 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400643 args->sa_slot = slot;
644 res->sr_slot = slot;
Chuck Lever3bd23842013-08-09 12:49:19 -0400645 return 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400646}
647
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400648static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400649{
650 struct nfs4_slot *slot = res->sr_slot;
651 struct nfs4_slot_table *tbl;
652
Chuck Lever3bd23842013-08-09 12:49:19 -0400653 tbl = slot->table;
654 spin_lock(&tbl->slot_tbl_lock);
655 if (!nfs41_wake_and_assign_slot(tbl, slot))
656 nfs4_free_slot(tbl, slot);
657 spin_unlock(&tbl->slot_tbl_lock);
658
659 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400660}
661
662static int nfs40_sequence_done(struct rpc_task *task,
663 struct nfs4_sequence_res *res)
664{
665 if (res->sr_slot != NULL)
666 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400667 return 1;
668}
669
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400670#if defined(CONFIG_NFS_V4_1)
671
Trond Myklebustd185a332010-06-16 09:52:25 -0400672static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400673{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500674 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400675 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500676 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400678
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500679 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500680 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500681
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400682 /* Bump the slot sequence number */
683 if (slot->seq_done)
684 slot->seq_nr++;
685 slot->seq_done = 0;
686
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500687 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500688 /* Be nice to the server: try to ensure that the last transmitted
689 * value for highest_user_slotid <= target_highest_slotid
690 */
691 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
692 send_new_highest_used_slotid = true;
693
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500694 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500695 send_new_highest_used_slotid = false;
696 goto out_unlock;
697 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500698 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500699
700 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
701 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500702out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500703 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400704 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500705 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400706 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400707 if (waitqueue_active(&tbl->slot_waitq))
708 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400709}
710
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400711static int nfs41_sequence_process(struct rpc_task *task,
712 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400713{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500714 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400716 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500717 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500718 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400719
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500720 if (slot == NULL)
721 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400722 /* don't increment the sequence number if the task wasn't sent */
723 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400724 goto out;
725
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500726 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500727
Trond Myklebustac20d162012-12-15 15:36:07 -0500728 if (slot->interrupted) {
729 slot->interrupted = 0;
730 interrupted = true;
731 }
732
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400733 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500734 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 switch (res->sr_status) {
736 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400737 /* If previous op on slot was interrupted and we reused
738 * the seq# and got a reply from the cache, then retry
739 */
740 if (task->tk_status == -EREMOTEIO && interrupted) {
741 ++slot->seq_nr;
742 goto retry_nowait;
743 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400744 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400745 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500746 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500747 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500748 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400749 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
750 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500751 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400752 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500753 case 1:
754 /*
755 * sr_status remains 1 if an RPC level error occurred.
756 * The server may or may not have processed the sequence
757 * operation..
758 * Mark the slot as having hosted an interrupted RPC call.
759 */
760 slot->interrupted = 1;
761 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400762 case -NFS4ERR_DELAY:
763 /* The server detected a resend of the RPC call and
764 * returned NFS4ERR_DELAY as per Section 2.10.6.2
765 * of RFC5661.
766 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500767 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400768 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500769 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500770 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400771 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500772 case -NFS4ERR_BADSLOT:
773 /*
774 * The slot id we used was probably retired. Try again
775 * using a different slot id.
776 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500777 goto retry_nowait;
778 case -NFS4ERR_SEQ_MISORDERED:
779 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500780 * Was the last operation on this sequence interrupted?
781 * If so, retry after bumping the sequence number.
782 */
783 if (interrupted) {
784 ++slot->seq_nr;
785 goto retry_nowait;
786 }
787 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500788 * Could this slot have been previously retired?
789 * If so, then the server may be expecting seq_nr = 1!
790 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500791 if (slot->seq_nr != 1) {
792 slot->seq_nr = 1;
793 goto retry_nowait;
794 }
795 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500796 case -NFS4ERR_SEQ_FALSE_RETRY:
797 ++slot->seq_nr;
798 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500799 case -NFS4ERR_DEADSESSION:
800 case -NFS4ERR_BADSESSION:
801 nfs4_schedule_session_recovery(session, res->sr_status);
802 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400803 default:
804 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400805 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400806 }
807out:
808 /* The session may be reset by one of the error handlers. */
809 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500810out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500811 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500812retry_nowait:
813 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400814 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500815 task->tk_status = 0;
816 ret = 0;
817 }
818 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400819out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400820 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400821 goto out;
822 rpc_delay(task, NFS4_POLL_RETRY_MAX);
823 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400824}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400825
826int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
827{
828 if (!nfs41_sequence_process(task, res))
829 return 0;
830 if (res->sr_slot != NULL)
831 nfs41_sequence_free_slot(res);
832 return 1;
833
834}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500835EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400836
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400837static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
838{
839 if (res->sr_slot == NULL)
840 return 1;
841 if (res->sr_slot->table->session != NULL)
842 return nfs41_sequence_process(task, res);
843 return nfs40_sequence_done(task, res);
844}
845
846static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
847{
848 if (res->sr_slot != NULL) {
849 if (res->sr_slot->table->session != NULL)
850 nfs41_sequence_free_slot(res);
851 else
852 nfs40_sequence_free_slot(res);
853 }
854}
855
Peng Tao2c4b1312014-06-11 05:24:16 +0800856int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400857{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500858 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400859 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400860 if (!res->sr_slot->table->session)
861 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400862 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400863}
Peng Tao2c4b1312014-06-11 05:24:16 +0800864EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400865
Anna Schumaker6de7e122017-01-09 16:51:52 -0500866static int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400867 struct nfs4_sequence_args *args,
868 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400869 struct rpc_task *task)
870{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400871 struct nfs4_slot *slot;
872 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400873
874 dprintk("--> %s\n", __func__);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400875 tbl = &session->fc_slot_table;
876
Trond Myklebust69d206b2012-11-22 13:21:02 -0500877 task->tk_timeout = 0;
878
Andy Adamson774d5f12013-05-20 14:13:50 -0400879 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400880 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500881 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500882 dprintk("%s session is draining\n", __func__);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500883 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400884 }
885
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500886 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500887 if (IS_ERR(slot)) {
888 /* If out of memory, try again in 1/4 second */
889 if (slot == ERR_PTR(-ENOMEM))
890 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 dprintk("<-- %s: no free slots\n", __func__);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500892 return -EAGAIN;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893 }
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400894
Trond Myklebust0a014a42016-09-22 13:38:51 -0400895 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500896 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400897
Chuck Levere8d92382013-08-09 12:47:51 -0400898 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500899 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400900
Benny Halevydfb4f3092010-09-24 09:17:01 -0400901 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500902 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400903 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400904 /*
905 * sr_status is only set in decode_sequence, and so will remain
906 * set to 1 if an rpc level failure occurs.
907 */
908 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400909 trace_nfs4_setup_sequence(session, args);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400910 return 0;
911}
912
Andy Adamsonce5039c2009-04-01 09:22:13 -0400913static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
914{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400915 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400916
Trond Myklebust035168a2010-06-16 09:52:26 -0400917 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
918
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500919 nfs4_setup_sequence(data->seq_server->nfs_client,
920 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400921}
922
Andy Adamson69ab40c2009-04-01 09:22:19 -0400923static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
924{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400925 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400926
Trond Myklebust14516c32010-07-31 14:29:06 -0400927 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400928}
929
Trond Myklebust17280172012-03-11 13:11:00 -0400930static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400931 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400932 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400933};
934
Chuck Lever3bd23842013-08-09 12:49:19 -0400935#else /* !CONFIG_NFS_V4_1 */
936
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400937static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
938{
939 return nfs40_sequence_done(task, res);
940}
941
942static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
943{
944 if (res->sr_slot != NULL)
945 nfs40_sequence_free_slot(res);
946}
947
Peng Tao2c4b1312014-06-11 05:24:16 +0800948int nfs4_sequence_done(struct rpc_task *task,
949 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400950{
Chuck Lever3bd23842013-08-09 12:49:19 -0400951 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400952}
Peng Tao2c4b1312014-06-11 05:24:16 +0800953EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400954
955#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400956
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500957int nfs4_setup_sequence(const struct nfs_client *client,
958 struct nfs4_sequence_args *args,
959 struct nfs4_sequence_res *res,
960 struct rpc_task *task)
961{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500962 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500963 struct nfs4_slot_table *tbl = session ? &session->fc_slot_table :
964 client->cl_slot_tbl;
965 int ret;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500966
Anna Schumaker9dd91072017-01-10 12:01:46 -0500967 /* slot already allocated? */
968 if (res->sr_slot != NULL)
969 goto out_start;
970
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500971 spin_lock(&tbl->slot_tbl_lock);
972
Anna Schumaker9dd91072017-01-10 12:01:46 -0500973#if defined(CONFIG_NFS_V4_1)
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500974 if (session)
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500975 ret = nfs41_setup_sequence(session, args, res, task);
976 else
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500977#endif /* CONFIG_NFS_V4_1 */
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500978 ret = nfs40_setup_sequence(client->cl_slot_tbl, args, res, task);
979
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500980 if (ret == -EAGAIN)
981 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500982 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500983
Anna Schumaker9dd91072017-01-10 12:01:46 -0500984out_start:
985 rpc_call_start(task);
986 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500987
988out_sleep:
989 if (args->sa_privileged)
990 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
991 NULL, RPC_PRIORITY_PRIVILEGED);
992 else
993 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
994 spin_unlock(&tbl->slot_tbl_lock);
995 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500996}
997EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
998
Chuck Lever9915ea72013-08-09 12:48:27 -0400999static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1000{
1001 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001002 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001003 data->seq_args, data->seq_res, task);
1004}
1005
1006static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1007{
1008 struct nfs4_call_sync_data *data = calldata;
1009 nfs4_sequence_done(task, data->seq_res);
1010}
1011
1012static const struct rpc_call_ops nfs40_call_sync_ops = {
1013 .rpc_call_prepare = nfs40_call_sync_prepare,
1014 .rpc_call_done = nfs40_call_sync_done,
1015};
1016
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001017static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001018 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001020 struct nfs4_sequence_args *args,
1021 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001022{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001023 int ret;
1024 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001025 struct nfs_client *clp = server->nfs_client;
1026 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001027 .seq_server = server,
1028 .seq_args = args,
1029 .seq_res = res,
1030 };
1031 struct rpc_task_setup task_setup = {
1032 .rpc_client = clnt,
1033 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001034 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001035 .callback_data = &data
1036 };
1037
1038 task = rpc_run_task(&task_setup);
1039 if (IS_ERR(task))
1040 ret = PTR_ERR(task);
1041 else {
1042 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001043 rpc_put_task(task);
1044 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001045 return ret;
1046}
1047
Bryan Schumaker7c513052011-03-24 17:12:24 +00001048int nfs4_call_sync(struct rpc_clnt *clnt,
1049 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001050 struct rpc_message *msg,
1051 struct nfs4_sequence_args *args,
1052 struct nfs4_sequence_res *res,
1053 int cache_reply)
1054{
Chuck Levera9c92d62013-08-09 12:48:18 -04001055 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001056 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001059static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1060 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 struct nfs_inode *nfsi = NFS_I(dir);
1063
1064 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001065 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001066 if (cinfo->atomic && cinfo->before == dir->i_version) {
1067 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1068 nfsi->attrtimeo_timestamp = jiffies;
1069 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001071 if (cinfo->before != dir->i_version)
1072 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1073 NFS_INO_INVALID_ACL;
1074 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001075 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001076 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001077 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001078 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 spin_unlock(&dir->i_lock);
1080}
1081
1082struct nfs4_opendata {
1083 struct kref kref;
1084 struct nfs_openargs o_arg;
1085 struct nfs_openres o_res;
1086 struct nfs_open_confirmargs c_arg;
1087 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001088 struct nfs4_string owner_name;
1089 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001090 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001092 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001094 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct nfs4_state_owner *owner;
1096 struct nfs4_state *state;
1097 struct iattr attrs;
1098 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001099 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001100 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001101 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001102 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int cancelled;
1104};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001105
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001106static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1107 int err, struct nfs4_exception *exception)
1108{
1109 if (err != -EINVAL)
1110 return false;
1111 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1112 return false;
1113 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1114 exception->retry = 1;
1115 return true;
1116}
1117
Trond Myklebust6ae37332015-01-30 14:21:14 -05001118static u32
1119nfs4_map_atomic_open_share(struct nfs_server *server,
1120 fmode_t fmode, int openflags)
1121{
1122 u32 res = 0;
1123
1124 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1125 case FMODE_READ:
1126 res = NFS4_SHARE_ACCESS_READ;
1127 break;
1128 case FMODE_WRITE:
1129 res = NFS4_SHARE_ACCESS_WRITE;
1130 break;
1131 case FMODE_READ|FMODE_WRITE:
1132 res = NFS4_SHARE_ACCESS_BOTH;
1133 }
1134 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1135 goto out;
1136 /* Want no delegation if we're using O_DIRECT */
1137 if (openflags & O_DIRECT)
1138 res |= NFS4_SHARE_WANT_NO_DELEG;
1139out:
1140 return res;
1141}
1142
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001143static enum open_claim_type4
1144nfs4_map_atomic_open_claim(struct nfs_server *server,
1145 enum open_claim_type4 claim)
1146{
1147 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1148 return claim;
1149 switch (claim) {
1150 default:
1151 return claim;
1152 case NFS4_OPEN_CLAIM_FH:
1153 return NFS4_OPEN_CLAIM_NULL;
1154 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1155 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1156 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1157 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1158 }
1159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162{
1163 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001164 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165 p->o_res.seqid = p->o_arg.seqid;
1166 p->c_res.seqid = p->c_arg.seqid;
1167 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001168 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001170 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001171}
1172
Al Viro82a2c1b2011-06-22 18:30:55 -04001173static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001175 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001176 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001177 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001178 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179{
Al Viro82a2c1b2011-06-22 18:30:55 -04001180 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001181 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001183 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001184 struct nfs4_opendata *p;
1185
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001186 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187 if (p == NULL)
1188 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001189
1190 p->f_label = nfs4_label_alloc(server, gfp_mask);
1191 if (IS_ERR(p->f_label))
1192 goto err_free_p;
1193
Kinglong Meea49c2692015-07-27 15:31:38 +08001194 p->a_label = nfs4_label_alloc(server, gfp_mask);
1195 if (IS_ERR(p->a_label))
1196 goto err_free_f;
1197
Trond Myklebust63f5f792015-01-23 19:19:25 -05001198 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1199 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001200 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001201 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001202 nfs_sb_active(dentry->d_sb);
1203 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001204 p->dir = parent;
1205 p->owner = sp;
1206 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001207 p->o_arg.open_flags = flags;
1208 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001209 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001210 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001211 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1212 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001213 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1214 * will return permission denied for all bits until close */
1215 if (!(flags & O_EXCL)) {
1216 /* ask server to check for all possible rights as results
1217 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001218 switch (p->o_arg.claim) {
1219 default:
1220 break;
1221 case NFS4_OPEN_CLAIM_NULL:
1222 case NFS4_OPEN_CLAIM_FH:
1223 p->o_arg.access = NFS4_ACCESS_READ |
1224 NFS4_ACCESS_MODIFY |
1225 NFS4_ACCESS_EXTEND |
1226 NFS4_ACCESS_EXECUTE;
1227 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001228 }
David Howells7539bba2006-08-22 20:06:09 -04001229 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001230 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1231 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001232 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001233 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001234 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001235 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001236 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001237 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001238 case NFS4_OPEN_CLAIM_NULL:
1239 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1240 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1241 p->o_arg.fh = NFS_FH(dir);
1242 break;
1243 case NFS4_OPEN_CLAIM_PREVIOUS:
1244 case NFS4_OPEN_CLAIM_FH:
1245 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1246 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001247 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001248 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001249 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001250 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001251
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001252 p->o_arg.u.attrs = &p->attrs;
1253 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001254
1255 verf[0] = jiffies;
1256 verf[1] = current->pid;
1257 memcpy(p->o_arg.u.verifier.data, verf,
1258 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001260 p->c_arg.fh = &p->o_res.fh;
1261 p->c_arg.stateid = &p->o_res.stateid;
1262 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001263 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001264 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001266
1267err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001268 nfs4_label_free(p->a_label);
1269err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001270 nfs4_label_free(p->f_label);
1271err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001272 kfree(p);
1273err:
1274 dput(parent);
1275 return NULL;
1276}
1277
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001279{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 struct nfs4_opendata *p = container_of(kref,
1281 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001282 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001283
1284 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001285 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001286 if (p->state != NULL)
1287 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001288 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001289
Kinglong Meea49c2692015-07-27 15:31:38 +08001290 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001291 nfs4_label_free(p->f_label);
1292
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001294 dput(p->dentry);
1295 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001296 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001297 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001298 kfree(p);
1299}
1300
1301static void nfs4_opendata_put(struct nfs4_opendata *p)
1302{
1303 if (p != NULL)
1304 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305}
1306
Trond Myklebust06f814a2006-01-03 09:55:07 +01001307static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1308{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001309 int ret;
1310
Trond Myklebust06f814a2006-01-03 09:55:07 +01001311 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001312 return ret;
1313}
1314
Trond Myklebust24311f82015-09-20 10:50:17 -04001315static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1316 fmode_t fmode)
1317{
1318 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1319 case FMODE_READ|FMODE_WRITE:
1320 return state->n_rdwr != 0;
1321 case FMODE_WRITE:
1322 return state->n_wronly != 0;
1323 case FMODE_READ:
1324 return state->n_rdonly != 0;
1325 }
1326 WARN_ON_ONCE(1);
1327 return false;
1328}
1329
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001330static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001331{
1332 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001333
Trond Myklebust536e43d2012-01-17 22:04:26 -05001334 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 goto out;
1336 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001337 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001338 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1339 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001340 break;
1341 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001342 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1343 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001344 break;
1345 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001346 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1347 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001348 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001349out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001350 return ret;
1351}
1352
Trond Myklebust2a606182015-08-19 22:30:00 -05001353static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1354 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001355{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001356 if (delegation == NULL)
1357 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001359 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001360 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1361 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001362 switch (claim) {
1363 case NFS4_OPEN_CLAIM_NULL:
1364 case NFS4_OPEN_CLAIM_FH:
1365 break;
1366 case NFS4_OPEN_CLAIM_PREVIOUS:
1367 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1368 break;
1369 default:
1370 return 0;
1371 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001372 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001373 return 1;
1374}
1375
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001376static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001377{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001378 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001379 case FMODE_WRITE:
1380 state->n_wronly++;
1381 break;
1382 case FMODE_READ:
1383 state->n_rdonly++;
1384 break;
1385 case FMODE_READ|FMODE_WRITE:
1386 state->n_rdwr++;
1387 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001388 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001389}
1390
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001391#ifdef CONFIG_NFS_V4_1
1392static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1393{
1394 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1395 return true;
1396 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1397 return true;
1398 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1399 return true;
1400 return false;
1401}
1402#endif /* CONFIG_NFS_V4_1 */
1403
Trond Myklebust4f14c192014-02-12 19:15:06 -05001404static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001405{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001406 struct nfs_client *clp = state->owner->so_server->nfs_client;
1407 bool need_recover = false;
1408
1409 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1410 need_recover = true;
1411 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1412 need_recover = true;
1413 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1414 need_recover = true;
1415 if (need_recover)
1416 nfs4_state_mark_reclaim_nograce(clp, state);
1417}
1418
Trond Myklebuste999e802014-02-10 18:20:47 -05001419static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001420 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001421{
1422 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1423 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001424 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001425 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001426 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001427 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001428 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001429 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1430 return true;
1431 return false;
1432}
1433
Trond Myklebustf95549c2015-01-23 18:06:09 -05001434static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1435{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001436 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1437 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001438 if (state->n_wronly)
1439 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1440 if (state->n_rdonly)
1441 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1442 if (state->n_rdwr)
1443 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001444 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001445}
1446
Trond Myklebust226056c2014-02-11 10:41:07 -05001447static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1448 nfs4_stateid *stateid, fmode_t fmode)
1449{
1450 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1451 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1452 case FMODE_WRITE:
1453 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1454 break;
1455 case FMODE_READ:
1456 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1457 break;
1458 case 0:
1459 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1461 clear_bit(NFS_OPEN_STATE, &state->flags);
1462 }
1463 if (stateid == NULL)
1464 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001465 /* Handle OPEN+OPEN_DOWNGRADE races */
1466 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1467 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001468 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001469 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001470 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001471 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001472 nfs4_stateid_copy(&state->stateid, stateid);
1473 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001474}
1475
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001476static void nfs_clear_open_stateid(struct nfs4_state *state,
1477 nfs4_stateid *arg_stateid,
1478 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001479{
1480 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001481 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1482 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1483 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001484 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001485 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1486 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001487}
1488
Trond Myklebust1393d962016-09-22 13:39:13 -04001489static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1490 const nfs4_stateid *stateid, fmode_t fmode,
1491 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001492{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001494 case FMODE_READ:
1495 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1496 break;
1497 case FMODE_WRITE:
1498 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1499 break;
1500 case FMODE_READ|FMODE_WRITE:
1501 set_bit(NFS_O_RDWR_STATE, &state->flags);
1502 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001503 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001504 return;
1505 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1506 nfs4_stateid_copy(&state->stateid, stateid);
1507 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001508}
1509
Trond Myklebust1393d962016-09-22 13:39:13 -04001510static void __update_open_stateid(struct nfs4_state *state,
1511 const nfs4_stateid *open_stateid,
1512 const nfs4_stateid *deleg_stateid,
1513 fmode_t fmode,
1514 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001516 /*
1517 * Protect the call to nfs4_state_set_mode_locked and
1518 * serialise the stateid update
1519 */
Andrew Elble361cad32015-12-02 09:20:57 -05001520 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001521 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001522 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001523 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001524 set_bit(NFS_DELEGATED_STATE, &state->flags);
1525 }
1526 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001527 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001528 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001529 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001530 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Trond Myklebust1393d962016-09-22 13:39:13 -04001533static int update_open_stateid(struct nfs4_state *state,
1534 const nfs4_stateid *open_stateid,
1535 const nfs4_stateid *delegation,
1536 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001537{
Trond Myklebust1393d962016-09-22 13:39:13 -04001538 struct nfs_server *server = NFS_SERVER(state->inode);
1539 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001540 struct nfs_inode *nfsi = NFS_I(state->inode);
1541 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001542 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001543 int ret = 0;
1544
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001546
1547 rcu_read_lock();
1548 deleg_cur = rcu_dereference(nfsi->delegation);
1549 if (deleg_cur == NULL)
1550 goto no_delegation;
1551
1552 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001553 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001554 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001555 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001556 goto no_delegation_unlock;
1557
1558 if (delegation == NULL)
1559 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001560 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001561 goto no_delegation_unlock;
1562
Trond Myklebustb7391f42008-12-23 15:21:52 -05001563 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001564 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1565 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001566 ret = 1;
1567no_delegation_unlock:
1568 spin_unlock(&deleg_cur->lock);
1569no_delegation:
1570 rcu_read_unlock();
1571
1572 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001573 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001574 ret = 1;
1575 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001576 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001577 nfs4_schedule_state_manager(clp);
1578 if (freeme.type != 0)
1579 nfs4_test_and_free_stateid(server, &freeme,
1580 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001581
1582 return ret;
1583}
1584
Trond Myklebust39071e62015-01-24 15:07:56 -05001585static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1586 const nfs4_stateid *stateid)
1587{
1588 struct nfs4_state *state = lsp->ls_state;
1589 bool ret = false;
1590
1591 spin_lock(&state->state_lock);
1592 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1593 goto out_noupdate;
1594 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1595 goto out_noupdate;
1596 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1597 ret = true;
1598out_noupdate:
1599 spin_unlock(&state->state_lock);
1600 return ret;
1601}
Trond Myklebust34310432008-12-23 15:21:38 -05001602
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001603static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001604{
1605 struct nfs_delegation *delegation;
1606
1607 rcu_read_lock();
1608 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001609 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001610 rcu_read_unlock();
1611 return;
1612 }
1613 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001614 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001615}
1616
Trond Myklebust6ee41262007-07-08 14:11:36 -04001617static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001618{
1619 struct nfs4_state *state = opendata->state;
1620 struct nfs_inode *nfsi = NFS_I(state->inode);
1621 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001622 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001624 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001625 nfs4_stateid stateid;
1626 int ret = -EAGAIN;
1627
Trond Myklebustaac00a82007-07-05 19:02:21 -04001628 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001629 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001630 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001631 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001632 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001633 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001634 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001635 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001636 rcu_read_lock();
1637 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001638 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001639 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001640 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001641 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001643 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001644 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001645 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001646 if (!opendata->is_recover) {
1647 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1648 if (ret != 0)
1649 goto out;
1650 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001652
1653 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001654 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001655 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001656 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001657out:
1658 return ERR_PTR(ret);
1659out_return_state:
1660 atomic_inc(&state->count);
1661 return state;
1662}
1663
Andy Adamsone23008e2012-10-02 21:07:32 -04001664static void
1665nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1666{
1667 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1668 struct nfs_delegation *delegation;
1669 int delegation_flags = 0;
1670
1671 rcu_read_lock();
1672 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1673 if (delegation)
1674 delegation_flags = delegation->flags;
1675 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001676 switch (data->o_arg.claim) {
1677 default:
1678 break;
1679 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1680 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001681 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1682 "returning a delegation for "
1683 "OPEN(CLAIM_DELEGATE_CUR)\n",
1684 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001685 return;
1686 }
1687 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001688 nfs_inode_set_delegation(state->inode,
1689 data->owner->so_cred,
1690 &data->o_res);
1691 else
1692 nfs_inode_reclaim_delegation(state->inode,
1693 data->owner->so_cred,
1694 &data->o_res);
1695}
1696
1697/*
1698 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1699 * and update the nfs4_state.
1700 */
1701static struct nfs4_state *
1702_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1703{
1704 struct inode *inode = data->state->inode;
1705 struct nfs4_state *state = data->state;
1706 int ret;
1707
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001708 if (!data->rpc_done) {
1709 if (data->rpc_status) {
1710 ret = data->rpc_status;
1711 goto err;
1712 }
1713 /* cached opens have already been processed */
1714 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001715 }
1716
Andy Adamsone23008e2012-10-02 21:07:32 -04001717 ret = nfs_refresh_inode(inode, &data->f_attr);
1718 if (ret)
1719 goto err;
1720
1721 if (data->o_res.delegation_type != 0)
1722 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001723update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001724 update_open_stateid(state, &data->o_res.stateid, NULL,
1725 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001726 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001727
1728 return state;
1729err:
1730 return ERR_PTR(ret);
1731
1732}
1733
1734static struct nfs4_state *
1735_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001736{
1737 struct inode *inode;
1738 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001739 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001740
Trond Myklebustaac00a82007-07-05 19:02:21 -04001741 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001742 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001743 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001744 goto out;
1745 }
1746
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001747 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001748 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001749 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001750 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001751 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001752 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001753 goto err;
1754 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001755 state = nfs4_get_open_state(inode, data->owner);
1756 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001757 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001758 if (data->o_res.delegation_type != 0)
1759 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001760 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001761 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001763out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001764 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001765 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001766err_put_inode:
1767 iput(inode);
1768err:
1769 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001770}
1771
Andy Adamsone23008e2012-10-02 21:07:32 -04001772static struct nfs4_state *
1773nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1774{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001775 struct nfs4_state *ret;
1776
Andy Adamsone23008e2012-10-02 21:07:32 -04001777 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001778 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1779 else
1780 ret = _nfs4_opendata_to_nfs4_state(data);
1781 nfs4_sequence_free_slot(&data->o_res.seq_res);
1782 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001783}
1784
Trond Myklebust864472e2006-01-03 09:55:15 +01001785static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1786{
1787 struct nfs_inode *nfsi = NFS_I(state->inode);
1788 struct nfs_open_context *ctx;
1789
1790 spin_lock(&state->inode->i_lock);
1791 list_for_each_entry(ctx, &nfsi->open_files, list) {
1792 if (ctx->state != state)
1793 continue;
1794 get_nfs_open_context(ctx);
1795 spin_unlock(&state->inode->i_lock);
1796 return ctx;
1797 }
1798 spin_unlock(&state->inode->i_lock);
1799 return ERR_PTR(-ENOENT);
1800}
1801
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001802static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1803 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001804{
1805 struct nfs4_opendata *opendata;
1806
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001807 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001808 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001809 if (opendata == NULL)
1810 return ERR_PTR(-ENOMEM);
1811 opendata->state = state;
1812 atomic_inc(&state->count);
1813 return opendata;
1814}
1815
Trond Myklebust24311f82015-09-20 10:50:17 -04001816static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1817 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001818{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001819 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001820 int ret;
1821
Trond Myklebust24311f82015-09-20 10:50:17 -04001822 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001823 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001824 opendata->o_arg.open_flags = 0;
1825 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001826 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1827 NFS_SB(opendata->dentry->d_sb),
1828 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001829 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1830 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1831 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001832 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001833 if (ret != 0)
1834 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001835 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001836 if (IS_ERR(newstate))
1837 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001838 if (newstate != opendata->state)
1839 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001840 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001841 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001842}
1843
1844static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1845{
Trond Myklebust864472e2006-01-03 09:55:15 +01001846 int ret;
1847
Trond Myklebust4f14c192014-02-12 19:15:06 -05001848 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1849 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1850 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1851 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001852 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001853 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001854 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001855 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001856 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1857 if (ret != 0)
1858 return ret;
1859 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1860 if (ret != 0)
1861 return ret;
1862 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1863 if (ret != 0)
1864 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001865 /*
1866 * We may have performed cached opens for all three recoveries.
1867 * Check if we need to update the current stateid.
1868 */
1869 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001870 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001871 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001872 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001873 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001874 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001875 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001876 return 0;
1877}
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879/*
1880 * OPEN_RECLAIM:
1881 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001883static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001885 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001886 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001887 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 int status;
1889
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001890 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1891 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001892 if (IS_ERR(opendata))
1893 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001894 rcu_read_lock();
1895 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001896 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001897 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001898 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001899 opendata->o_arg.u.delegation_type = delegation_type;
1900 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001901 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return status;
1903}
1904
Trond Myklebust539cd032007-06-05 11:46:42 -04001905static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 struct nfs_server *server = NFS_SERVER(state->inode);
1908 struct nfs4_exception exception = { };
1909 int err;
1910 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001911 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001912 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001913 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1914 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001915 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001916 break;
1917 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 } while (exception.retry);
1919 return err;
1920}
1921
Trond Myklebust864472e2006-01-03 09:55:15 +01001922static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1923{
1924 struct nfs_open_context *ctx;
1925 int ret;
1926
1927 ctx = nfs4_state_find_open_context(state);
1928 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001929 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001930 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001931 put_nfs_open_context(ctx);
1932 return ret;
1933}
1934
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001935static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001937 switch (err) {
1938 default:
1939 printk(KERN_ERR "NFS: %s: unhandled error "
1940 "%d.\n", __func__, err);
1941 case 0:
1942 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001943 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001944 case -ESTALE:
1945 break;
1946 case -NFS4ERR_BADSESSION:
1947 case -NFS4ERR_BADSLOT:
1948 case -NFS4ERR_BAD_HIGH_SLOT:
1949 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1950 case -NFS4ERR_DEADSESSION:
1951 set_bit(NFS_DELEGATED_STATE, &state->flags);
1952 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1953 return -EAGAIN;
1954 case -NFS4ERR_STALE_CLIENTID:
1955 case -NFS4ERR_STALE_STATEID:
1956 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001957 /* Don't recall a delegation if it was lost */
1958 nfs4_schedule_lease_recovery(server->nfs_client);
1959 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001960 case -NFS4ERR_MOVED:
1961 nfs4_schedule_migration_recovery(server);
1962 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001963 case -NFS4ERR_LEASE_MOVED:
1964 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1965 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001966 case -NFS4ERR_DELEG_REVOKED:
1967 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001968 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001969 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001970 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001971 nfs_inode_find_state_and_recover(state->inode,
1972 stateid);
1973 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001974 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001975 case -NFS4ERR_DELAY:
1976 case -NFS4ERR_GRACE:
1977 set_bit(NFS_DELEGATED_STATE, &state->flags);
1978 ssleep(1);
1979 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001980 case -ENOMEM:
1981 case -NFS4ERR_DENIED:
1982 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1983 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return err;
1986}
1987
Trond Myklebust24311f82015-09-20 10:50:17 -04001988int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1989 struct nfs4_state *state, const nfs4_stateid *stateid,
1990 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001991{
1992 struct nfs_server *server = NFS_SERVER(state->inode);
1993 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001994 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001995
1996 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1997 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1998 if (IS_ERR(opendata))
1999 return PTR_ERR(opendata);
2000 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002001 write_seqlock(&state->seqlock);
2002 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2003 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002004 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2005 switch (type & (FMODE_READ|FMODE_WRITE)) {
2006 case FMODE_READ|FMODE_WRITE:
2007 case FMODE_WRITE:
2008 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2009 if (err)
2010 break;
2011 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2012 if (err)
2013 break;
2014 case FMODE_READ:
2015 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2016 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002017 nfs4_opendata_put(opendata);
2018 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2019}
2020
Chuck Leverbe05c862013-08-09 12:49:47 -04002021static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2022{
2023 struct nfs4_opendata *data = calldata;
2024
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002025 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2026 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002027}
2028
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002029static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2030{
2031 struct nfs4_opendata *data = calldata;
2032
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002033 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002034
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002035 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002036 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002037 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002038 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002039 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002040 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002041 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002042}
2043
2044static void nfs4_open_confirm_release(void *calldata)
2045{
2046 struct nfs4_opendata *data = calldata;
2047 struct nfs4_state *state = NULL;
2048
2049 /* If this request hasn't been cancelled, do nothing */
2050 if (data->cancelled == 0)
2051 goto out_free;
2052 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002053 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002054 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002055 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002056 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002057 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002058out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002059 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060}
2061
2062static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002063 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002064 .rpc_call_done = nfs4_open_confirm_done,
2065 .rpc_release = nfs4_open_confirm_release,
2066};
2067
2068/*
2069 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2070 */
2071static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2072{
David Howells2b0143b2015-03-17 22:25:59 +00002073 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002074 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002075 struct rpc_message msg = {
2076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2077 .rpc_argp = &data->c_arg,
2078 .rpc_resp = &data->c_res,
2079 .rpc_cred = data->owner->so_cred,
2080 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002081 struct rpc_task_setup task_setup_data = {
2082 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002083 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002084 .callback_ops = &nfs4_open_confirm_ops,
2085 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002086 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002087 .flags = RPC_TASK_ASYNC,
2088 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 int status;
2090
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002091 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002092 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002093 data->rpc_done = 0;
2094 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002095 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002096 if (data->is_recover)
2097 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002098 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002099 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002100 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002101 status = nfs4_wait_for_completion_rpc_task(task);
2102 if (status != 0) {
2103 data->cancelled = 1;
2104 smp_wmb();
2105 } else
2106 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002107 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return status;
2109}
2110
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002113 struct nfs4_opendata *data = calldata;
2114 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002115 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002116 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002117
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002118 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002119 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002120 /*
2121 * Check if we still need to send an OPEN call, or if we can use
2122 * a delegation instead.
2123 */
2124 if (data->state != NULL) {
2125 struct nfs_delegation *delegation;
2126
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002127 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002128 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002129 rcu_read_lock();
2130 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002131 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002132 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002133 rcu_read_unlock();
2134 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002135 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002136 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002137 switch (claim) {
2138 default:
2139 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002140 case NFS4_OPEN_CLAIM_PREVIOUS:
2141 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2142 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002143 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002144 case NFS4_OPEN_CLAIM_FH:
2145 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002146 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2147 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002148 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002149 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002150 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002151 &data->o_res.seq_res,
2152 task) != 0)
2153 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002154
2155 /* Set the create mode (note dependency on the session type) */
2156 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2157 if (data->o_arg.open_flags & O_EXCL) {
2158 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2159 if (nfs4_has_persistent_session(clp))
2160 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2161 else if (clp->cl_mvops->minor_version > 0)
2162 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2163 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002164 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002165unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002166 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002167 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002168out_no_action:
2169 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002170out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002171 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002172}
2173
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002174static void nfs4_open_done(struct rpc_task *task, void *calldata)
2175{
2176 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002178 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002179
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002180 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002181 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002182
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002183 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002184 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2185 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002186 case S_IFREG:
2187 break;
2188 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002189 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002190 break;
2191 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002192 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002193 break;
2194 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002195 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002196 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002197 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002198 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002199 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2200 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002201 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002202 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002203}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002204
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205static void nfs4_open_release(void *calldata)
2206{
2207 struct nfs4_opendata *data = calldata;
2208 struct nfs4_state *state = NULL;
2209
2210 /* If this request hasn't been cancelled, do nothing */
2211 if (data->cancelled == 0)
2212 goto out_free;
2213 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002214 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002215 goto out_free;
2216 /* In case we need an open_confirm, no cleanup! */
2217 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2218 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002219 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002220 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002221 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002222out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002223 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002224}
2225
2226static const struct rpc_call_ops nfs4_open_ops = {
2227 .rpc_call_prepare = nfs4_open_prepare,
2228 .rpc_call_done = nfs4_open_done,
2229 .rpc_release = nfs4_open_release,
2230};
2231
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002232static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002233{
David Howells2b0143b2015-03-17 22:25:59 +00002234 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002235 struct nfs_server *server = NFS_SERVER(dir);
2236 struct nfs_openargs *o_arg = &data->o_arg;
2237 struct nfs_openres *o_res = &data->o_res;
2238 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002239 struct rpc_message msg = {
2240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2241 .rpc_argp = o_arg,
2242 .rpc_resp = o_res,
2243 .rpc_cred = data->owner->so_cred,
2244 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002245 struct rpc_task_setup task_setup_data = {
2246 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002247 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002248 .callback_ops = &nfs4_open_ops,
2249 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002250 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002251 .flags = RPC_TASK_ASYNC,
2252 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002253 int status;
2254
Chuck Levera9c92d62013-08-09 12:48:18 -04002255 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002256 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002257 data->rpc_done = 0;
2258 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002259 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002260 data->is_recover = 0;
2261 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002262 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002263 data->is_recover = 1;
2264 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002265 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002266 if (IS_ERR(task))
2267 return PTR_ERR(task);
2268 status = nfs4_wait_for_completion_rpc_task(task);
2269 if (status != 0) {
2270 data->cancelled = 1;
2271 smp_wmb();
2272 } else
2273 status = data->rpc_status;
2274 rpc_put_task(task);
2275
2276 return status;
2277}
2278
2279static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2280{
David Howells2b0143b2015-03-17 22:25:59 +00002281 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002282 struct nfs_openres *o_res = &data->o_res;
2283 int status;
2284
2285 status = nfs4_run_open_task(data, 1);
2286 if (status != 0 || !data->rpc_done)
2287 return status;
2288
Trond Myklebust6926afd2012-01-07 13:22:46 -05002289 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2290
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002291 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2292 status = _nfs4_proc_open_confirm(data);
2293 if (status != 0)
2294 return status;
2295 }
2296
2297 return status;
2298}
2299
Trond Myklebustf3792d62014-07-10 08:54:32 -04002300/*
2301 * Additional permission checks in order to distinguish between an
2302 * open for read, and an open for execute. This works around the
2303 * fact that NFSv4 OPEN treats read and execute permissions as being
2304 * the same.
2305 * Note that in the non-execute case, we want to turn off permission
2306 * checking if we just created a new file (POSIX open() semantics).
2307 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002308static int nfs4_opendata_access(struct rpc_cred *cred,
2309 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002310 struct nfs4_state *state, fmode_t fmode,
2311 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002312{
2313 struct nfs_access_entry cache;
2314 u32 mask;
2315
2316 /* access call failed or for some reason the server doesn't
2317 * support any access modes -- defer access call until later */
2318 if (opendata->o_res.access_supported == 0)
2319 return 0;
2320
2321 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002322 /*
2323 * Use openflags to check for exec, because fmode won't
2324 * always have FMODE_EXEC set when file open for exec.
2325 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002326 if (openflags & __FMODE_EXEC) {
2327 /* ONLY check for exec rights */
2328 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002329 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002330 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002331
2332 cache.cred = cred;
2333 cache.jiffies = jiffies;
2334 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2335 nfs_access_add_cache(state->inode, &cache);
2336
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002337 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002338 return 0;
2339
2340 /* even though OPEN succeeded, access is denied. Close the file */
2341 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002342 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002343}
2344
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002345/*
2346 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2347 */
2348static int _nfs4_proc_open(struct nfs4_opendata *data)
2349{
David Howells2b0143b2015-03-17 22:25:59 +00002350 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002351 struct nfs_server *server = NFS_SERVER(dir);
2352 struct nfs_openargs *o_arg = &data->o_arg;
2353 struct nfs_openres *o_res = &data->o_res;
2354 int status;
2355
2356 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002357 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002358 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002359 if (status != 0) {
2360 if (status == -NFS4ERR_BADNAME &&
2361 !(o_arg->open_flags & O_CREAT))
2362 return -ENOENT;
2363 return status;
2364 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002365
Trond Myklebust6926afd2012-01-07 13:22:46 -05002366 nfs_fattr_map_and_free_names(server, &data->f_attr);
2367
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002368 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002369 if (o_arg->open_flags & O_EXCL)
2370 data->file_created = 1;
2371 else if (o_res->cinfo.before != o_res->cinfo.after)
2372 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002373 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002374 update_changeattr(dir, &o_res->cinfo,
2375 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002376 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002377 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2378 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002380 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002382 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002385 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Andy Adamsond83217c2011-03-01 01:34:17 +00002389static int nfs4_recover_expired_lease(struct nfs_server *server)
2390{
2391 return nfs4_client_recover_expired_lease(server->nfs_client);
2392}
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394/*
2395 * OPEN_EXPIRED:
2396 * reclaim state on the server after a network partition.
2397 * Assumes caller holds the appropriate lock
2398 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002399static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002401 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002402 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002404 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002405 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002406 if (IS_ERR(opendata))
2407 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002408 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002409 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002410 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002411 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002415static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002416{
Trond Myklebust539cd032007-06-05 11:46:42 -04002417 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002418 struct nfs4_exception exception = { };
2419 int err;
2420
2421 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002422 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002423 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002424 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2425 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002426 switch (err) {
2427 default:
2428 goto out;
2429 case -NFS4ERR_GRACE:
2430 case -NFS4ERR_DELAY:
2431 nfs4_handle_exception(server, err, &exception);
2432 err = 0;
2433 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002434 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002435out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002436 return err;
2437}
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002442 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Trond Myklebust864472e2006-01-03 09:55:15 +01002444 ctx = nfs4_state_find_open_context(state);
2445 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002446 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002447 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002448 put_nfs_open_context(ctx);
2449 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Trond Myklebust41020b62016-09-22 13:38:58 -04002452static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2453 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002454{
Trond Myklebust41020b62016-09-22 13:38:58 -04002455 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002456 write_seqlock(&state->seqlock);
2457 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2458 write_sequnlock(&state->seqlock);
2459 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2460}
2461
2462static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2463{
2464 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002465 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002466}
2467
2468static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2469{
2470 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2471 nfs40_clear_delegation_stateid(state);
2472 return nfs4_open_expired(sp, state);
2473}
2474
Trond Myklebust45870d62016-09-22 13:38:59 -04002475static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2476 nfs4_stateid *stateid,
2477 struct rpc_cred *cred)
2478{
2479 return -NFS4ERR_BAD_STATEID;
2480}
2481
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002482#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002483static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2484 nfs4_stateid *stateid,
2485 struct rpc_cred *cred)
2486{
2487 int status;
2488
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002489 switch (stateid->type) {
2490 default:
2491 break;
2492 case NFS4_INVALID_STATEID_TYPE:
2493 case NFS4_SPECIAL_STATEID_TYPE:
2494 return -NFS4ERR_BAD_STATEID;
2495 case NFS4_REVOKED_STATEID_TYPE:
2496 goto out_free;
2497 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002498
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002499 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002500 switch (status) {
2501 case -NFS4ERR_EXPIRED:
2502 case -NFS4ERR_ADMIN_REVOKED:
2503 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002504 break;
2505 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002506 return status;
2507 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002508out_free:
2509 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002510 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002511 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002512}
2513
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002514static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002515{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002516 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002517 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002518 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002519 struct rpc_cred *cred;
2520 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002521
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002522 /* Get the delegation credential for use by test/free_stateid */
2523 rcu_read_lock();
2524 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002525 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002526 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002527 return;
2528 }
2529
2530 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002531 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2532 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002533 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002534 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002535 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002536
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002537 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2538 rcu_read_unlock();
2539 return;
2540 }
2541
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002542 cred = get_rpccred(delegation->cred);
2543 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002544 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002545 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002546 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002547 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002548
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002549 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002550}
2551
2552/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002553 * nfs41_check_expired_locks - possibly free a lock stateid
2554 *
2555 * @state: NFSv4 state for an inode
2556 *
2557 * Returns NFS_OK if recovery for this stateid is now finished.
2558 * Otherwise a negative NFS4ERR value is returned.
2559 */
2560static int nfs41_check_expired_locks(struct nfs4_state *state)
2561{
2562 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002563 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002564 struct nfs_server *server = NFS_SERVER(state->inode);
2565
2566 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2567 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002568
2569 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002570 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2571 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2572 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2573
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002574 atomic_inc(&lsp->ls_count);
2575 spin_unlock(&state->state_lock);
2576
2577 nfs4_put_lock_state(prev);
2578 prev = lsp;
2579
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002580 status = nfs41_test_and_free_expired_stateid(server,
2581 &lsp->ls_stateid,
2582 cred);
2583 trace_nfs4_test_lock_stateid(state, lsp, status);
2584 if (status == -NFS4ERR_EXPIRED ||
2585 status == -NFS4ERR_BAD_STATEID) {
2586 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002587 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002588 if (!recover_lost_locks)
2589 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2590 } else if (status != NFS_OK) {
2591 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002592 nfs4_put_lock_state(prev);
2593 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002594 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002595 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002596 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002597 }
2598 spin_unlock(&state->state_lock);
2599 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002600out:
2601 return ret;
2602}
2603
2604/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002605 * nfs41_check_open_stateid - possibly free an open stateid
2606 *
2607 * @state: NFSv4 state for an inode
2608 *
2609 * Returns NFS_OK if recovery for this stateid is now finished.
2610 * Otherwise a negative NFS4ERR value is returned.
2611 */
2612static int nfs41_check_open_stateid(struct nfs4_state *state)
2613{
2614 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002615 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002616 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002617 int status;
2618
Trond Myklebustb134fc42016-09-22 13:39:16 -04002619 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002620 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2621 if (nfs4_have_delegation(state->inode, state->state))
2622 return NFS_OK;
2623 return -NFS4ERR_OPENMODE;
2624 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002626 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002627 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002628 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002629 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002630 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2631 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2632 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002633 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002634 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002635 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002636 if (status != NFS_OK)
2637 return status;
2638 if (nfs_open_stateid_recover_openmode(state))
2639 return -NFS4ERR_OPENMODE;
2640 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002641}
2642
2643static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2644{
Chuck Levereb64cf92012-07-11 16:30:05 -04002645 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002646
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002647 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002648 status = nfs41_check_expired_locks(state);
2649 if (status != NFS_OK)
2650 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002651 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002652 if (status != NFS_OK)
2653 status = nfs4_open_expired(sp, state);
2654 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002655}
2656#endif
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002659 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2660 * fields corresponding to attributes that were used to store the verifier.
2661 * Make sure we clobber those fields in the later setattr call
2662 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002663static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2664 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002665{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002666 const u32 *attrset = opendata->o_res.attrset;
2667
2668 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002669 !(sattr->ia_valid & ATTR_ATIME_SET))
2670 sattr->ia_valid |= ATTR_ATIME;
2671
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002672 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002673 !(sattr->ia_valid & ATTR_MTIME_SET))
2674 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002675
2676 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002677 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2678 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002679 sattr->ia_valid &= ~ATTR_MODE;
2680
2681 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2682 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002683}
2684
Trond Myklebustc21443c2013-02-07 14:26:21 -05002685static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2686 fmode_t fmode,
2687 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002688 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002689{
2690 struct nfs4_state_owner *sp = opendata->owner;
2691 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002692 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002693 struct nfs4_state *state;
2694 unsigned int seq;
2695 int ret;
2696
2697 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2698
2699 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002700 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002701 goto out;
2702
2703 state = nfs4_opendata_to_nfs4_state(opendata);
2704 ret = PTR_ERR(state);
2705 if (IS_ERR(state))
2706 goto out;
2707 if (server->caps & NFS_CAP_POSIX_LOCK)
2708 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002709 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2710 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002711
Trond Myklebust275bb302013-05-29 13:11:28 -04002712 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002713 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002714 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002715 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002716 alias = d_exact_alias(dentry, state->inode);
2717 if (!alias)
2718 alias = d_splice_alias(igrab(state->inode), dentry);
2719 /* d_splice_alias() can't fail here - it's a non-directory */
2720 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002721 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002722 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002723 }
2724 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002725 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002726 }
2727
Trond Myklebustc21443c2013-02-07 14:26:21 -05002728 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2729 if (ret != 0)
2730 goto out;
2731
Trond Myklebust3efb9722013-05-29 13:17:04 -04002732 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002733 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002734 nfs_inode_attach_open_context(ctx);
2735 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2736 nfs4_schedule_stateid_recovery(server, state);
2737 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002738out:
2739 return ret;
2740}
2741
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002742/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002743 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 */
Andy Adamson82be4172012-05-23 05:02:35 -04002745static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002746 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002747 int flags,
2748 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002749 struct nfs4_label *label,
2750 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 struct nfs4_state_owner *sp;
2753 struct nfs4_state *state = NULL;
2754 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002755 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002756 struct dentry *dentry = ctx->dentry;
2757 struct rpc_cred *cred = ctx->cred;
2758 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2759 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002760 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002761 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002762 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002766 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2767 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2769 goto out_err;
2770 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002771 status = nfs4_recover_expired_lease(server);
2772 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002773 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002774 if (d_really_is_positive(dentry))
2775 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002776 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002777 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002778 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002779 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002780 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002781 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002782 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
David Quigley14c43f72013-05-22 12:50:43 -04002784 if (label) {
2785 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2786 if (IS_ERR(olabel)) {
2787 status = PTR_ERR(olabel);
2788 goto err_opendata_put;
2789 }
2790 }
2791
Trond Myklebuste911b812014-03-26 13:24:37 -07002792 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2793 if (!opendata->f_attr.mdsthreshold) {
2794 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2795 if (!opendata->f_attr.mdsthreshold)
2796 goto err_free_label;
2797 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002798 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002799 }
David Howells2b0143b2015-03-17 22:25:59 +00002800 if (d_really_is_positive(dentry))
2801 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002802
Trond Myklebust3efb9722013-05-29 13:17:04 -04002803 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002804 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002805 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002806 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002807
NeilBrownefcbc042015-07-30 13:00:56 +10002808 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002809 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002810 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002811 /*
2812 * send create attributes which was not set by open
2813 * with an extra setattr.
2814 */
2815 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2816 nfs_fattr_init(opendata->o_res.f_attr);
2817 status = nfs4_do_setattr(state->inode, cred,
2818 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002819 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002820 if (status == 0) {
2821 nfs_setattr_update_inode(state->inode, sattr,
2822 opendata->o_res.f_attr);
2823 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2824 }
David Quigley1775fd32013-05-22 12:50:42 -04002825 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002826 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002827 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002828 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002829
Trond Myklebuste911b812014-03-26 13:24:37 -07002830 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002831 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002832 opendata->f_attr.mdsthreshold = NULL;
2833 }
Andy Adamson82be4172012-05-23 05:02:35 -04002834
David Quigley14c43f72013-05-22 12:50:43 -04002835 nfs4_label_free(olabel);
2836
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002837 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002840err_free_label:
2841 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002842err_opendata_put:
2843 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002844err_put_state_owner:
2845 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return status;
2848}
2849
2850
Andy Adamson82be4172012-05-23 05:02:35 -04002851static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002852 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002853 int flags,
2854 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002855 struct nfs4_label *label,
2856 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002858 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct nfs4_exception exception = { };
2860 struct nfs4_state *res;
2861 int status;
2862
2863 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002864 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002865 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002866 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 if (status == 0)
2868 break;
2869 /* NOTE: BAD_SEQID means the server and client disagree about the
2870 * book-keeping w.r.t. state-changing operations
2871 * (OPEN/CLOSE/LOCK/LOCKU...)
2872 * It is actually a sign of a bug on the client or on the server.
2873 *
2874 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002875 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 * have unhashed the old state_owner for us, and that we can
2877 * therefore safely retry using a new one. We should still warn
2878 * the user though...
2879 */
2880 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002881 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002882 " returned a bad sequence-id error!\n",
2883 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 exception.retry = 1;
2885 continue;
2886 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002887 /*
2888 * BAD_STATEID on OPEN means that the server cancelled our
2889 * state before it received the OPEN_CONFIRM.
2890 * Recover by retrying the request as per the discussion
2891 * on Page 181 of RFC3530.
2892 */
2893 if (status == -NFS4ERR_BAD_STATEID) {
2894 exception.retry = 1;
2895 continue;
2896 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002897 if (status == -EAGAIN) {
2898 /* We must have found a delegation */
2899 exception.retry = 1;
2900 continue;
2901 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002902 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2903 continue;
2904 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 status, &exception));
2906 } while (exception.retry);
2907 return res;
2908}
2909
Trond Myklebust8487c472016-06-26 08:44:35 -04002910static int _nfs4_do_setattr(struct inode *inode,
2911 struct nfs_setattrargs *arg,
2912 struct nfs_setattrres *res,
2913 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002914 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002915{
2916 struct nfs_server *server = NFS_SERVER(inode);
2917 struct rpc_message msg = {
2918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2919 .rpc_argp = arg,
2920 .rpc_resp = res,
2921 .rpc_cred = cred,
2922 };
2923 struct rpc_cred *delegation_cred = NULL;
2924 unsigned long timestamp = jiffies;
2925 fmode_t fmode;
2926 bool truncate;
2927 int status;
2928
2929 nfs_fattr_init(res->fattr);
2930
2931 /* Servers should only apply open mode checks for file size changes */
2932 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2933 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2934
2935 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2936 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002937 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002938 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002939 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002940 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002941 l_ctx = nfs_get_lock_context(ctx);
2942 if (IS_ERR(l_ctx))
2943 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002944 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2945 &arg->stateid, &delegation_cred);
2946 nfs_put_lock_context(l_ctx);
2947 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002948 return -EBADF;
2949 } else
2950 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2951 if (delegation_cred)
2952 msg.rpc_cred = delegation_cred;
2953
2954 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2955
2956 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002957 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002958 renew_lease(server, timestamp);
2959 trace_nfs4_setattr(inode, &arg->stateid, status);
2960 return status;
2961}
2962
2963static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2964 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002965 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002966 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002968 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002969 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002971 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 .iap = sattr,
2973 .server = server,
2974 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002975 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 };
2977 struct nfs_setattrres res = {
2978 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002979 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 .server = server,
2981 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002982 struct nfs4_exception exception = {
2983 .state = state,
2984 .inode = inode,
2985 .stateid = &arg.stateid,
2986 };
2987 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
David Quigleyaa9c2662013-05-22 12:50:44 -04002989 arg.bitmask = nfs4_bitmask(server, ilabel);
2990 if (ilabel)
2991 arg.bitmask = nfs4_bitmask(server, olabel);
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002994 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002995 switch (err) {
2996 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002997 if (!(sattr->ia_valid & ATTR_SIZE)) {
2998 pr_warn_once("NFSv4: server %s is incorrectly "
2999 "applying open mode checks to "
3000 "a SETATTR that is not "
3001 "changing file size.\n",
3002 server->nfs_client->cl_hostname);
3003 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003004 if (state && !(state->state & FMODE_WRITE)) {
3005 err = -EBADF;
3006 if (sattr->ia_valid & ATTR_OPEN)
3007 err = -EACCES;
3008 goto out;
3009 }
3010 }
3011 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003013out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return err;
3015}
3016
Peng Tao500d7012015-09-22 11:35:22 +08003017static bool
3018nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3019{
3020 if (inode == NULL || !nfs_have_layout(inode))
3021 return false;
3022
3023 return pnfs_wait_on_layoutreturn(inode, task);
3024}
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026struct nfs4_closedata {
3027 struct inode *inode;
3028 struct nfs4_state *state;
3029 struct nfs_closeargs arg;
3030 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003031 struct {
3032 struct nfs4_layoutreturn_args arg;
3033 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003034 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003035 u32 roc_barrier;
3036 bool roc;
3037 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003038 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003039 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040};
3041
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003042static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003043{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003044 struct nfs4_closedata *calldata = data;
3045 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003046 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003047
Trond Myklebustcf805162016-11-15 14:56:07 -05003048 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003049 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3050 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003051 nfs4_put_open_state(calldata->state);
3052 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003053 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003054 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003055 kfree(calldata);
3056}
3057
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003058static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003060 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003063 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Chuck Levera3ca5652012-03-01 17:00:40 -05003065 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003066 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3067 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003068 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003069
3070 /* Handle Layoutreturn errors */
3071 if (calldata->arg.lr_args && task->tk_status != 0) {
3072 switch (calldata->res.lr_ret) {
3073 default:
3074 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3075 break;
3076 case 0:
3077 calldata->arg.lr_args = NULL;
3078 calldata->res.lr_res = NULL;
3079 break;
3080 case -NFS4ERR_ADMIN_REVOKED:
3081 case -NFS4ERR_DELEG_REVOKED:
3082 case -NFS4ERR_EXPIRED:
3083 case -NFS4ERR_BAD_STATEID:
3084 case -NFS4ERR_OLD_STATEID:
3085 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3086 case -NFS4ERR_WRONG_CRED:
3087 calldata->arg.lr_args = NULL;
3088 calldata->res.lr_res = NULL;
3089 calldata->res.lr_ret = 0;
3090 rpc_restart_call_prepare(task);
3091 return;
3092 }
3093 }
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 /* hmm. we are done with the inode, and in the process of freeing
3096 * the state_owner. we keep this around to process errors
3097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 switch (task->tk_status) {
3099 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003100 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003101 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003102 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003103 case -NFS4ERR_ACCESS:
3104 if (calldata->arg.bitmask != NULL) {
3105 calldata->arg.bitmask = NULL;
3106 calldata->res.fattr = NULL;
3107 task->tk_status = 0;
3108 rpc_restart_call_prepare(task);
3109 goto out_release;
3110
3111 }
3112 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003113 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003115 case -NFS4ERR_EXPIRED:
3116 nfs4_free_revoked_stateid(server,
3117 &calldata->arg.stateid,
3118 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003119 case -NFS4ERR_OLD_STATEID:
3120 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003121 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003122 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003123 rpc_restart_call_prepare(task);
3124 goto out_release;
3125 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003126 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003129 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003130 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003131 goto out_release;
3132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003134 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3135 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003136out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003137 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003138 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003139 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003142static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003144 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003145 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003146 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003147 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003148 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003149
Chuck Levera3ca5652012-03-01 17:00:40 -05003150 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003151 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003152 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003153
Trond Myklebust88069f72009-12-08 08:33:16 -05003154 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003155 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003156 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3157 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3158 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003159 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003160 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003161 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003162 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003163 if (state->n_rdonly == 0)
3164 call_close |= is_rdonly;
3165 else if (is_rdonly)
3166 calldata->arg.fmode |= FMODE_READ;
3167 if (state->n_wronly == 0)
3168 call_close |= is_wronly;
3169 else if (is_wronly)
3170 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003171 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3172 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003173 } else if (is_rdwr)
3174 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3175
Trond Myklebust5cc78612016-11-14 11:19:56 -05003176 if (!nfs4_valid_open_stateid(state) ||
3177 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003178 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003179 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003180
3181 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003182 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003183 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003184 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003185
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003186 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003187 nfs_release_seqid(calldata->arg.seqid);
3188 goto out_wait;
3189 }
3190
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003191 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003192 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003193
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003194 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003195 /* Close-to-open cache consistency revalidation */
3196 if (!nfs4_have_delegation(inode, FMODE_READ))
3197 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3198 else
3199 calldata->arg.bitmask = NULL;
3200 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003201
Trond Myklebust6ae37332015-01-30 14:21:14 -05003202 calldata->arg.share_access =
3203 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3204 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003205
Trond Myklebustd8d84982016-12-19 12:14:44 -05003206 if (calldata->res.fattr == NULL)
3207 calldata->arg.bitmask = NULL;
3208 else if (calldata->arg.bitmask == NULL)
3209 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003210 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003211 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003212 &calldata->arg.seq_args,
3213 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003214 task) != 0)
3215 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003216 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003217 return;
3218out_no_action:
3219 task->tk_action = NULL;
3220out_wait:
3221 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003224static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003225 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003226 .rpc_call_done = nfs4_close_done,
3227 .rpc_release = nfs4_free_closedata,
3228};
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230/*
3231 * It is possible for data to be read/written from a mem-mapped file
3232 * after the sys_close call (which hits the vfs layer as a flush).
3233 * This means that we can't safely call nfsv4 close on a file until
3234 * the inode is cleared. This in turn means that we are not good
3235 * NFSv4 citizens - we do not indicate to the server to update the file's
3236 * share state even when we are done with one of the three share
3237 * stateid's in the inode.
3238 *
3239 * NOTE: Caller must be holding the sp->so_owner semaphore!
3240 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003241int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003243 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003244 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003246 struct nfs4_state_owner *sp = state->owner;
3247 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003248 struct rpc_message msg = {
3249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3250 .rpc_cred = state->owner->so_cred,
3251 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003252 struct rpc_task_setup task_setup_data = {
3253 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003254 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003255 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003256 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003257 .flags = RPC_TASK_ASYNC,
3258 };
Trond Myklebust95121352005-10-18 14:20:12 -07003259 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003261 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3262 &task_setup_data.rpc_client, &msg);
3263
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003264 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003266 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003267 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003268 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003270 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003272 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3273 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003274 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003275 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003276 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003277 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003278 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003279 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003280 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003281 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003282 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003283 calldata->lr.roc = pnfs_roc(state->inode,
3284 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3285 if (calldata->lr.roc) {
3286 calldata->arg.lr_args = &calldata->lr.arg;
3287 calldata->res.lr_res = &calldata->lr.res;
3288 }
Al Viro643168c2011-06-22 18:20:23 -04003289 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003290
Trond Myklebust1174dd12010-12-21 10:52:24 -05003291 msg.rpc_argp = &calldata->arg;
3292 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003293 task_setup_data.callback_data = calldata;
3294 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003295 if (IS_ERR(task))
3296 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003297 status = 0;
3298 if (wait)
3299 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003300 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003301 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003302out_free_calldata:
3303 kfree(calldata);
3304out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003305 nfs4_put_open_state(state);
3306 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003307 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
Trond Myklebust2b484292010-09-17 10:56:51 -04003310static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003311nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3312 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003315 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3316
3317 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Trond Myklebust565277f2007-10-15 18:17:53 -04003319 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003320 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003321
3322 nfs4_label_release_security(label);
3323
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003324 if (IS_ERR(state))
3325 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003326 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
Trond Myklebust1185a552009-12-03 15:54:02 -05003329static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003330{
3331 if (ctx->state == NULL)
3332 return;
3333 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003334 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003335 else
Al Viro643168c2011-06-22 18:20:23 -04003336 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Trond Myklebustb944dba2013-11-04 15:20:20 -05003339#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3340#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003341#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3344{
Kinglong Mee8c612822015-08-26 21:12:58 +08003345 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003346 struct nfs4_server_caps_arg args = {
3347 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003348 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003349 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct nfs4_server_caps_res res = {};
3351 struct rpc_message msg = {
3352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003353 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 .rpc_resp = &res,
3355 };
3356 int status;
3357
Kinglong Mee8c612822015-08-26 21:12:58 +08003358 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3359 FATTR4_WORD0_FH_EXPIRE_TYPE |
3360 FATTR4_WORD0_LINK_SUPPORT |
3361 FATTR4_WORD0_SYMLINK_SUPPORT |
3362 FATTR4_WORD0_ACLSUPPORT;
3363 if (minorversion)
3364 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3365
Bryan Schumaker7c513052011-03-24 17:12:24 +00003366 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003368 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003369 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003370 case 0:
3371 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3372 res.attr_bitmask[2] = 0;
3373 break;
3374 case 1:
3375 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3376 break;
3377 case 2:
3378 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003381 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3382 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3383 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3384 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003385 NFS_CAP_CTIME|NFS_CAP_MTIME|
3386 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003387 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3388 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 server->caps |= NFS_CAP_ACLS;
3390 if (res.has_links != 0)
3391 server->caps |= NFS_CAP_HARDLINKS;
3392 if (res.has_symlinks != 0)
3393 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003394 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3395 server->caps |= NFS_CAP_FILEID;
3396 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3397 server->caps |= NFS_CAP_MODE;
3398 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3399 server->caps |= NFS_CAP_NLINK;
3400 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3401 server->caps |= NFS_CAP_OWNER;
3402 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3403 server->caps |= NFS_CAP_OWNER_GROUP;
3404 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3405 server->caps |= NFS_CAP_ATIME;
3406 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3407 server->caps |= NFS_CAP_CTIME;
3408 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3409 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003410#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3411 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3412 server->caps |= NFS_CAP_SECURITY_LABEL;
3413#endif
3414 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3415 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003416 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003417
Trond Myklebusta65318b2009-03-11 14:10:28 -04003418 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3419 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3420 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003421 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003422 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3423 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003425 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return status;
3429}
3430
Trond Myklebust55a97592006-06-09 09:34:19 -04003431int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 struct nfs4_exception exception = { };
3434 int err;
3435 do {
3436 err = nfs4_handle_exception(server,
3437 _nfs4_server_capabilities(server, fhandle),
3438 &exception);
3439 } while (exception.retry);
3440 return err;
3441}
3442
3443static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3444 struct nfs_fsinfo *info)
3445{
David Quigleyaa9c2662013-05-22 12:50:44 -04003446 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003448 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 };
3450 struct nfs4_lookup_res res = {
3451 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003452 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 .fh = fhandle,
3454 };
3455 struct rpc_message msg = {
3456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3457 .rpc_argp = &args,
3458 .rpc_resp = &res,
3459 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003460
David Quigleyaa9c2662013-05-22 12:50:44 -04003461 bitmask[0] = nfs4_fattr_bitmap[0];
3462 bitmask[1] = nfs4_fattr_bitmap[1];
3463 /*
3464 * Process the label in the upcoming getfattr
3465 */
3466 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3467
Trond Myklebust0e574af2005-10-27 22:12:38 -04003468 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003469 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
3472static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3473 struct nfs_fsinfo *info)
3474{
3475 struct nfs4_exception exception = { };
3476 int err;
3477 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003478 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003479 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003480 switch (err) {
3481 case 0:
3482 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003483 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003484 default:
3485 err = nfs4_handle_exception(server, err, &exception);
3486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003488out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return err;
3490}
3491
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003492static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3493 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3494{
Trond Myklebustc2190662013-08-26 19:23:04 -04003495 struct rpc_auth_create_args auth_args = {
3496 .pseudoflavor = flavor,
3497 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003498 struct rpc_auth *auth;
3499 int ret;
3500
Trond Myklebustc2190662013-08-26 19:23:04 -04003501 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003502 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003503 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003504 goto out;
3505 }
3506 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003507out:
3508 return ret;
3509}
3510
Chuck Lever9a744ba2013-03-16 15:56:02 -04003511/*
3512 * Retry pseudoroot lookup with various security flavors. We do this when:
3513 *
3514 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3515 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3516 *
3517 * Returns zero on success, or a negative NFS4ERR value, or a
3518 * negative errno value.
3519 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003520static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003521 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003523 /* Per 3530bis 15.33.5 */
3524 static const rpc_authflavor_t flav_array[] = {
3525 RPC_AUTH_GSS_KRB5P,
3526 RPC_AUTH_GSS_KRB5I,
3527 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003528 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003529 RPC_AUTH_NULL,
3530 };
3531 int status = -EPERM;
3532 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003534 if (server->auth_info.flavor_len > 0) {
3535 /* try each flavor specified by user */
3536 for (i = 0; i < server->auth_info.flavor_len; i++) {
3537 status = nfs4_lookup_root_sec(server, fhandle, info,
3538 server->auth_info.flavors[i]);
3539 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3540 continue;
3541 break;
3542 }
3543 } else {
3544 /* no flavors specified by user, try default list */
3545 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3546 status = nfs4_lookup_root_sec(server, fhandle, info,
3547 flav_array[i]);
3548 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3549 continue;
3550 break;
3551 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003552 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003553
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003554 /*
3555 * -EACCESS could mean that the user doesn't have correct permissions
3556 * to access the mount. It could also mean that we tried to mount
3557 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3558 * existing mount programs don't handle -EACCES very well so it should
3559 * be mapped to -EPERM instead.
3560 */
3561 if (status == -EACCES)
3562 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003563 return status;
3564}
3565
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003566/**
3567 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3568 * @server: initialized nfs_server handle
3569 * @fhandle: we fill in the pseudo-fs root file handle
3570 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003571 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003572 *
3573 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003574 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003575int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003576 struct nfs_fsinfo *info,
3577 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003578{
Andre Przywarac7757072015-04-23 17:17:40 +01003579 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003580
Andre Przywarac7757072015-04-23 17:17:40 +01003581 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003582 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003583
3584 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003585 status = server->nfs_client->cl_mvops->find_root_sec(server,
3586 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if (status == 0)
3589 status = nfs4_server_capabilities(server, fhandle);
3590 if (status == 0)
3591 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003592
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003593 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
Bryan Schumakerbae36242012-05-10 15:07:31 -04003596static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3597 struct nfs_fsinfo *info)
3598{
3599 int error;
3600 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003601 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003602
3603 error = nfs4_server_capabilities(server, mntfh);
3604 if (error < 0) {
3605 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3606 return error;
3607 }
3608
David Quigley14c43f72013-05-22 12:50:43 -04003609 label = nfs4_label_alloc(server, GFP_KERNEL);
3610 if (IS_ERR(label))
3611 return PTR_ERR(label);
3612
David Quigley1775fd32013-05-22 12:50:42 -04003613 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003614 if (error < 0) {
3615 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003616 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003617 }
3618
3619 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3620 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3621 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3622
David Quigley14c43f72013-05-22 12:50:43 -04003623err_free_label:
3624 nfs4_label_free(label);
3625
Bryan Schumakerbae36242012-05-10 15:07:31 -04003626 return error;
3627}
3628
Manoj Naik6b97fd32006-06-09 09:34:29 -04003629/*
3630 * Get locations and (maybe) other attributes of a referral.
3631 * Note that we'll actually follow the referral later when
3632 * we detect fsid mismatch in inode revalidation
3633 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003634static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3635 const struct qstr *name, struct nfs_fattr *fattr,
3636 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003637{
3638 int status = -ENOMEM;
3639 struct page *page = NULL;
3640 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003641
3642 page = alloc_page(GFP_KERNEL);
3643 if (page == NULL)
3644 goto out;
3645 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3646 if (locations == NULL)
3647 goto out;
3648
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003649 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003650 if (status != 0)
3651 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003652
3653 /*
3654 * If the fsid didn't change, this is a migration event, not a
3655 * referral. Cause us to drop into the exception handler, which
3656 * will kick off migration recovery.
3657 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003658 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003659 dprintk("%s: server did not return a different fsid for"
3660 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003661 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003662 goto out;
3663 }
Andy Adamson533eb462011-06-13 18:25:56 -04003664 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3665 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003666
Andy Adamson533eb462011-06-13 18:25:56 -04003667 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003668 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003669 memset(fhandle, 0, sizeof(struct nfs_fh));
3670out:
3671 if (page)
3672 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003673 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003674 return status;
3675}
3676
David Quigley1775fd32013-05-22 12:50:42 -04003677static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3678 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 struct nfs4_getattr_arg args = {
3681 .fh = fhandle,
3682 .bitmask = server->attr_bitmask,
3683 };
3684 struct nfs4_getattr_res res = {
3685 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003686 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 .server = server,
3688 };
3689 struct rpc_message msg = {
3690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3691 .rpc_argp = &args,
3692 .rpc_resp = &res,
3693 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003694
3695 args.bitmask = nfs4_bitmask(server, label);
3696
Trond Myklebust0e574af2005-10-27 22:12:38 -04003697 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003698 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
3700
David Quigley1775fd32013-05-22 12:50:42 -04003701static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3702 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
3704 struct nfs4_exception exception = { };
3705 int err;
3706 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003707 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3708 trace_nfs4_getattr(server, fhandle, fattr, err);
3709 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 &exception);
3711 } while (exception.retry);
3712 return err;
3713}
3714
3715/*
3716 * The file is not closed if it is opened due to the a request to change
3717 * the size of the file. The open call will not be needed once the
3718 * VFS layer lookup-intents are implemented.
3719 *
3720 * Close is called when the inode is destroyed.
3721 * If we haven't opened the file for O_WRONLY, we
3722 * need to in the size_change case to obtain a stateid.
3723 *
3724 * Got race?
3725 * Because OPEN is always done by name in nfsv4, it is
3726 * possible that we opened a different file by the same
3727 * name. We can recognize this race condition, but we
3728 * can't do anything about it besides returning an error.
3729 *
3730 * This will be fixed with VFS changes (lookup-intent).
3731 */
3732static int
3733nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3734 struct iattr *sattr)
3735{
David Howells2b0143b2015-03-17 22:25:59 +00003736 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003737 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003738 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003739 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 int status;
3741
Peng Tao88ac8152014-09-12 11:04:10 +08003742 if (pnfs_ld_layoutret_on_setattr(inode) &&
3743 sattr->ia_valid & ATTR_SIZE &&
3744 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003745 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003746
Trond Myklebust0e574af2005-10-27 22:12:38 -04003747 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Andy Adamson26699402012-05-30 16:12:24 -04003749 /* Deal with open(O_TRUNC) */
3750 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003751 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003752
3753 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003754 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003755 return 0;
3756
Trond Myklebustd5308382005-11-04 15:33:38 -05003757 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003758 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003759
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003760 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003761 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003762 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003763 }
3764
David Quigley14c43f72013-05-22 12:50:43 -04003765 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3766 if (IS_ERR(label))
3767 return PTR_ERR(label);
3768
NeilBrown29b59f92016-10-13 15:26:47 +11003769 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003770 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003771 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003772 nfs_setsecurity(inode, fattr, label);
3773 }
David Quigley14c43f72013-05-22 12:50:43 -04003774 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return status;
3776}
3777
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003778static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3779 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003780 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003781{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003782 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003783 int status;
3784 struct nfs4_lookup_arg args = {
3785 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003786 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003787 .name = name,
3788 };
3789 struct nfs4_lookup_res res = {
3790 .server = server,
3791 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003792 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003793 .fh = fhandle,
3794 };
3795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3797 .rpc_argp = &args,
3798 .rpc_resp = &res,
3799 };
3800
David Quigleyaa9c2662013-05-22 12:50:44 -04003801 args.bitmask = nfs4_bitmask(server, label);
3802
David Howells2b3de442006-08-22 20:06:09 -04003803 nfs_fattr_init(fattr);
3804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003806 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 dprintk("NFS reply lookup: %d\n", status);
3808 return status;
3809}
3810
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003811static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003812{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003813 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003814 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003815 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3816 fattr->nlink = 2;
3817}
3818
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003819static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003820 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003821 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003822{
3823 struct nfs4_exception exception = { };
3824 struct rpc_clnt *client = *clnt;
3825 int err;
3826 do {
David Quigley1775fd32013-05-22 12:50:42 -04003827 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003828 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003829 switch (err) {
3830 case -NFS4ERR_BADNAME:
3831 err = -ENOENT;
3832 goto out;
3833 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003834 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003835 if (err == -NFS4ERR_MOVED)
3836 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003837 goto out;
3838 case -NFS4ERR_WRONGSEC:
3839 err = -EPERM;
3840 if (client != *clnt)
3841 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003842 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003843 if (IS_ERR(client))
3844 return PTR_ERR(client);
3845
3846 exception.retry = 1;
3847 break;
3848 default:
3849 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3850 }
3851 } while (exception.retry);
3852
3853out:
3854 if (err == 0)
3855 *clnt = client;
3856 else if (client != *clnt)
3857 rpc_shutdown_client(client);
3858
3859 return err;
3860}
3861
Al Virobeffb8f2016-07-20 16:34:42 -04003862static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003863 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3864 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003866 int status;
3867 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003868
David Quigley1775fd32013-05-22 12:50:42 -04003869 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003870 if (client != NFS_CLIENT(dir)) {
3871 rpc_shutdown_client(client);
3872 nfs_fixup_secinfo_attributes(fattr);
3873 }
3874 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875}
3876
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003877struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003878nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003879 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3880{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003881 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003882 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003883
David Quigley1775fd32013-05-22 12:50:42 -04003884 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003885 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003886 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003887 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003888}
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3891{
Trond Myklebust76b32992007-08-10 17:45:11 -04003892 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 struct nfs4_accessargs args = {
3894 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003895 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003897 struct nfs4_accessres res = {
3898 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003899 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 struct rpc_message msg = {
3901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3902 .rpc_argp = &args,
3903 .rpc_resp = &res,
3904 .rpc_cred = entry->cred,
3905 };
3906 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003907 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 /*
3910 * Determine which access bits we want to ask for...
3911 */
3912 if (mode & MAY_READ)
3913 args.access |= NFS4_ACCESS_READ;
3914 if (S_ISDIR(inode->i_mode)) {
3915 if (mode & MAY_WRITE)
3916 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3917 if (mode & MAY_EXEC)
3918 args.access |= NFS4_ACCESS_LOOKUP;
3919 } else {
3920 if (mode & MAY_WRITE)
3921 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3922 if (mode & MAY_EXEC)
3923 args.access |= NFS4_ACCESS_EXECUTE;
3924 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003925
3926 res.fattr = nfs_alloc_fattr();
3927 if (res.fattr == NULL)
3928 return -ENOMEM;
3929
Bryan Schumaker7c513052011-03-24 17:12:24 +00003930 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003932 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003933 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003935 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 return status;
3937}
3938
3939static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3940{
3941 struct nfs4_exception exception = { };
3942 int err;
3943 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003944 err = _nfs4_proc_access(inode, entry);
3945 trace_nfs4_access(inode, err);
3946 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 &exception);
3948 } while (exception.retry);
3949 return err;
3950}
3951
3952/*
3953 * TODO: For the time being, we don't try to get any attributes
3954 * along with any of the zero-copy operations READ, READDIR,
3955 * READLINK, WRITE.
3956 *
3957 * In the case of the first three, we want to put the GETATTR
3958 * after the read-type operation -- this is because it is hard
3959 * to predict the length of a GETATTR response in v4, and thus
3960 * align the READ data correctly. This means that the GETATTR
3961 * may end up partially falling into the page cache, and we should
3962 * shift it into the 'tail' of the xdr_buf before processing.
3963 * To do this efficiently, we need to know the total length
3964 * of data received, which doesn't seem to be available outside
3965 * of the RPC layer.
3966 *
3967 * In the case of WRITE, we also want to put the GETATTR after
3968 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003969 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 *
3971 * Both of these changes to the XDR layer would in fact be quite
3972 * minor, but I decided to leave them for a subsequent patch.
3973 */
3974static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3975 unsigned int pgbase, unsigned int pglen)
3976{
3977 struct nfs4_readlink args = {
3978 .fh = NFS_FH(inode),
3979 .pgbase = pgbase,
3980 .pglen = pglen,
3981 .pages = &page,
3982 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003983 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3986 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003987 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 };
3989
Bryan Schumaker7c513052011-03-24 17:12:24 +00003990 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 -07003991}
3992
3993static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3994 unsigned int pgbase, unsigned int pglen)
3995{
3996 struct nfs4_exception exception = { };
3997 int err;
3998 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003999 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4000 trace_nfs4_readlink(inode, err);
4001 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 &exception);
4003 } while (exception.retry);
4004 return err;
4005}
4006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004008 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010static int
4011nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004012 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004014 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004015 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004016 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 int status = 0;
4019
NeilBrown532d4de2016-10-13 15:26:47 +11004020 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004021 if (IS_ERR(ctx))
4022 return PTR_ERR(ctx);
4023
David Quigleyaa9c2662013-05-22 12:50:44 -04004024 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4025
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004026 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4027 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004028 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 if (IS_ERR(state)) {
4030 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004031 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004034 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004035 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return status;
4037}
4038
Al Virobeffb8f2016-07-20 16:34:42 -04004039static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
Trond Myklebust16e42952005-10-27 22:12:44 -04004041 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004042 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004044 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004046 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004047 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4051 .rpc_argp = &args,
4052 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004054 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004055 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004056
Bryan Schumaker7c513052011-03-24 17:12:24 +00004057 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004058 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004059 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 return status;
4061}
4062
Al Virobeffb8f2016-07-20 16:34:42 -04004063static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
4065 struct nfs4_exception exception = { };
4066 int err;
4067 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004068 err = _nfs4_proc_remove(dir, name);
4069 trace_nfs4_remove(dir, name, err);
4070 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 &exception);
4072 } while (exception.retry);
4073 return err;
4074}
4075
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004076static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004078 struct nfs_server *server = NFS_SERVER(dir);
4079 struct nfs_removeargs *args = msg->rpc_argp;
4080 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004082 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004084 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004085
4086 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087}
4088
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004089static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4090{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004091 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004092 &data->args.seq_args,
4093 &data->res.seq_res,
4094 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095}
4096
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004097static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004099 struct nfs_unlinkdata *data = task->tk_calldata;
4100 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004101
Trond Myklebust14516c32010-07-31 14:29:06 -04004102 if (!nfs4_sequence_done(task, &res->seq_res))
4103 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004104 if (nfs4_async_handle_error(task, res->server, NULL,
4105 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004106 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004107 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004108 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004109 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110}
4111
Jeff Laytond3d41522010-09-17 17:31:57 -04004112static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4113{
4114 struct nfs_server *server = NFS_SERVER(dir);
4115 struct nfs_renameargs *arg = msg->rpc_argp;
4116 struct nfs_renameres *res = msg->rpc_resp;
4117
4118 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004119 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004120 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004121}
4122
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004123static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4124{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004125 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004126 &data->args.seq_args,
4127 &data->res.seq_res,
4128 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004129}
4130
4131static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4132 struct inode *new_dir)
4133{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004134 struct nfs_renamedata *data = task->tk_calldata;
4135 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004136
4137 if (!nfs4_sequence_done(task, &res->seq_res))
4138 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004139 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004140 return 0;
4141
Trond Myklebustc733c492017-01-11 12:32:26 -05004142 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004143 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004144 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004145 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004146 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004147 return 1;
4148}
4149
Al Virobeffb8f2016-07-20 16:34:42 -04004150static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004152 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 struct nfs4_link_arg arg = {
4154 .fh = NFS_FH(inode),
4155 .dir_fh = NFS_FH(dir),
4156 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004157 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004159 struct nfs4_link_res res = {
4160 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004161 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 struct rpc_message msg = {
4164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4165 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004166 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004168 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Trond Myklebust136f2622010-04-16 16:22:49 -04004170 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004171 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004172 goto out;
4173
David Quigley14c43f72013-05-22 12:50:43 -04004174 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4175 if (IS_ERR(res.label)) {
4176 status = PTR_ERR(res.label);
4177 goto out;
4178 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004179 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004180
Bryan Schumaker7c513052011-03-24 17:12:24 +00004181 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004182 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004183 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004184 status = nfs_post_op_update_inode(inode, res.fattr);
4185 if (!status)
4186 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004187 }
David Quigley14c43f72013-05-22 12:50:43 -04004188
4189
4190 nfs4_label_free(res.label);
4191
Trond Myklebust136f2622010-04-16 16:22:49 -04004192out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004193 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 return status;
4195}
4196
Al Virobeffb8f2016-07-20 16:34:42 -04004197static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 struct nfs4_exception exception = { };
4200 int err;
4201 do {
4202 err = nfs4_handle_exception(NFS_SERVER(inode),
4203 _nfs4_proc_link(inode, dir, name),
4204 &exception);
4205 } while (exception.retry);
4206 return err;
4207}
4208
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004209struct nfs4_createdata {
4210 struct rpc_message msg;
4211 struct nfs4_create_arg arg;
4212 struct nfs4_create_res res;
4213 struct nfs_fh fh;
4214 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004215 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216};
4217
4218static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004219 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004220{
4221 struct nfs4_createdata *data;
4222
4223 data = kzalloc(sizeof(*data), GFP_KERNEL);
4224 if (data != NULL) {
4225 struct nfs_server *server = NFS_SERVER(dir);
4226
David Quigley14c43f72013-05-22 12:50:43 -04004227 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4228 if (IS_ERR(data->label))
4229 goto out_free;
4230
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004231 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4232 data->msg.rpc_argp = &data->arg;
4233 data->msg.rpc_resp = &data->res;
4234 data->arg.dir_fh = NFS_FH(dir);
4235 data->arg.server = server;
4236 data->arg.name = name;
4237 data->arg.attrs = sattr;
4238 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004239 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004240 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 data->res.server = server;
4242 data->res.fh = &data->fh;
4243 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004244 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004245 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246 }
4247 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004248out_free:
4249 kfree(data);
4250 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004251}
4252
4253static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4254{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004255 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004256 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004257 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004258 update_changeattr(dir, &data->res.dir_cinfo,
4259 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004260 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004261 }
4262 return status;
4263}
4264
4265static void nfs4_free_createdata(struct nfs4_createdata *data)
4266{
David Quigley14c43f72013-05-22 12:50:43 -04004267 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004268 kfree(data);
4269}
4270
Chuck Lever4f390c12006-08-22 20:06:22 -04004271static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004272 struct page *page, unsigned int len, struct iattr *sattr,
4273 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004275 struct nfs4_createdata *data;
4276 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Chuck Lever94a6d752006-08-22 20:06:23 -04004278 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004279 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004280
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004281 status = -ENOMEM;
4282 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4283 if (data == NULL)
4284 goto out;
4285
4286 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4287 data->arg.u.symlink.pages = &page;
4288 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004289 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004291 status = nfs4_do_create(dir, dentry, data);
4292
4293 nfs4_free_createdata(data);
4294out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return status;
4296}
4297
Chuck Lever4f390c12006-08-22 20:06:22 -04004298static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004299 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004302 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004304
4305 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004308 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4309 trace_nfs4_symlink(dir, &dentry->d_name, err);
4310 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 &exception);
4312 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004313
4314 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return err;
4316}
4317
4318static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004319 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004321 struct nfs4_createdata *data;
4322 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004324 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4325 if (data == NULL)
4326 goto out;
4327
David Quigley1775fd32013-05-22 12:50:42 -04004328 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004329 status = nfs4_do_create(dir, dentry, data);
4330
4331 nfs4_free_createdata(data);
4332out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return status;
4334}
4335
4336static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4337 struct iattr *sattr)
4338{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004339 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004341 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004343
David Quigleyaa9c2662013-05-22 12:50:44 -04004344 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4345
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004346 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4347 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004349 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4350 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4351 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 &exception);
4353 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004354 nfs4_label_release_security(label);
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 return err;
4357}
4358
4359static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004360 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
David Howells2b0143b2015-03-17 22:25:59 +00004362 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 struct nfs4_readdir_arg args = {
4364 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004365 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 .pgbase = 0,
4367 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004368 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004369 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 };
4371 struct nfs4_readdir_res res;
4372 struct rpc_message msg = {
4373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4374 .rpc_argp = &args,
4375 .rpc_resp = &res,
4376 .rpc_cred = cred,
4377 };
4378 int status;
4379
Al Viro6de14722013-09-16 10:53:17 -04004380 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4381 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004382 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004383 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004385 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 -05004386 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004387 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004388 status += args.pgbase;
4389 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004390
4391 nfs_invalidate_atime(dir);
4392
Harvey Harrison3110ff82008-05-02 13:42:44 -07004393 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 return status;
4395}
4396
4397static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004398 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
4400 struct nfs4_exception exception = { };
4401 int err;
4402 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004403 err = _nfs4_proc_readdir(dentry, cred, cookie,
4404 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004405 trace_nfs4_readdir(d_inode(dentry), err);
4406 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 &exception);
4408 } while (exception.retry);
4409 return err;
4410}
4411
4412static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004413 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004415 struct nfs4_createdata *data;
4416 int mode = sattr->ia_mode;
4417 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4420 if (data == NULL)
4421 goto out;
4422
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004424 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004426 data->arg.ftype = NF4BLK;
4427 data->arg.u.device.specdata1 = MAJOR(rdev);
4428 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 }
4430 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004431 data->arg.ftype = NF4CHR;
4432 data->arg.u.device.specdata1 = MAJOR(rdev);
4433 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004434 } else if (!S_ISSOCK(mode)) {
4435 status = -EINVAL;
4436 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 }
David Quigley1775fd32013-05-22 12:50:42 -04004438
David Quigleyaa9c2662013-05-22 12:50:44 -04004439 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004440 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004441out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004442 nfs4_free_createdata(data);
4443out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 return status;
4445}
4446
4447static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4448 struct iattr *sattr, dev_t rdev)
4449{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004450 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004452 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004454
David Quigleyaa9c2662013-05-22 12:50:44 -04004455 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4456
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004457 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4458 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004460 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4461 trace_nfs4_mknod(dir, &dentry->d_name, err);
4462 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 &exception);
4464 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004465
4466 nfs4_label_release_security(label);
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return err;
4469}
4470
4471static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4472 struct nfs_fsstat *fsstat)
4473{
4474 struct nfs4_statfs_arg args = {
4475 .fh = fhandle,
4476 .bitmask = server->attr_bitmask,
4477 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004478 struct nfs4_statfs_res res = {
4479 .fsstat = fsstat,
4480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 struct rpc_message msg = {
4482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4483 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004484 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 };
4486
Trond Myklebust0e574af2005-10-27 22:12:38 -04004487 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004488 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489}
4490
4491static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4492{
4493 struct nfs4_exception exception = { };
4494 int err;
4495 do {
4496 err = nfs4_handle_exception(server,
4497 _nfs4_proc_statfs(server, fhandle, fsstat),
4498 &exception);
4499 } while (exception.retry);
4500 return err;
4501}
4502
4503static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4504 struct nfs_fsinfo *fsinfo)
4505{
4506 struct nfs4_fsinfo_arg args = {
4507 .fh = fhandle,
4508 .bitmask = server->attr_bitmask,
4509 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004510 struct nfs4_fsinfo_res res = {
4511 .fsinfo = fsinfo,
4512 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 struct rpc_message msg = {
4514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4515 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004516 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 };
4518
Bryan Schumaker7c513052011-03-24 17:12:24 +00004519 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520}
4521
4522static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4523{
4524 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004525 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 int err;
4527
4528 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004529 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004530 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004531 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004532 nfs4_set_lease_period(server->nfs_client,
4533 fsinfo->lease_time * HZ,
4534 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004535 break;
4536 }
4537 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 } while (exception.retry);
4539 return err;
4540}
4541
4542static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4543{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004544 int error;
4545
Trond Myklebust0e574af2005-10-27 22:12:38 -04004546 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004547 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004548 if (error == 0) {
4549 /* block layout checks this! */
4550 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004551 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004552 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004553
4554 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555}
4556
4557static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4558 struct nfs_pathconf *pathconf)
4559{
4560 struct nfs4_pathconf_arg args = {
4561 .fh = fhandle,
4562 .bitmask = server->attr_bitmask,
4563 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004564 struct nfs4_pathconf_res res = {
4565 .pathconf = pathconf,
4566 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 struct rpc_message msg = {
4568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4569 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004570 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 };
4572
4573 /* None of the pathconf attributes are mandatory to implement */
4574 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4575 memset(pathconf, 0, sizeof(*pathconf));
4576 return 0;
4577 }
4578
Trond Myklebust0e574af2005-10-27 22:12:38 -04004579 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004580 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581}
4582
4583static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4584 struct nfs_pathconf *pathconf)
4585{
4586 struct nfs4_exception exception = { };
4587 int err;
4588
4589 do {
4590 err = nfs4_handle_exception(server,
4591 _nfs4_proc_pathconf(server, fhandle, pathconf),
4592 &exception);
4593 } while (exception.retry);
4594 return err;
4595}
4596
Trond Myklebust5521abf2013-03-16 20:54:34 -04004597int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004598 const struct nfs_open_context *ctx,
4599 const struct nfs_lock_context *l_ctx,
4600 fmode_t fmode)
4601{
NeilBrown17393472016-10-13 15:26:47 +11004602 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004603}
4604EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4605
Trond Myklebust5521abf2013-03-16 20:54:34 -04004606static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4607 const struct nfs_open_context *ctx,
4608 const struct nfs_lock_context *l_ctx,
4609 fmode_t fmode)
4610{
4611 nfs4_stateid current_stateid;
4612
Trond Myklebuste1253be2014-03-05 08:44:23 -05004613 /* If the current stateid represents a lost lock, then exit */
4614 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4615 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004616 return nfs4_stateid_match(stateid, &current_stateid);
4617}
4618
4619static bool nfs4_error_stateid_expired(int err)
4620{
4621 switch (err) {
4622 case -NFS4ERR_DELEG_REVOKED:
4623 case -NFS4ERR_ADMIN_REVOKED:
4624 case -NFS4ERR_BAD_STATEID:
4625 case -NFS4ERR_STALE_STATEID:
4626 case -NFS4ERR_OLD_STATEID:
4627 case -NFS4ERR_OPENMODE:
4628 case -NFS4ERR_EXPIRED:
4629 return true;
4630 }
4631 return false;
4632}
4633
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004634static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004636 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004638 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004639 if (task->tk_status < 0) {
4640 struct nfs4_exception exception = {
4641 .inode = hdr->inode,
4642 .state = hdr->args.context->state,
4643 .stateid = &hdr->args.stateid,
4644 };
4645 task->tk_status = nfs4_async_handle_exception(task,
4646 server, task->tk_status, &exception);
4647 if (exception.retry) {
4648 rpc_restart_call_prepare(task);
4649 return -EAGAIN;
4650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004654 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
Trond Myklebust5521abf2013-03-16 20:54:34 -04004658static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004659 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004660{
4661
4662 if (!nfs4_error_stateid_expired(task->tk_status) ||
4663 nfs4_stateid_is_current(&args->stateid,
4664 args->context,
4665 args->lock_context,
4666 FMODE_READ))
4667 return false;
4668 rpc_restart_call_prepare(task);
4669 return true;
4670}
4671
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004672static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004673{
4674
4675 dprintk("--> %s\n", __func__);
4676
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004678 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004680 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004681 if (task->tk_status > 0)
4682 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4684 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004685}
4686
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004687static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4688 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004691 if (!hdr->pgio_done_cb)
4692 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004693 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4698 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004699{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004700 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004701 &hdr->args.seq_args,
4702 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004703 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004704 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4706 hdr->args.lock_context,
4707 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004708 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004709 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004710 return -EIO;
4711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004714static int nfs4_write_done_cb(struct rpc_task *task,
4715 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004717 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004718
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004720 if (task->tk_status < 0) {
4721 struct nfs4_exception exception = {
4722 .inode = hdr->inode,
4723 .state = hdr->args.context->state,
4724 .stateid = &hdr->args.stateid,
4725 };
4726 task->tk_status = nfs4_async_handle_exception(task,
4727 NFS_SERVER(inode), task->tk_status,
4728 &exception);
4729 if (exception.retry) {
4730 rpc_restart_call_prepare(task);
4731 return -EAGAIN;
4732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004734 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004735 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004736 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004737 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
Trond Myklebust5521abf2013-03-16 20:54:34 -04004741static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004742 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004743{
4744
4745 if (!nfs4_error_stateid_expired(task->tk_status) ||
4746 nfs4_stateid_is_current(&args->stateid,
4747 args->context,
4748 args->lock_context,
4749 FMODE_WRITE))
4750 return false;
4751 rpc_restart_call_prepare(task);
4752 return true;
4753}
4754
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004755static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004756{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004757 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004758 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004759 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004760 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004761 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4762 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004763}
4764
Trond Myklebust5a37f852012-04-28 14:55:16 -04004765static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004766bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004767{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004768 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004769 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004770 return false;
4771 /* Otherwise, request attributes if and only if we don't hold
4772 * a delegation
4773 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004774 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004775}
Fred Isamana69aef12011-03-03 15:13:47 +00004776
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004777static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4778 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004780 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004781
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004782 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4783 hdr->args.bitmask = NULL;
4784 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004785 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004786 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004787
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004788 if (!hdr->pgio_done_cb)
4789 hdr->pgio_done_cb = nfs4_write_done_cb;
4790 hdr->res.server = server;
4791 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004793 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004794 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796
Fred Isaman0b7c0152012-04-20 14:47:39 -04004797static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4798{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004799 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004800 &data->args.seq_args,
4801 &data->res.seq_res,
4802 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
Fred Isaman0b7c0152012-04-20 14:47:39 -04004805static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004808
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004809 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004810 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4811 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004812 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004813 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
Fred Isaman0b7c0152012-04-20 14:47:39 -04004818static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004819{
4820 if (!nfs4_sequence_done(task, &data->res.seq_res))
4821 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004822 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004823}
4824
Fred Isaman0b7c0152012-04-20 14:47:39 -04004825static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004827 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004828
Fred Isaman0b7c0152012-04-20 14:47:39 -04004829 if (data->commit_done_cb == NULL)
4830 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004831 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004832 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004833 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834}
4835
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004836struct nfs4_renewdata {
4837 struct nfs_client *client;
4838 unsigned long timestamp;
4839};
4840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841/*
4842 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4843 * standalone procedure for queueing an asynchronous RENEW.
4844 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004845static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004846{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004847 struct nfs4_renewdata *data = calldata;
4848 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004849
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004850 if (atomic_read(&clp->cl_count) > 1)
4851 nfs4_schedule_state_renewal(clp);
4852 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004853 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004854}
4855
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004856static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004858 struct nfs4_renewdata *data = calldata;
4859 struct nfs_client *clp = data->client;
4860 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004862 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004863 switch (task->tk_status) {
4864 case 0:
4865 break;
4866 case -NFS4ERR_LEASE_MOVED:
4867 nfs4_schedule_lease_moved_recovery(clp);
4868 break;
4869 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004870 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004871 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4872 return;
4873 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004874 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004875 return;
4876 }
4877 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004879 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880}
4881
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004882static const struct rpc_call_ops nfs4_renew_ops = {
4883 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004884 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004885};
4886
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004887static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
4889 struct rpc_message msg = {
4890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4891 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004892 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004894 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004896 if (renew_flags == 0)
4897 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004898 if (!atomic_inc_not_zero(&clp->cl_count))
4899 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004900 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004901 if (data == NULL)
4902 return -ENOMEM;
4903 data->client = clp;
4904 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004905 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004906 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907}
4908
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004909static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910{
4911 struct rpc_message msg = {
4912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4913 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004914 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 };
4916 unsigned long now = jiffies;
4917 int status;
4918
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004919 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 if (status < 0)
4921 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004922 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 return 0;
4924}
4925
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004926static inline int nfs4_server_supports_acls(struct nfs_server *server)
4927{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004928 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004929}
4930
Trond Myklebust21f498c2012-08-24 10:59:25 -04004931/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4932 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004933 * the stack.
4934 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004935#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004936
Neil Hormane9e3d722011-03-04 19:26:03 -05004937static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004938 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004939{
4940 struct page *newpage, **spages;
4941 int rc = 0;
4942 size_t len;
4943 spages = pages;
4944
4945 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004946 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004947 newpage = alloc_page(GFP_KERNEL);
4948
4949 if (newpage == NULL)
4950 goto unwind;
4951 memcpy(page_address(newpage), buf, len);
4952 buf += len;
4953 buflen -= len;
4954 *pages++ = newpage;
4955 rc++;
4956 } while (buflen != 0);
4957
4958 return rc;
4959
4960unwind:
4961 for(; rc > 0; rc--)
4962 __free_page(spages[rc-1]);
4963 return -ENOMEM;
4964}
4965
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004966struct nfs4_cached_acl {
4967 int cached;
4968 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004969 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004970};
4971
4972static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004973{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004974 struct nfs_inode *nfsi = NFS_I(inode);
4975
4976 spin_lock(&inode->i_lock);
4977 kfree(nfsi->nfs4_acl);
4978 nfsi->nfs4_acl = acl;
4979 spin_unlock(&inode->i_lock);
4980}
4981
4982static void nfs4_zap_acl_attr(struct inode *inode)
4983{
4984 nfs4_set_cached_acl(inode, NULL);
4985}
4986
4987static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4988{
4989 struct nfs_inode *nfsi = NFS_I(inode);
4990 struct nfs4_cached_acl *acl;
4991 int ret = -ENOENT;
4992
4993 spin_lock(&inode->i_lock);
4994 acl = nfsi->nfs4_acl;
4995 if (acl == NULL)
4996 goto out;
4997 if (buf == NULL) /* user is just asking for length */
4998 goto out_len;
4999 if (acl->cached == 0)
5000 goto out;
5001 ret = -ERANGE; /* see getxattr(2) man page */
5002 if (acl->len > buflen)
5003 goto out;
5004 memcpy(buf, acl->data, acl->len);
5005out_len:
5006 ret = acl->len;
5007out:
5008 spin_unlock(&inode->i_lock);
5009 return ret;
5010}
5011
Sachin Prabhu5794d212012-04-17 14:36:40 +01005012static 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 +00005013{
5014 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005015 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005016
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005017 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005018 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005019 if (acl == NULL)
5020 goto out;
5021 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005022 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005023 } else {
5024 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5025 if (acl == NULL)
5026 goto out;
5027 acl->cached = 0;
5028 }
5029 acl->len = acl_len;
5030out:
5031 nfs4_set_cached_acl(inode, acl);
5032}
5033
Andy Adamsonbf118a32011-12-07 11:55:27 -05005034/*
5035 * The getxattr API returns the required buffer length when called with a
5036 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5037 * the required buf. On a NULL buf, we send a page of data to the server
5038 * guessing that the ACL request can be serviced by a page. If so, we cache
5039 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5040 * the cache. If not so, we throw away the page, and cache the required
5041 * length. The next getxattr call will then produce another round trip to
5042 * the server, this time with the input buf of the required size.
5043 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005044static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005046 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005047 struct nfs_getaclargs args = {
5048 .fh = NFS_FH(inode),
5049 .acl_pages = pages,
5050 .acl_len = buflen,
5051 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005052 struct nfs_getaclres res = {
5053 .acl_len = buflen,
5054 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005055 struct rpc_message msg = {
5056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5057 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005058 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005059 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005060 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5061 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005062
Andy Adamsonbf118a32011-12-07 11:55:27 -05005063 /* As long as we're doing a round trip to the server anyway,
5064 * let's be prepared for a page of acl data. */
5065 if (npages == 0)
5066 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005067 if (npages > ARRAY_SIZE(pages))
5068 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005069
Andy Adamsonbf118a32011-12-07 11:55:27 -05005070 for (i = 0; i < npages; i++) {
5071 pages[i] = alloc_page(GFP_KERNEL);
5072 if (!pages[i])
5073 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005074 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005075
5076 /* for decoding across pages */
5077 res.acl_scratch = alloc_page(GFP_KERNEL);
5078 if (!res.acl_scratch)
5079 goto out_free;
5080
Andy Adamsonbf118a32011-12-07 11:55:27 -05005081 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005082
Peng Taode040be2012-01-10 22:42:47 +08005083 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005084 __func__, buf, buflen, npages, args.acl_len);
5085 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5086 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005087 if (ret)
5088 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005089
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005090 /* Handle the case where the passed-in buffer is too short */
5091 if (res.acl_flags & NFS4_ACL_TRUNC) {
5092 /* Did the user only issue a request for the acl length? */
5093 if (buf == NULL)
5094 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005095 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005096 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005097 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005098 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005099 if (buf) {
5100 if (res.acl_len > buflen) {
5101 ret = -ERANGE;
5102 goto out_free;
5103 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005104 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005105 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005106out_ok:
5107 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005108out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005109 for (i = 0; i < npages; i++)
5110 if (pages[i])
5111 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005112 if (res.acl_scratch)
5113 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005114 return ret;
5115}
5116
Trond Myklebust16b42892006-08-24 12:27:15 -04005117static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5118{
5119 struct nfs4_exception exception = { };
5120 ssize_t ret;
5121 do {
5122 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005123 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005124 if (ret >= 0)
5125 break;
5126 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5127 } while (exception.retry);
5128 return ret;
5129}
5130
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005131static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5132{
5133 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005134 int ret;
5135
5136 if (!nfs4_server_supports_acls(server))
5137 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005138 ret = nfs_revalidate_inode(server, inode);
5139 if (ret < 0)
5140 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005141 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5142 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005143 ret = nfs4_read_cached_acl(inode, buf, buflen);
5144 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005145 /* -ENOENT is returned if there is no ACL or if there is an ACL
5146 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005147 return ret;
5148 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005149}
5150
Trond Myklebust16b42892006-08-24 12:27:15 -04005151static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005152{
5153 struct nfs_server *server = NFS_SERVER(inode);
5154 struct page *pages[NFS4ACL_MAXPAGES];
5155 struct nfs_setaclargs arg = {
5156 .fh = NFS_FH(inode),
5157 .acl_pages = pages,
5158 .acl_len = buflen,
5159 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005160 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005161 struct rpc_message msg = {
5162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5163 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005164 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005165 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005166 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005167 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005168
5169 if (!nfs4_server_supports_acls(server))
5170 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005171 if (npages > ARRAY_SIZE(pages))
5172 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005173 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005174 if (i < 0)
5175 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005176 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005177 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005178
5179 /*
5180 * Free each page after tx, so the only ref left is
5181 * held by the network stack
5182 */
5183 for (; i > 0; i--)
5184 put_page(pages[i-1]);
5185
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005186 /*
5187 * Acl update can result in inode attribute update.
5188 * so mark the attribute cache invalid.
5189 */
5190 spin_lock(&inode->i_lock);
5191 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5192 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005193 nfs_access_zap_cache(inode);
5194 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005195 return ret;
5196}
5197
Trond Myklebust16b42892006-08-24 12:27:15 -04005198static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5199{
5200 struct nfs4_exception exception = { };
5201 int err;
5202 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005203 err = __nfs4_proc_set_acl(inode, buf, buflen);
5204 trace_nfs4_set_acl(inode, err);
5205 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005206 &exception);
5207 } while (exception.retry);
5208 return err;
5209}
5210
David Quigleyaa9c2662013-05-22 12:50:44 -04005211#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5212static int _nfs4_get_security_label(struct inode *inode, void *buf,
5213 size_t buflen)
5214{
5215 struct nfs_server *server = NFS_SERVER(inode);
5216 struct nfs_fattr fattr;
5217 struct nfs4_label label = {0, 0, buflen, buf};
5218
5219 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005220 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005221 .fh = NFS_FH(inode),
5222 .bitmask = bitmask,
5223 };
5224 struct nfs4_getattr_res res = {
5225 .fattr = &fattr,
5226 .label = &label,
5227 .server = server,
5228 };
5229 struct rpc_message msg = {
5230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005231 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005232 .rpc_resp = &res,
5233 };
5234 int ret;
5235
5236 nfs_fattr_init(&fattr);
5237
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005238 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005239 if (ret)
5240 return ret;
5241 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5242 return -ENOENT;
5243 if (buflen < label.len)
5244 return -ERANGE;
5245 return 0;
5246}
5247
5248static int nfs4_get_security_label(struct inode *inode, void *buf,
5249 size_t buflen)
5250{
5251 struct nfs4_exception exception = { };
5252 int err;
5253
5254 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5255 return -EOPNOTSUPP;
5256
5257 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005258 err = _nfs4_get_security_label(inode, buf, buflen);
5259 trace_nfs4_get_security_label(inode, err);
5260 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005261 &exception);
5262 } while (exception.retry);
5263 return err;
5264}
5265
5266static int _nfs4_do_set_security_label(struct inode *inode,
5267 struct nfs4_label *ilabel,
5268 struct nfs_fattr *fattr,
5269 struct nfs4_label *olabel)
5270{
5271
5272 struct iattr sattr = {0};
5273 struct nfs_server *server = NFS_SERVER(inode);
5274 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005275 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005276 .fh = NFS_FH(inode),
5277 .iap = &sattr,
5278 .server = server,
5279 .bitmask = bitmask,
5280 .label = ilabel,
5281 };
5282 struct nfs_setattrres res = {
5283 .fattr = fattr,
5284 .label = olabel,
5285 .server = server,
5286 };
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005289 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005290 .rpc_resp = &res,
5291 };
5292 int status;
5293
Jeff Layton12207f62013-11-01 10:49:32 -04005294 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005295
Jeff Layton12207f62013-11-01 10:49:32 -04005296 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005297 if (status)
5298 dprintk("%s failed: %d\n", __func__, status);
5299
5300 return status;
5301}
5302
5303static int nfs4_do_set_security_label(struct inode *inode,
5304 struct nfs4_label *ilabel,
5305 struct nfs_fattr *fattr,
5306 struct nfs4_label *olabel)
5307{
5308 struct nfs4_exception exception = { };
5309 int err;
5310
5311 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005312 err = _nfs4_do_set_security_label(inode, ilabel,
5313 fattr, olabel);
5314 trace_nfs4_set_security_label(inode, err);
5315 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005316 &exception);
5317 } while (exception.retry);
5318 return err;
5319}
5320
5321static int
Al Viro59301222016-05-27 10:19:30 -04005322nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005323{
5324 struct nfs4_label ilabel, *olabel = NULL;
5325 struct nfs_fattr fattr;
5326 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005327 int status;
5328
5329 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5330 return -EOPNOTSUPP;
5331
5332 nfs_fattr_init(&fattr);
5333
5334 ilabel.pi = 0;
5335 ilabel.lfs = 0;
5336 ilabel.label = (char *)buf;
5337 ilabel.len = buflen;
5338
5339 cred = rpc_lookup_cred();
5340 if (IS_ERR(cred))
5341 return PTR_ERR(cred);
5342
5343 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5344 if (IS_ERR(olabel)) {
5345 status = -PTR_ERR(olabel);
5346 goto out;
5347 }
5348
5349 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5350 if (status == 0)
5351 nfs_setsecurity(inode, &fattr, olabel);
5352
5353 nfs4_label_free(olabel);
5354out:
5355 put_rpccred(cred);
5356 return status;
5357}
5358#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5359
5360
Chuck Leverf0920752012-05-21 22:45:41 -04005361static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5362 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005363{
5364 __be32 verf[2];
5365
Chuck Lever2c820d92012-05-21 22:45:33 -04005366 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5367 /* An impossible timestamp guarantees this value
5368 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005369 verf[0] = cpu_to_be32(U32_MAX);
5370 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005371 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005372 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005373 u64 ns = ktime_to_ns(nn->boot_time);
5374
5375 verf[0] = cpu_to_be32(ns >> 32);
5376 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005377 }
Chuck Levercd937102012-03-02 17:14:31 -05005378 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5379}
5380
Jeff Laytona3192682015-06-09 19:43:59 -04005381static int
5382nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005383{
Jeff Laytona3192682015-06-09 19:43:59 -04005384 size_t len;
5385 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005386
Trond Myklebustceb3a162015-01-03 15:16:04 -05005387 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005388 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005389
Jeff Laytona3192682015-06-09 19:43:59 -04005390 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005391 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005392 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5393 1 +
5394 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5395 1;
5396 rcu_read_unlock();
5397
5398 if (len > NFS4_OPAQUE_LIMIT + 1)
5399 return -EINVAL;
5400
5401 /*
5402 * Since this string is allocated at mount time, and held until the
5403 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5404 * about a memory-reclaim deadlock.
5405 */
5406 str = kmalloc(len, GFP_KERNEL);
5407 if (!str)
5408 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005409
Chuck Levere984a552012-09-14 17:24:21 -04005410 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005411 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005412 clp->cl_ipaddr,
5413 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5414 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005415 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005416
Jeff Laytona3192682015-06-09 19:43:59 -04005417 clp->cl_owner_id = str;
5418 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005419}
5420
Jeff Layton873e3852015-06-09 19:44:00 -04005421static int
5422nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005423{
Jeff Layton873e3852015-06-09 19:44:00 -04005424 size_t len;
5425 char *str;
5426
5427 len = 10 + 10 + 1 + 10 + 1 +
5428 strlen(nfs4_client_id_uniquifier) + 1 +
5429 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5430
5431 if (len > NFS4_OPAQUE_LIMIT + 1)
5432 return -EINVAL;
5433
5434 /*
5435 * Since this string is allocated at mount time, and held until the
5436 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5437 * about a memory-reclaim deadlock.
5438 */
5439 str = kmalloc(len, GFP_KERNEL);
5440 if (!str)
5441 return -ENOMEM;
5442
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005443 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005444 clp->rpc_ops->version, clp->cl_minorversion,
5445 nfs4_client_id_uniquifier,
5446 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005447 clp->cl_owner_id = str;
5448 return 0;
5449}
5450
5451static int
5452nfs4_init_uniform_client_string(struct nfs_client *clp)
5453{
Jeff Layton873e3852015-06-09 19:44:00 -04005454 size_t len;
5455 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005456
5457 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005458 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005459
5460 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005461 return nfs4_init_uniquifier_client_string(clp);
5462
5463 len = 10 + 10 + 1 + 10 + 1 +
5464 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5465
5466 if (len > NFS4_OPAQUE_LIMIT + 1)
5467 return -EINVAL;
5468
5469 /*
5470 * Since this string is allocated at mount time, and held until the
5471 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5472 * about a memory-reclaim deadlock.
5473 */
5474 str = kmalloc(len, GFP_KERNEL);
5475 if (!str)
5476 return -ENOMEM;
5477
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005478 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005479 clp->rpc_ops->version, clp->cl_minorversion,
5480 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005481 clp->cl_owner_id = str;
5482 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005483}
5484
Chuck Lever706cb8d2014-03-12 12:51:47 -04005485/*
5486 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5487 * services. Advertise one based on the address family of the
5488 * clientaddr.
5489 */
5490static unsigned int
5491nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5492{
5493 if (strchr(clp->cl_ipaddr, ':') != NULL)
5494 return scnprintf(buf, len, "tcp6");
5495 else
5496 return scnprintf(buf, len, "tcp");
5497}
5498
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005499static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5500{
5501 struct nfs4_setclientid *sc = calldata;
5502
5503 if (task->tk_status == 0)
5504 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5505}
5506
5507static const struct rpc_call_ops nfs4_setclientid_ops = {
5508 .rpc_call_done = nfs4_setclientid_done,
5509};
5510
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005511/**
5512 * nfs4_proc_setclientid - Negotiate client ID
5513 * @clp: state data structure
5514 * @program: RPC program for NFSv4 callback service
5515 * @port: IP port number for NFS4 callback service
5516 * @cred: RPC credential to use for this call
5517 * @res: where to place the result
5518 *
5519 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5520 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005521int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5522 unsigned short port, struct rpc_cred *cred,
5523 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
5525 nfs4_verifier sc_verifier;
5526 struct nfs4_setclientid setclientid = {
5527 .sc_verifier = &sc_verifier,
5528 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005529 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 };
5531 struct rpc_message msg = {
5532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5533 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005534 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005535 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005537 struct rpc_task *task;
5538 struct rpc_task_setup task_setup_data = {
5539 .rpc_client = clp->cl_rpcclient,
5540 .rpc_message = &msg,
5541 .callback_ops = &nfs4_setclientid_ops,
5542 .callback_data = &setclientid,
5543 .flags = RPC_TASK_TIMEOUT,
5544 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005545 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
Chuck Leverde734832012-07-11 16:30:50 -04005547 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005548 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005549
5550 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5551 status = nfs4_init_uniform_client_string(clp);
5552 else
Jeff Laytona3192682015-06-09 19:43:59 -04005553 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005554
5555 if (status)
5556 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005557
Chuck Leverde734832012-07-11 16:30:50 -04005558 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005559 setclientid.sc_netid_len =
5560 nfs4_init_callback_netid(clp,
5561 setclientid.sc_netid,
5562 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005563 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005564 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 clp->cl_ipaddr, port >> 8, port & 255);
5566
Jeff Layton3a6bb732015-06-09 19:43:57 -04005567 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005568 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005569 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005570 task = rpc_run_task(&task_setup_data);
5571 if (IS_ERR(task)) {
5572 status = PTR_ERR(task);
5573 goto out;
5574 }
5575 status = task->tk_status;
5576 if (setclientid.sc_cred) {
5577 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5578 put_rpccred(setclientid.sc_cred);
5579 }
5580 rpc_put_task(task);
5581out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005582 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005583 dprintk("NFS reply setclientid: %d\n", status);
5584 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585}
5586
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005587/**
5588 * nfs4_proc_setclientid_confirm - Confirm client ID
5589 * @clp: state data structure
5590 * @res: result of a previous SETCLIENTID
5591 * @cred: RPC credential to use for this call
5592 *
5593 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5594 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005595int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005596 struct nfs4_setclientid_res *arg,
5597 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 struct rpc_message msg = {
5600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005601 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005602 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 int status;
5605
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005606 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5607 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5608 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005609 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005610 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005611 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 return status;
5613}
5614
Trond Myklebustfe650402006-01-03 09:55:18 +01005615struct nfs4_delegreturndata {
5616 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005617 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005618 struct nfs_fh fh;
5619 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005620 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005621 struct {
5622 struct nfs4_layoutreturn_args arg;
5623 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005624 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005625 u32 roc_barrier;
5626 bool roc;
5627 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005628 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005629 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005630 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005631};
5632
Trond Myklebustfe650402006-01-03 09:55:18 +01005633static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5634{
5635 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005636
Trond Myklebust14516c32010-07-31 14:29:06 -04005637 if (!nfs4_sequence_done(task, &data->res.seq_res))
5638 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005639
Trond Myklebustca8acf82013-08-13 10:36:56 -04005640 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005641
5642 /* Handle Layoutreturn errors */
5643 if (data->args.lr_args && task->tk_status != 0) {
5644 switch(data->res.lr_ret) {
5645 default:
5646 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5647 break;
5648 case 0:
5649 data->args.lr_args = NULL;
5650 data->res.lr_res = NULL;
5651 break;
5652 case -NFS4ERR_ADMIN_REVOKED:
5653 case -NFS4ERR_DELEG_REVOKED:
5654 case -NFS4ERR_EXPIRED:
5655 case -NFS4ERR_BAD_STATEID:
5656 case -NFS4ERR_OLD_STATEID:
5657 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5658 case -NFS4ERR_WRONG_CRED:
5659 data->args.lr_args = NULL;
5660 data->res.lr_res = NULL;
5661 data->res.lr_ret = 0;
5662 rpc_restart_call_prepare(task);
5663 return;
5664 }
5665 }
5666
Ricardo Labiaga79708862009-12-07 09:23:21 -05005667 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005668 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005669 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005670 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005671 case -NFS4ERR_ADMIN_REVOKED:
5672 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005673 case -NFS4ERR_EXPIRED:
5674 nfs4_free_revoked_stateid(data->res.server,
5675 data->args.stateid,
5676 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005677 case -NFS4ERR_BAD_STATEID:
5678 case -NFS4ERR_OLD_STATEID:
5679 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005680 task->tk_status = 0;
5681 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005682 case -NFS4ERR_ACCESS:
5683 if (data->args.bitmask) {
5684 data->args.bitmask = NULL;
5685 data->res.fattr = NULL;
5686 task->tk_status = 0;
5687 rpc_restart_call_prepare(task);
5688 return;
5689 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005690 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005691 if (nfs4_async_handle_error(task, data->res.server,
5692 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005693 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005694 return;
5695 }
5696 }
5697 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005698}
5699
5700static void nfs4_delegreturn_release(void *calldata)
5701{
Peng Tao039b7562014-07-03 13:05:02 +08005702 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005703 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005704
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005705 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005706 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005707 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5708 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005709 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005710 nfs_iput_and_deactive(inode);
5711 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005712 kfree(calldata);
5713}
5714
Andy Adamson938e1012009-04-01 09:22:28 -04005715static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5716{
5717 struct nfs4_delegreturndata *d_data;
5718
5719 d_data = (struct nfs4_delegreturndata *)data;
5720
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005721 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005722 return;
5723
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005724 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005725 &d_data->args.seq_args,
5726 &d_data->res.seq_res,
5727 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005728}
Andy Adamson938e1012009-04-01 09:22:28 -04005729
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005730static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005731 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005732 .rpc_call_done = nfs4_delegreturn_done,
5733 .rpc_release = nfs4_delegreturn_release,
5734};
5735
Trond Myklebuste6f81072008-01-24 18:14:34 -05005736static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005737{
5738 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005739 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005740 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005741 struct rpc_message msg = {
5742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5743 .rpc_cred = cred,
5744 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005745 struct rpc_task_setup task_setup_data = {
5746 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005747 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005748 .callback_ops = &nfs4_delegreturn_ops,
5749 .flags = RPC_TASK_ASYNC,
5750 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005751 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005752
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005753 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005754 if (data == NULL)
5755 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005756 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005757
5758 nfs4_state_protect(server->nfs_client,
5759 NFS_SP4_MACH_CRED_CLEANUP,
5760 &task_setup_data.rpc_client, &msg);
5761
Trond Myklebustfe650402006-01-03 09:55:18 +01005762 data->args.fhandle = &data->fh;
5763 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005764 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005765 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005766 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005767 data->res.fattr = &data->fattr;
5768 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005769 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005770 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005771 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005772 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005773 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005774 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005775 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005776 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005777 if (data->lr.roc) {
5778 data->args.lr_args = &data->lr.arg;
5779 data->res.lr_res = &data->lr.res;
5780 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005781 } else if (data->lr.roc) {
5782 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5783 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005784 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005785
Trond Myklebustc970aa82007-07-14 15:39:59 -04005786 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005787 msg.rpc_argp = &data->args;
5788 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005789 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005790 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005791 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005792 if (!issync)
5793 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005794 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005795 if (status != 0)
5796 goto out;
5797 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005798out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005799 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005800 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801}
5802
Trond Myklebuste6f81072008-01-24 18:14:34 -05005803int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804{
5805 struct nfs_server *server = NFS_SERVER(inode);
5806 struct nfs4_exception exception = { };
5807 int err;
5808 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005809 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005810 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 switch (err) {
5812 case -NFS4ERR_STALE_STATEID:
5813 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 case 0:
5815 return 0;
5816 }
5817 err = nfs4_handle_exception(server, err, &exception);
5818 } while (exception.retry);
5819 return err;
5820}
5821
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5823{
5824 struct inode *inode = state->inode;
5825 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005826 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005827 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005829 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005831 struct nfs_lockt_res res = {
5832 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 };
5834 struct rpc_message msg = {
5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5836 .rpc_argp = &arg,
5837 .rpc_resp = &res,
5838 .rpc_cred = state->owner->so_cred,
5839 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 struct nfs4_lock_state *lsp;
5841 int status;
5842
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005843 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005844 status = nfs4_set_lock_state(state, request);
5845 if (status != 0)
5846 goto out;
5847 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005848 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005849 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005850 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005851 switch (status) {
5852 case 0:
5853 request->fl_type = F_UNLCK;
5854 break;
5855 case -NFS4ERR_DENIED:
5856 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005858 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005859 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005860out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 return status;
5862}
5863
5864static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5865{
5866 struct nfs4_exception exception = { };
5867 int err;
5868
5869 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005870 err = _nfs4_proc_getlk(state, cmd, request);
5871 trace_nfs4_get_lock(request, state, cmd, err);
5872 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 &exception);
5874 } while (exception.retry);
5875 return err;
5876}
5877
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005878struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005879 struct nfs_locku_args arg;
5880 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005881 struct nfs4_lock_state *lsp;
5882 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005883 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005884 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005885 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005886};
5887
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005888static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5889 struct nfs_open_context *ctx,
5890 struct nfs4_lock_state *lsp,
5891 struct nfs_seqid *seqid)
5892{
5893 struct nfs4_unlockdata *p;
5894 struct inode *inode = lsp->ls_state->inode;
5895
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005896 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005897 if (p == NULL)
5898 return NULL;
5899 p->arg.fh = NFS_FH(inode);
5900 p->arg.fl = &p->fl;
5901 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005902 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005903 p->lsp = lsp;
5904 atomic_inc(&lsp->ls_count);
5905 /* Ensure we don't close file until we're done freeing locks! */
5906 p->ctx = get_nfs_open_context(ctx);
5907 memcpy(&p->fl, fl, sizeof(p->fl));
5908 p->server = NFS_SERVER(inode);
5909 return p;
5910}
5911
Trond Myklebust06f814a2006-01-03 09:55:07 +01005912static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005913{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005914 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005915 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005916 nfs4_put_lock_state(calldata->lsp);
5917 put_nfs_open_context(calldata->ctx);
5918 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005919}
5920
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005921static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005922{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005923 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005924
Trond Myklebust14516c32010-07-31 14:29:06 -04005925 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5926 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005927 switch (task->tk_status) {
5928 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005929 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005930 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005931 if (nfs4_update_lock_stateid(calldata->lsp,
5932 &calldata->res.stateid))
5933 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005934 case -NFS4ERR_ADMIN_REVOKED:
5935 case -NFS4ERR_EXPIRED:
5936 nfs4_free_revoked_stateid(calldata->server,
5937 &calldata->arg.stateid,
5938 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005939 case -NFS4ERR_BAD_STATEID:
5940 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005941 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005942 if (!nfs4_stateid_match(&calldata->arg.stateid,
5943 &calldata->lsp->ls_stateid))
5944 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005945 break;
5946 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005947 if (nfs4_async_handle_error(task, calldata->server,
5948 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005949 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005950 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005951 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005952}
5953
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005954static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005955{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005956 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005958 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005959 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005960 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005961 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005962 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005963 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005964 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005965 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005966 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005967 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005968 &calldata->res.seq_res,
5969 task) != 0)
5970 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005971 return;
5972out_no_action:
5973 task->tk_action = NULL;
5974out_wait:
5975 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976}
5977
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005978static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005979 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005980 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005981 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005982};
5983
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005984static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5985 struct nfs_open_context *ctx,
5986 struct nfs4_lock_state *lsp,
5987 struct nfs_seqid *seqid)
5988{
5989 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005990 struct rpc_message msg = {
5991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5992 .rpc_cred = ctx->cred,
5993 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005994 struct rpc_task_setup task_setup_data = {
5995 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005996 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005997 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005998 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005999 .flags = RPC_TASK_ASYNC,
6000 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006001
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006002 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6003 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6004
Frank Filz137d6ac2007-07-09 15:32:29 -07006005 /* Ensure this is an unlock - when canceling a lock, the
6006 * canceled lock is passed in, and it won't be an unlock.
6007 */
6008 fl->fl_type = F_UNLCK;
6009
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6011 if (data == NULL) {
6012 nfs_free_seqid(seqid);
6013 return ERR_PTR(-ENOMEM);
6014 }
6015
Chuck Levera9c92d62013-08-09 12:48:18 -04006016 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006017 msg.rpc_argp = &data->arg;
6018 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006019 task_setup_data.callback_data = data;
6020 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006021}
6022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6024{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006025 struct inode *inode = state->inode;
6026 struct nfs4_state_owner *sp = state->owner;
6027 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006028 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006029 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006030 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006031 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006032 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006033 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Trond Myklebust9b073572006-06-29 16:38:34 -04006035 status = nfs4_set_lock_state(state, request);
6036 /* Unlock _before_ we do the RPC call */
6037 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006038 /* Exclude nfs_delegation_claim_locks() */
6039 mutex_lock(&sp->so_delegreturn_mutex);
6040 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006041 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006042 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006043 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006044 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006045 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006046 }
6047 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006048 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006049 if (status != 0)
6050 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006051 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006052 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006053 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6054 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006055 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6056 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006057 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006058 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006059 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006060 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006061 status = PTR_ERR(task);
6062 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006063 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006065 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006066out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006067 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006068 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006069 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070}
6071
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006072struct nfs4_lockdata {
6073 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006074 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006075 struct nfs4_lock_state *lsp;
6076 struct nfs_open_context *ctx;
6077 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006078 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006079 int rpc_status;
6080 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006081 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006082};
6083
6084static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006085 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6086 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087{
6088 struct nfs4_lockdata *p;
6089 struct inode *inode = lsp->ls_state->inode;
6090 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006091 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006092
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006093 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094 if (p == NULL)
6095 return NULL;
6096
6097 p->arg.fh = NFS_FH(inode);
6098 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006099 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006100 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006101 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006102 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6103 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006104 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006105 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006106 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006107 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006108 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006109 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006110 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006111 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006112 atomic_inc(&lsp->ls_count);
6113 p->ctx = get_nfs_open_context(ctx);
6114 memcpy(&p->fl, fl, sizeof(p->fl));
6115 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006116out_free_seqid:
6117 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006118out_free:
6119 kfree(p);
6120 return NULL;
6121}
6122
6123static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6124{
6125 struct nfs4_lockdata *data = calldata;
6126 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Harvey Harrison3110ff82008-05-02 13:42:44 -07006128 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006129 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006130 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006131 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006132 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006133 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006134 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006135 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006136 nfs4_stateid_copy(&data->arg.open_stateid,
6137 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006139 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006140 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006141 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006142 nfs4_stateid_copy(&data->arg.lock_stateid,
6143 &data->lsp->ls_stateid);
6144 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006145 if (!nfs4_valid_open_stateid(state)) {
6146 data->rpc_status = -EBADF;
6147 task->tk_action = NULL;
6148 goto out_release_open_seqid;
6149 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006150 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006151 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168a2010-06-16 09:52:26 -04006152 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006153 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006154 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006155 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006156out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006157 nfs_release_seqid(data->arg.open_seqid);
6158out_release_lock_seqid:
6159 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006160out_wait:
6161 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006162 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006163}
6164
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006165static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6166{
6167 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006168 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006169
Harvey Harrison3110ff82008-05-02 13:42:44 -07006170 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006171
Trond Myklebust14516c32010-07-31 14:29:06 -04006172 if (!nfs4_sequence_done(task, &data->res.seq_res))
6173 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006174
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006175 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006176 switch (task->tk_status) {
6177 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006178 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006179 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006180 if (data->arg.new_lock) {
6181 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006182 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006183 rpc_restart_call_prepare(task);
6184 break;
6185 }
6186 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006187 if (data->arg.new_lock_owner != 0) {
6188 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6189 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6190 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6191 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6192 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006193 break;
6194 case -NFS4ERR_BAD_STATEID:
6195 case -NFS4ERR_OLD_STATEID:
6196 case -NFS4ERR_STALE_STATEID:
6197 case -NFS4ERR_EXPIRED:
6198 if (data->arg.new_lock_owner != 0) {
6199 if (!nfs4_stateid_match(&data->arg.open_stateid,
6200 &lsp->ls_state->open_stateid))
6201 rpc_restart_call_prepare(task);
6202 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6203 &lsp->ls_stateid))
6204 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006206 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006207}
6208
6209static void nfs4_lock_release(void *calldata)
6210{
6211 struct nfs4_lockdata *data = calldata;
6212
Harvey Harrison3110ff82008-05-02 13:42:44 -07006213 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006214 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006215 if (data->cancelled != 0) {
6216 struct rpc_task *task;
6217 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6218 data->arg.lock_seqid);
6219 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006220 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006221 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 } else
6223 nfs_free_seqid(data->arg.lock_seqid);
6224 nfs4_put_lock_state(data->lsp);
6225 put_nfs_open_context(data->ctx);
6226 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006227 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006228}
6229
6230static const struct rpc_call_ops nfs4_lock_ops = {
6231 .rpc_call_prepare = nfs4_lock_prepare,
6232 .rpc_call_done = nfs4_lock_done,
6233 .rpc_release = nfs4_lock_release,
6234};
6235
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006236static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6237{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006238 switch (error) {
6239 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006240 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006241 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006242 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006243 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006244 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006245 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006246 break;
6247 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006248 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006249 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006250 };
6251}
6252
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006253static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006254{
6255 struct nfs4_lockdata *data;
6256 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006257 struct rpc_message msg = {
6258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6259 .rpc_cred = state->owner->so_cred,
6260 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006261 struct rpc_task_setup task_setup_data = {
6262 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006263 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006264 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006265 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006266 .flags = RPC_TASK_ASYNC,
6267 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006268 int ret;
6269
Harvey Harrison3110ff82008-05-02 13:42:44 -07006270 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006271 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006272 fl->fl_u.nfs4_fl.owner,
6273 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006274 if (data == NULL)
6275 return -ENOMEM;
6276 if (IS_SETLKW(cmd))
6277 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006278 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006279 msg.rpc_argp = &data->arg;
6280 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006281 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006282 if (recovery_type > NFS_LOCK_NEW) {
6283 if (recovery_type == NFS_LOCK_RECLAIM)
6284 data->arg.reclaim = NFS_LOCK_RECLAIM;
6285 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006286 } else
6287 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006288 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006289 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006290 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006291 ret = nfs4_wait_for_completion_rpc_task(task);
6292 if (ret == 0) {
6293 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006294 if (ret)
6295 nfs4_handle_setlk_error(data->server, data->lsp,
6296 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006297 } else
6298 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006299 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006300 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006301 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303}
6304
6305static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6306{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006307 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006308 struct nfs4_exception exception = {
6309 .inode = state->inode,
6310 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006311 int err;
6312
6313 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006314 /* Cache the lock if possible... */
6315 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6316 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006317 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006318 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006319 break;
6320 nfs4_handle_exception(server, err, &exception);
6321 } while (exception.retry);
6322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323}
6324
6325static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6326{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006327 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006328 struct nfs4_exception exception = {
6329 .inode = state->inode,
6330 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006331 int err;
6332
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006333 err = nfs4_set_lock_state(state, request);
6334 if (err != 0)
6335 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006336 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006337 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6338 return 0;
6339 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006340 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006341 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6342 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006343 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006344 switch (err) {
6345 default:
6346 goto out;
6347 case -NFS4ERR_GRACE:
6348 case -NFS4ERR_DELAY:
6349 nfs4_handle_exception(server, err, &exception);
6350 err = 0;
6351 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006352 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006353out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006354 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355}
6356
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006357#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006358static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6359{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006360 struct nfs4_lock_state *lsp;
6361 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006362
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006363 status = nfs4_set_lock_state(state, request);
6364 if (status != 0)
6365 return status;
6366 lsp = request->fl_u.nfs4_fl.owner;
6367 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6368 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6369 return 0;
6370 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006371 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006372}
6373#endif
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6376{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006377 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006378 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006379 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006380 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
Trond Myklebust01c3b862006-06-29 16:38:39 -04006382 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006383 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006384 if (status < 0)
6385 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006386 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006387 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006388 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006389 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006390 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006391 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006392 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006393 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006394 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006395 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006396 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006397 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006398 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006399 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006400out:
6401 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 return status;
6403}
6404
6405static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6406{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006407 struct nfs4_exception exception = {
6408 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006409 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006410 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 int err;
6412
6413 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006414 err = _nfs4_proc_setlk(state, cmd, request);
6415 if (err == -NFS4ERR_DENIED)
6416 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006418 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 } while (exception.retry);
6420 return err;
6421}
6422
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006423#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6424#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6425
6426static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006427nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6428 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006429{
6430 int status = -ERESTARTSYS;
6431 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6432
6433 while(!signalled()) {
6434 status = nfs4_proc_setlk(state, cmd, request);
6435 if ((status != -EAGAIN) || IS_SETLK(cmd))
6436 break;
6437 freezable_schedule_timeout_interruptible(timeout);
6438 timeout *= 2;
6439 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6440 status = -ERESTARTSYS;
6441 }
6442 return status;
6443}
6444
Jeff Laytona1d617d82016-09-17 18:17:39 -04006445#ifdef CONFIG_NFS_V4_1
6446struct nfs4_lock_waiter {
6447 struct task_struct *task;
6448 struct inode *inode;
6449 struct nfs_lowner *owner;
6450 bool notified;
6451};
6452
6453static int
6454nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6455{
6456 int ret;
6457 struct cb_notify_lock_args *cbnl = key;
6458 struct nfs4_lock_waiter *waiter = wait->private;
6459 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6460 *wowner = waiter->owner;
6461
6462 /* Only wake if the callback was for the same owner */
6463 if (lowner->clientid != wowner->clientid ||
6464 lowner->id != wowner->id ||
6465 lowner->s_dev != wowner->s_dev)
6466 return 0;
6467
6468 /* Make sure it's for the right inode */
6469 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6470 return 0;
6471
6472 waiter->notified = true;
6473
6474 /* override "private" so we can use default_wake_function */
6475 wait->private = waiter->task;
6476 ret = autoremove_wake_function(wait, mode, flags, key);
6477 wait->private = waiter;
6478 return ret;
6479}
6480
6481static int
6482nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6483{
6484 int status = -ERESTARTSYS;
6485 unsigned long flags;
6486 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6487 struct nfs_server *server = NFS_SERVER(state->inode);
6488 struct nfs_client *clp = server->nfs_client;
6489 wait_queue_head_t *q = &clp->cl_lock_waitq;
6490 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6491 .id = lsp->ls_seqid.owner_id,
6492 .s_dev = server->s_dev };
6493 struct nfs4_lock_waiter waiter = { .task = current,
6494 .inode = state->inode,
6495 .owner = &owner,
6496 .notified = false };
6497 wait_queue_t wait;
6498
6499 /* Don't bother with waitqueue if we don't expect a callback */
6500 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6501 return nfs4_retry_setlk_simple(state, cmd, request);
6502
6503 init_wait(&wait);
6504 wait.private = &waiter;
6505 wait.func = nfs4_wake_lock_waiter;
6506 add_wait_queue(q, &wait);
6507
6508 while(!signalled()) {
6509 status = nfs4_proc_setlk(state, cmd, request);
6510 if ((status != -EAGAIN) || IS_SETLK(cmd))
6511 break;
6512
6513 status = -ERESTARTSYS;
6514 spin_lock_irqsave(&q->lock, flags);
6515 if (waiter.notified) {
6516 spin_unlock_irqrestore(&q->lock, flags);
6517 continue;
6518 }
6519 set_current_state(TASK_INTERRUPTIBLE);
6520 spin_unlock_irqrestore(&q->lock, flags);
6521
6522 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6523 }
6524
6525 finish_wait(q, &wait);
6526 return status;
6527}
6528#else /* !CONFIG_NFS_V4_1 */
6529static inline int
6530nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6531{
6532 return nfs4_retry_setlk_simple(state, cmd, request);
6533}
6534#endif
6535
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536static int
6537nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6538{
6539 struct nfs_open_context *ctx;
6540 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 int status;
6542
6543 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006544 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 state = ctx->state;
6546
6547 if (request->fl_start < 0 || request->fl_end < 0)
6548 return -EINVAL;
6549
Trond Myklebustd9531262009-07-21 19:22:38 -04006550 if (IS_GETLK(cmd)) {
6551 if (state != NULL)
6552 return nfs4_proc_getlk(state, F_GETLK, request);
6553 return 0;
6554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
6556 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6557 return -EINVAL;
6558
Trond Myklebustd9531262009-07-21 19:22:38 -04006559 if (request->fl_type == F_UNLCK) {
6560 if (state != NULL)
6561 return nfs4_proc_unlck(state, cmd, request);
6562 return 0;
6563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
Trond Myklebustd9531262009-07-21 19:22:38 -04006565 if (state == NULL)
6566 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006567
6568 if ((request->fl_flags & FL_POSIX) &&
6569 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6570 return -ENOLCK;
6571
Trond Myklebust55725512012-04-18 12:48:35 -04006572 /*
6573 * Don't rely on the VFS having checked the file open mode,
6574 * since it won't do this for flock() locks.
6575 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006576 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006577 case F_RDLCK:
6578 if (!(filp->f_mode & FMODE_READ))
6579 return -EBADF;
6580 break;
6581 case F_WRLCK:
6582 if (!(filp->f_mode & FMODE_WRITE))
6583 return -EBADF;
6584 }
6585
Jeff Layton1ea67db2016-09-17 18:17:37 -04006586 status = nfs4_set_lock_state(state, request);
6587 if (status != 0)
6588 return status;
6589
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006590 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591}
6592
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006593int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006594{
6595 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006596 int err;
6597
6598 err = nfs4_set_lock_state(state, fl);
6599 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006600 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006601 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006602 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006603}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006604
Trond Myklebustcf470c32012-03-07 13:49:12 -05006605struct nfs_release_lockowner_data {
6606 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006607 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006608 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006609 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006610 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006611};
6612
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006613static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6614{
6615 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006616 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006617 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6618 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006619 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006620 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006621}
6622
6623static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6624{
6625 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006626 struct nfs_server *server = data->server;
6627
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006628 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006629
6630 switch (task->tk_status) {
6631 case 0:
6632 renew_lease(server, data->timestamp);
6633 break;
6634 case -NFS4ERR_STALE_CLIENTID:
6635 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006636 nfs4_schedule_lease_recovery(server->nfs_client);
6637 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006638 case -NFS4ERR_LEASE_MOVED:
6639 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006640 if (nfs4_async_handle_error(task, server,
6641 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006642 rpc_restart_call_prepare(task);
6643 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006644}
6645
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006646static void nfs4_release_lockowner_release(void *calldata)
6647{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006648 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006649 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006650 kfree(calldata);
6651}
6652
Trond Myklebust17280172012-03-11 13:11:00 -04006653static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006654 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6655 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006656 .rpc_release = nfs4_release_lockowner_release,
6657};
6658
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006659static void
6660nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006661{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006662 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006663 struct rpc_message msg = {
6664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6665 };
6666
6667 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006668 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006669
Trond Myklebustcf470c32012-03-07 13:49:12 -05006670 data = kmalloc(sizeof(*data), GFP_NOFS);
6671 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006672 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006673 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006674 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006675 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6676 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6677 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006678
Trond Myklebustcf470c32012-03-07 13:49:12 -05006679 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006680 msg.rpc_resp = &data->res;
6681 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006682 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006683}
6684
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006685#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6686
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006687static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006688 struct dentry *unused, struct inode *inode,
6689 const char *key, const void *buf,
6690 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006691{
Al Viro59301222016-05-27 10:19:30 -04006692 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006693}
6694
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006695static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006696 struct dentry *unused, struct inode *inode,
6697 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006698{
Al Virob2968212016-04-10 20:48:24 -04006699 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006700}
6701
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006702static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006703{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006704 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006705}
6706
David Quigleyc9bccef2013-05-22 12:50:45 -04006707#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006708
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006709static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006710 struct dentry *unused, struct inode *inode,
6711 const char *key, const void *buf,
6712 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006713{
6714 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006715 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006716
6717 return -EOPNOTSUPP;
6718}
6719
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006720static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006721 struct dentry *unused, struct inode *inode,
6722 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006723{
6724 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006725 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006726 return -EOPNOTSUPP;
6727}
6728
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006729static ssize_t
6730nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006731{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006732 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006733
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006734 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6735 len = security_inode_listsecurity(inode, list, list_len);
6736 if (list_len && len > list_len)
6737 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006738 }
6739 return len;
6740}
6741
6742static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6743 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006744 .get = nfs4_xattr_get_nfs4_label,
6745 .set = nfs4_xattr_set_nfs4_label,
6746};
David Quigleyc9bccef2013-05-22 12:50:45 -04006747
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006748#else
6749
6750static ssize_t
6751nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6752{
6753 return 0;
6754}
6755
6756#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006757
Andy Adamson533eb462011-06-13 18:25:56 -04006758/*
6759 * nfs_fhget will use either the mounted_on_fileid or the fileid
6760 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006761static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6762{
Andy Adamson533eb462011-06-13 18:25:56 -04006763 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6764 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6765 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006766 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006767 return;
6768
6769 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006770 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006771 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6772 fattr->nlink = 2;
6773}
6774
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006775static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6776 const struct qstr *name,
6777 struct nfs4_fs_locations *fs_locations,
6778 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006779{
6780 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006781 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006782 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006783 };
6784 struct nfs4_fs_locations_arg args = {
6785 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006786 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006787 .page = page,
6788 .bitmask = bitmask,
6789 };
Benny Halevy22958462009-04-01 09:22:02 -04006790 struct nfs4_fs_locations_res res = {
6791 .fs_locations = fs_locations,
6792 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006793 struct rpc_message msg = {
6794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6795 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006796 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006797 };
6798 int status;
6799
Harvey Harrison3110ff82008-05-02 13:42:44 -07006800 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006801
6802 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6803 * is not supported */
6804 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6805 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6806 else
6807 bitmask[0] |= FATTR4_WORD0_FILEID;
6808
Trond Myklebustc228fd32007-01-13 02:28:11 -05006809 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006810 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006811 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006812 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006813 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006814 return status;
6815}
6816
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006817int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6818 const struct qstr *name,
6819 struct nfs4_fs_locations *fs_locations,
6820 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006821{
6822 struct nfs4_exception exception = { };
6823 int err;
6824 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006825 err = _nfs4_proc_fs_locations(client, dir, name,
6826 fs_locations, page);
6827 trace_nfs4_get_fs_locations(dir, name, err);
6828 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006829 &exception);
6830 } while (exception.retry);
6831 return err;
6832}
6833
Chuck Leverb03d7352013-10-17 14:12:50 -04006834/*
6835 * This operation also signals the server that this client is
6836 * performing migration recovery. The server can stop returning
6837 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6838 * appended to this compound to identify the client ID which is
6839 * performing recovery.
6840 */
6841static int _nfs40_proc_get_locations(struct inode *inode,
6842 struct nfs4_fs_locations *locations,
6843 struct page *page, struct rpc_cred *cred)
6844{
6845 struct nfs_server *server = NFS_SERVER(inode);
6846 struct rpc_clnt *clnt = server->client;
6847 u32 bitmask[2] = {
6848 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6849 };
6850 struct nfs4_fs_locations_arg args = {
6851 .clientid = server->nfs_client->cl_clientid,
6852 .fh = NFS_FH(inode),
6853 .page = page,
6854 .bitmask = bitmask,
6855 .migration = 1, /* skip LOOKUP */
6856 .renew = 1, /* append RENEW */
6857 };
6858 struct nfs4_fs_locations_res res = {
6859 .fs_locations = locations,
6860 .migration = 1,
6861 .renew = 1,
6862 };
6863 struct rpc_message msg = {
6864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6865 .rpc_argp = &args,
6866 .rpc_resp = &res,
6867 .rpc_cred = cred,
6868 };
6869 unsigned long now = jiffies;
6870 int status;
6871
6872 nfs_fattr_init(&locations->fattr);
6873 locations->server = server;
6874 locations->nlocations = 0;
6875
6876 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6877 nfs4_set_sequence_privileged(&args.seq_args);
6878 status = nfs4_call_sync_sequence(clnt, server, &msg,
6879 &args.seq_args, &res.seq_res);
6880 if (status)
6881 return status;
6882
6883 renew_lease(server, now);
6884 return 0;
6885}
6886
6887#ifdef CONFIG_NFS_V4_1
6888
6889/*
6890 * This operation also signals the server that this client is
6891 * performing migration recovery. The server can stop asserting
6892 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6893 * performing this operation is identified in the SEQUENCE
6894 * operation in this compound.
6895 *
6896 * When the client supports GETATTR(fs_locations_info), it can
6897 * be plumbed in here.
6898 */
6899static int _nfs41_proc_get_locations(struct inode *inode,
6900 struct nfs4_fs_locations *locations,
6901 struct page *page, struct rpc_cred *cred)
6902{
6903 struct nfs_server *server = NFS_SERVER(inode);
6904 struct rpc_clnt *clnt = server->client;
6905 u32 bitmask[2] = {
6906 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6907 };
6908 struct nfs4_fs_locations_arg args = {
6909 .fh = NFS_FH(inode),
6910 .page = page,
6911 .bitmask = bitmask,
6912 .migration = 1, /* skip LOOKUP */
6913 };
6914 struct nfs4_fs_locations_res res = {
6915 .fs_locations = locations,
6916 .migration = 1,
6917 };
6918 struct rpc_message msg = {
6919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6920 .rpc_argp = &args,
6921 .rpc_resp = &res,
6922 .rpc_cred = cred,
6923 };
6924 int status;
6925
6926 nfs_fattr_init(&locations->fattr);
6927 locations->server = server;
6928 locations->nlocations = 0;
6929
6930 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6931 nfs4_set_sequence_privileged(&args.seq_args);
6932 status = nfs4_call_sync_sequence(clnt, server, &msg,
6933 &args.seq_args, &res.seq_res);
6934 if (status == NFS4_OK &&
6935 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6936 status = -NFS4ERR_LEASE_MOVED;
6937 return status;
6938}
6939
6940#endif /* CONFIG_NFS_V4_1 */
6941
6942/**
6943 * nfs4_proc_get_locations - discover locations for a migrated FSID
6944 * @inode: inode on FSID that is migrating
6945 * @locations: result of query
6946 * @page: buffer
6947 * @cred: credential to use for this operation
6948 *
6949 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6950 * operation failed, or a negative errno if a local error occurred.
6951 *
6952 * On success, "locations" is filled in, but if the server has
6953 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6954 * asserted.
6955 *
6956 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6957 * from this client that require migration recovery.
6958 */
6959int nfs4_proc_get_locations(struct inode *inode,
6960 struct nfs4_fs_locations *locations,
6961 struct page *page, struct rpc_cred *cred)
6962{
6963 struct nfs_server *server = NFS_SERVER(inode);
6964 struct nfs_client *clp = server->nfs_client;
6965 const struct nfs4_mig_recovery_ops *ops =
6966 clp->cl_mvops->mig_recovery_ops;
6967 struct nfs4_exception exception = { };
6968 int status;
6969
6970 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6971 (unsigned long long)server->fsid.major,
6972 (unsigned long long)server->fsid.minor,
6973 clp->cl_hostname);
6974 nfs_display_fhandle(NFS_FH(inode), __func__);
6975
6976 do {
6977 status = ops->get_locations(inode, locations, page, cred);
6978 if (status != -NFS4ERR_DELAY)
6979 break;
6980 nfs4_handle_exception(server, status, &exception);
6981 } while (exception.retry);
6982 return status;
6983}
6984
Chuck Lever44c99932013-10-17 14:13:30 -04006985/*
6986 * This operation also signals the server that this client is
6987 * performing "lease moved" recovery. The server can stop
6988 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6989 * is appended to this compound to identify the client ID which is
6990 * performing recovery.
6991 */
6992static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6993{
6994 struct nfs_server *server = NFS_SERVER(inode);
6995 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6996 struct rpc_clnt *clnt = server->client;
6997 struct nfs4_fsid_present_arg args = {
6998 .fh = NFS_FH(inode),
6999 .clientid = clp->cl_clientid,
7000 .renew = 1, /* append RENEW */
7001 };
7002 struct nfs4_fsid_present_res res = {
7003 .renew = 1,
7004 };
7005 struct rpc_message msg = {
7006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7007 .rpc_argp = &args,
7008 .rpc_resp = &res,
7009 .rpc_cred = cred,
7010 };
7011 unsigned long now = jiffies;
7012 int status;
7013
7014 res.fh = nfs_alloc_fhandle();
7015 if (res.fh == NULL)
7016 return -ENOMEM;
7017
7018 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7019 nfs4_set_sequence_privileged(&args.seq_args);
7020 status = nfs4_call_sync_sequence(clnt, server, &msg,
7021 &args.seq_args, &res.seq_res);
7022 nfs_free_fhandle(res.fh);
7023 if (status)
7024 return status;
7025
7026 do_renew_lease(clp, now);
7027 return 0;
7028}
7029
7030#ifdef CONFIG_NFS_V4_1
7031
7032/*
7033 * This operation also signals the server that this client is
7034 * performing "lease moved" recovery. The server can stop asserting
7035 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7036 * this operation is identified in the SEQUENCE operation in this
7037 * compound.
7038 */
7039static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7040{
7041 struct nfs_server *server = NFS_SERVER(inode);
7042 struct rpc_clnt *clnt = server->client;
7043 struct nfs4_fsid_present_arg args = {
7044 .fh = NFS_FH(inode),
7045 };
7046 struct nfs4_fsid_present_res res = {
7047 };
7048 struct rpc_message msg = {
7049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7050 .rpc_argp = &args,
7051 .rpc_resp = &res,
7052 .rpc_cred = cred,
7053 };
7054 int status;
7055
7056 res.fh = nfs_alloc_fhandle();
7057 if (res.fh == NULL)
7058 return -ENOMEM;
7059
7060 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7061 nfs4_set_sequence_privileged(&args.seq_args);
7062 status = nfs4_call_sync_sequence(clnt, server, &msg,
7063 &args.seq_args, &res.seq_res);
7064 nfs_free_fhandle(res.fh);
7065 if (status == NFS4_OK &&
7066 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7067 status = -NFS4ERR_LEASE_MOVED;
7068 return status;
7069}
7070
7071#endif /* CONFIG_NFS_V4_1 */
7072
7073/**
7074 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7075 * @inode: inode on FSID to check
7076 * @cred: credential to use for this operation
7077 *
7078 * Server indicates whether the FSID is present, moved, or not
7079 * recognized. This operation is necessary to clear a LEASE_MOVED
7080 * condition for this client ID.
7081 *
7082 * Returns NFS4_OK if the FSID is present on this server,
7083 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7084 * NFS4ERR code if some error occurred on the server, or a
7085 * negative errno if a local failure occurred.
7086 */
7087int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7088{
7089 struct nfs_server *server = NFS_SERVER(inode);
7090 struct nfs_client *clp = server->nfs_client;
7091 const struct nfs4_mig_recovery_ops *ops =
7092 clp->cl_mvops->mig_recovery_ops;
7093 struct nfs4_exception exception = { };
7094 int status;
7095
7096 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7097 (unsigned long long)server->fsid.major,
7098 (unsigned long long)server->fsid.minor,
7099 clp->cl_hostname);
7100 nfs_display_fhandle(NFS_FH(inode), __func__);
7101
7102 do {
7103 status = ops->fsid_present(inode, cred);
7104 if (status != -NFS4ERR_DELAY)
7105 break;
7106 nfs4_handle_exception(server, status, &exception);
7107 } while (exception.retry);
7108 return status;
7109}
7110
Andy Adamson5ec16a82013-08-08 10:57:55 -04007111/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007112 * If 'use_integrity' is true and the state managment nfs_client
7113 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7114 * and the machine credential as per RFC3530bis and RFC5661 Security
7115 * Considerations sections. Otherwise, just use the user cred with the
7116 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007117 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007118static 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 +00007119{
7120 int status;
7121 struct nfs4_secinfo_arg args = {
7122 .dir_fh = NFS_FH(dir),
7123 .name = name,
7124 };
7125 struct nfs4_secinfo_res res = {
7126 .flavors = flavors,
7127 };
7128 struct rpc_message msg = {
7129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7130 .rpc_argp = &args,
7131 .rpc_resp = &res,
7132 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007133 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007134 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007135
7136 if (use_integrity) {
7137 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007138 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7139 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007140 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007141
7142 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007143
7144 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7145 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7146
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007147 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7148 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007149 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007150
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007151 if (cred)
7152 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007153
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007154 return status;
7155}
7156
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007157int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7158 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007159{
7160 struct nfs4_exception exception = { };
7161 int err;
7162 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007163 err = -NFS4ERR_WRONGSEC;
7164
7165 /* try to use integrity protection with machine cred */
7166 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7167 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7168
7169 /*
7170 * if unable to use integrity protection, or SECINFO with
7171 * integrity protection returns NFS4ERR_WRONGSEC (which is
7172 * disallowed by spec, but exists in deployed servers) use
7173 * the current filesystem's rpc_client and the user cred.
7174 */
7175 if (err == -NFS4ERR_WRONGSEC)
7176 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7177
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007178 trace_nfs4_secinfo(dir, name, err);
7179 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007180 &exception);
7181 } while (exception.retry);
7182 return err;
7183}
7184
Andy Adamson557134a2009-04-01 09:21:53 -04007185#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007186/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007187 * Check the exchange flags returned by the server for invalid flags, having
7188 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7189 * DS flags set.
7190 */
7191static int nfs4_check_cl_exchange_flags(u32 flags)
7192{
7193 if (flags & ~EXCHGID4_FLAG_MASK_R)
7194 goto out_inval;
7195 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7196 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7197 goto out_inval;
7198 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7199 goto out_inval;
7200 return NFS_OK;
7201out_inval:
7202 return -NFS4ERR_INVAL;
7203}
7204
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007205static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007206nfs41_same_server_scope(struct nfs41_server_scope *a,
7207 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007208{
7209 if (a->server_scope_sz == b->server_scope_sz &&
7210 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7211 return true;
7212
7213 return false;
7214}
7215
Andy Adamson02a95de2016-02-05 16:08:37 -05007216static void
7217nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7218{
7219}
7220
7221static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7222 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7223};
7224
Andy Adamson357f54d2010-12-14 10:11:57 -05007225/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007226 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007227 *
7228 * The 4.1 client currently uses the same TCP connection for the
7229 * fore and backchannel.
7230 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007231static
7232int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7233 struct rpc_xprt *xprt,
7234 struct nfs_client *clp,
7235 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007236{
7237 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007238 struct nfs41_bind_conn_to_session_args args = {
7239 .client = clp,
7240 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7241 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007242 struct nfs41_bind_conn_to_session_res res;
7243 struct rpc_message msg = {
7244 .rpc_proc =
7245 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007246 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007247 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007248 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007249 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007250 struct rpc_task_setup task_setup_data = {
7251 .rpc_client = clnt,
7252 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007253 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007254 .rpc_message = &msg,
7255 .flags = RPC_TASK_TIMEOUT,
7256 };
7257 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007258
7259 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007260
Trond Myklebust71a097c2015-02-18 09:27:18 -08007261 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7262 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7263 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007264
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007265 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7266 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7267 args.dir = NFS4_CDFC4_FORE;
7268
7269 task = rpc_run_task(&task_setup_data);
7270 if (!IS_ERR(task)) {
7271 status = task->tk_status;
7272 rpc_put_task(task);
7273 } else
7274 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007275 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007276 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007277 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007278 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7279 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7280 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007281 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007282 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007283 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007284 dprintk("NFS: %s: Unexpected direction from server\n",
7285 __func__);
7286 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007287 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007288 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007289 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007290 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7291 __func__);
7292 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007293 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007294 }
7295 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007296out:
7297 dprintk("<-- %s status= %d\n", __func__, status);
7298 return status;
7299}
7300
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007301struct rpc_bind_conn_calldata {
7302 struct nfs_client *clp;
7303 struct rpc_cred *cred;
7304};
7305
7306static int
7307nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7308 struct rpc_xprt *xprt,
7309 void *calldata)
7310{
7311 struct rpc_bind_conn_calldata *p = calldata;
7312
7313 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7314}
7315
7316int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7317{
7318 struct rpc_bind_conn_calldata data = {
7319 .clp = clp,
7320 .cred = cred,
7321 };
7322 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7323 nfs4_proc_bind_conn_to_session_callback, &data);
7324}
7325
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007326/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007327 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7328 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007329 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007330static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7331 .how = SP4_MACH_CRED,
7332 .enforce.u.words = {
7333 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7334 1 << (OP_EXCHANGE_ID - 32) |
7335 1 << (OP_CREATE_SESSION - 32) |
7336 1 << (OP_DESTROY_SESSION - 32) |
7337 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007338 },
7339 .allow.u.words = {
7340 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007341 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007342 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007343 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007344 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007345 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007346 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007347 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007348 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007349 1 << (OP_FREE_STATEID - 32) |
7350 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007351 }
7352};
7353
7354/*
7355 * Select the state protection mode for client `clp' given the server results
7356 * from exchange_id in `sp'.
7357 *
7358 * Returns 0 on success, negative errno otherwise.
7359 */
7360static int nfs4_sp4_select_mode(struct nfs_client *clp,
7361 struct nfs41_state_protection *sp)
7362{
7363 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7364 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7365 1 << (OP_EXCHANGE_ID - 32) |
7366 1 << (OP_CREATE_SESSION - 32) |
7367 1 << (OP_DESTROY_SESSION - 32) |
7368 1 << (OP_DESTROY_CLIENTID - 32)
7369 };
7370 unsigned int i;
7371
7372 if (sp->how == SP4_MACH_CRED) {
7373 /* Print state protect result */
7374 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7375 for (i = 0; i <= LAST_NFS4_OP; i++) {
7376 if (test_bit(i, sp->enforce.u.longs))
7377 dfprintk(MOUNT, " enforce op %d\n", i);
7378 if (test_bit(i, sp->allow.u.longs))
7379 dfprintk(MOUNT, " allow op %d\n", i);
7380 }
7381
7382 /* make sure nothing is on enforce list that isn't supported */
7383 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7384 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7385 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7386 return -EINVAL;
7387 }
7388 }
7389
7390 /*
7391 * Minimal mode - state operations are allowed to use machine
7392 * credential. Note this already happens by default, so the
7393 * client doesn't have to do anything more than the negotiation.
7394 *
7395 * NOTE: we don't care if EXCHANGE_ID is in the list -
7396 * we're already using the machine cred for exchange_id
7397 * and will never use a different cred.
7398 */
7399 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7400 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7401 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7402 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7403 dfprintk(MOUNT, "sp4_mach_cred:\n");
7404 dfprintk(MOUNT, " minimal mode enabled\n");
7405 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7406 } else {
7407 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7408 return -EINVAL;
7409 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007410
7411 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007412 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7413 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007414 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7415 dfprintk(MOUNT, " cleanup mode enabled\n");
7416 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7417 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007418
Andrew Elble99ade3c2015-12-02 09:39:51 -05007419 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7420 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7421 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7422 &clp->cl_sp4_flags);
7423 }
7424
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007425 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7426 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7427 dfprintk(MOUNT, " secinfo mode enabled\n");
7428 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7429 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007430
7431 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7432 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7433 dfprintk(MOUNT, " stateid mode enabled\n");
7434 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7435 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007436
7437 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7438 dfprintk(MOUNT, " write mode enabled\n");
7439 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7440 }
7441
7442 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7443 dfprintk(MOUNT, " commit mode enabled\n");
7444 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7445 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007446 }
7447
7448 return 0;
7449}
7450
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451struct nfs41_exchange_id_data {
7452 struct nfs41_exchange_id_res res;
7453 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007454 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007455 int rpc_status;
7456};
7457
7458static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007459{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007460 struct nfs41_exchange_id_data *cdata =
7461 (struct nfs41_exchange_id_data *)data;
7462 struct nfs_client *clp = cdata->args.client;
7463 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007464
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007465 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007466
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007467 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007468 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007469
7470 if (cdata->xprt && status == 0) {
7471 status = nfs4_detect_session_trunking(clp, &cdata->res,
7472 cdata->xprt);
7473 goto out;
7474 }
7475
Andy Adamson8d89bd72016-09-09 09:22:18 -04007476 if (status == 0)
7477 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007478
Chuck Lever177313f2012-05-21 22:44:58 -04007479 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007480 clp->cl_clientid = cdata->res.clientid;
7481 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007482 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007484 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007485 &clp->cl_session->session_state);
7486 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007487 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007488
Chuck Leveracdeb692012-05-21 22:46:16 -04007489 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007490 clp->cl_serverowner = cdata->res.server_owner;
7491 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007492
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007493 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007494 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007495 clp->cl_implid = cdata->res.impl_id;
7496 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007497
Chuck Lever177313f2012-05-21 22:44:58 -04007498 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007499 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007501 dprintk("%s: server_scope mismatch detected\n",
7502 __func__);
7503 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007504 kfree(clp->cl_serverscope);
7505 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007506 }
7507
Chuck Lever177313f2012-05-21 22:44:58 -04007508 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007509 clp->cl_serverscope = cdata->res.server_scope;
7510 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007511 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007512 /* Save the EXCHANGE_ID verifier session trunk tests */
7513 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7514 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007516out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007517 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007518 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007519}
7520
7521static void nfs4_exchange_id_release(void *data)
7522{
7523 struct nfs41_exchange_id_data *cdata =
7524 (struct nfs41_exchange_id_data *)data;
7525
7526 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007527 if (cdata->xprt) {
7528 xprt_put(cdata->xprt);
7529 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7530 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007531 kfree(cdata->res.impl_id);
7532 kfree(cdata->res.server_scope);
7533 kfree(cdata->res.server_owner);
7534 kfree(cdata);
7535}
7536
7537static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7538 .rpc_call_done = nfs4_exchange_id_done,
7539 .rpc_release = nfs4_exchange_id_release,
7540};
7541
Benny Halevy99fe60d2009-04-01 09:22:29 -04007542/*
7543 * _nfs4_proc_exchange_id()
7544 *
7545 * Wrapper for EXCHANGE_ID operation.
7546 */
7547static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007548 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007549{
7550 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007551 struct rpc_message msg = {
7552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007553 .rpc_cred = cred,
7554 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007555 struct rpc_task_setup task_setup_data = {
7556 .rpc_client = clp->cl_rpcclient,
7557 .callback_ops = &nfs4_exchange_id_call_ops,
7558 .rpc_message = &msg,
7559 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7560 };
7561 struct nfs41_exchange_id_data *calldata;
7562 struct rpc_task *task;
7563 int status = -EIO;
7564
7565 if (!atomic_inc_not_zero(&clp->cl_count))
7566 goto out;
7567
7568 status = -ENOMEM;
7569 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7570 if (!calldata)
7571 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007572
Andy Adamsonad0849a2016-09-09 09:22:28 -04007573 if (!xprt)
7574 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007575
7576 status = nfs4_init_uniform_client_string(clp);
7577 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007578 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007579
7580 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7581 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7582 clp->cl_owner_id);
7583
Andy Adamson8d89bd72016-09-09 09:22:18 -04007584 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7585 GFP_NOFS);
7586 status = -ENOMEM;
7587 if (unlikely(calldata->res.server_owner == NULL))
7588 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007589
Andy Adamson8d89bd72016-09-09 09:22:18 -04007590 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007591 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007592 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007593 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007594
Andy Adamson8d89bd72016-09-09 09:22:18 -04007595 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7596 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007597 goto out_server_scope;
7598
7599 switch (sp4_how) {
7600 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007601 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007602 break;
7603
7604 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007605 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007606 break;
7607
7608 default:
7609 /* unsupported! */
7610 WARN_ON_ONCE(1);
7611 status = -EINVAL;
7612 goto out_impl_id;
7613 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007614 if (xprt) {
7615 calldata->xprt = xprt;
7616 task_setup_data.rpc_xprt = xprt;
7617 task_setup_data.flags =
7618 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7619 calldata->args.verifier = &clp->cl_confirm;
7620 } else {
7621 calldata->args.verifier = &verifier;
7622 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007623 calldata->args.client = clp;
7624#ifdef CONFIG_NFS_V4_1_MIGRATION
7625 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7626 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7627 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7628#else
7629 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7630 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7631#endif
7632 msg.rpc_argp = &calldata->args;
7633 msg.rpc_resp = &calldata->res;
7634 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007635
Andy Adamson8d89bd72016-09-09 09:22:18 -04007636 task = rpc_run_task(&task_setup_data);
7637 if (IS_ERR(task)) {
7638 status = PTR_ERR(task);
7639 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007640 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007641
Andy Adamsonad0849a2016-09-09 09:22:28 -04007642 if (!xprt) {
7643 status = rpc_wait_for_completion_task(task);
7644 if (!status)
7645 status = calldata->rpc_status;
7646 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007647 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007648
Andy Adamson8d89bd72016-09-09 09:22:18 -04007649 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007650out:
Chuck Lever177313f2012-05-21 22:44:58 -04007651 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007652 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007653 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007654 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007655 clp->cl_implid->date.seconds,
7656 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007657 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007658 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007659
7660out_impl_id:
7661 kfree(calldata->res.impl_id);
7662out_server_scope:
7663 kfree(calldata->res.server_scope);
7664out_server_owner:
7665 kfree(calldata->res.server_owner);
7666out_calldata:
7667 kfree(calldata);
7668 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007669}
7670
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007671/*
7672 * nfs4_proc_exchange_id()
7673 *
7674 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7675 *
7676 * Since the clientid has expired, all compounds using sessions
7677 * associated with the stale clientid will be returning
7678 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7679 * be in some phase of session reset.
7680 *
7681 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7682 */
7683int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7684{
7685 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7686 int status;
7687
7688 /* try SP4_MACH_CRED if krb5i/p */
7689 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7690 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007691 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007692 if (!status)
7693 return 0;
7694 }
7695
7696 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007697 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007698}
7699
Andy Adamson04fa2c62016-09-09 09:22:29 -04007700/**
7701 * nfs4_test_session_trunk
7702 *
7703 * This is an add_xprt_test() test function called from
7704 * rpc_clnt_setup_test_and_add_xprt.
7705 *
7706 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7707 * and is dereferrenced in nfs4_exchange_id_release
7708 *
7709 * Upon success, add the new transport to the rpc_clnt
7710 *
7711 * @clnt: struct rpc_clnt to get new transport
7712 * @xprt: the rpc_xprt to test
7713 * @data: call data for _nfs4_proc_exchange_id.
7714 */
7715int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7716 void *data)
7717{
7718 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7719 u32 sp4_how;
7720
7721 dprintk("--> %s try %s\n", __func__,
7722 xprt->address_strings[RPC_DISPLAY_ADDR]);
7723
7724 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7725
7726 /* Test connection for session trunking. Async exchange_id call */
7727 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7728}
7729EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7730
Trond Myklebust66245532012-05-25 17:18:09 -04007731static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7732 struct rpc_cred *cred)
7733{
7734 struct rpc_message msg = {
7735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7736 .rpc_argp = clp,
7737 .rpc_cred = cred,
7738 };
7739 int status;
7740
7741 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007742 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007743 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007744 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007745 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7746 return status;
7747}
7748
7749static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7750 struct rpc_cred *cred)
7751{
7752 unsigned int loop;
7753 int ret;
7754
7755 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7756 ret = _nfs4_proc_destroy_clientid(clp, cred);
7757 switch (ret) {
7758 case -NFS4ERR_DELAY:
7759 case -NFS4ERR_CLIENTID_BUSY:
7760 ssleep(1);
7761 break;
7762 default:
7763 return ret;
7764 }
7765 }
7766 return 0;
7767}
7768
7769int nfs4_destroy_clientid(struct nfs_client *clp)
7770{
7771 struct rpc_cred *cred;
7772 int ret = 0;
7773
7774 if (clp->cl_mvops->minor_version < 1)
7775 goto out;
7776 if (clp->cl_exchange_flags == 0)
7777 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007778 if (clp->cl_preserve_clid)
7779 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007780 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007781 ret = nfs4_proc_destroy_clientid(clp, cred);
7782 if (cred)
7783 put_rpccred(cred);
7784 switch (ret) {
7785 case 0:
7786 case -NFS4ERR_STALE_CLIENTID:
7787 clp->cl_exchange_flags = 0;
7788 }
7789out:
7790 return ret;
7791}
7792
Andy Adamson2050f0c2009-04-01 09:22:30 -04007793struct nfs4_get_lease_time_data {
7794 struct nfs4_get_lease_time_args *args;
7795 struct nfs4_get_lease_time_res *res;
7796 struct nfs_client *clp;
7797};
7798
7799static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7800 void *calldata)
7801{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007802 struct nfs4_get_lease_time_data *data =
7803 (struct nfs4_get_lease_time_data *)calldata;
7804
7805 dprintk("--> %s\n", __func__);
7806 /* just setup sequence, do not trigger session recovery
7807 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007808 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007809 &data->args->la_seq_args,
7810 &data->res->lr_seq_res,
7811 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007812 dprintk("<-- %s\n", __func__);
7813}
7814
7815/*
7816 * Called from nfs4_state_manager thread for session setup, so don't recover
7817 * from sequence operation or clientid errors.
7818 */
7819static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7820{
7821 struct nfs4_get_lease_time_data *data =
7822 (struct nfs4_get_lease_time_data *)calldata;
7823
7824 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007825 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7826 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007827 switch (task->tk_status) {
7828 case -NFS4ERR_DELAY:
7829 case -NFS4ERR_GRACE:
7830 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7831 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7832 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007833 /* fall through */
7834 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007835 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007836 return;
7837 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007838 dprintk("<-- %s\n", __func__);
7839}
7840
Trond Myklebust17280172012-03-11 13:11:00 -04007841static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007842 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7843 .rpc_call_done = nfs4_get_lease_time_done,
7844};
7845
7846int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7847{
7848 struct rpc_task *task;
7849 struct nfs4_get_lease_time_args args;
7850 struct nfs4_get_lease_time_res res = {
7851 .lr_fsinfo = fsinfo,
7852 };
7853 struct nfs4_get_lease_time_data data = {
7854 .args = &args,
7855 .res = &res,
7856 .clp = clp,
7857 };
7858 struct rpc_message msg = {
7859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7860 .rpc_argp = &args,
7861 .rpc_resp = &res,
7862 };
7863 struct rpc_task_setup task_setup = {
7864 .rpc_client = clp->cl_rpcclient,
7865 .rpc_message = &msg,
7866 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007867 .callback_data = &data,
7868 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007869 };
7870 int status;
7871
Chuck Levera9c92d62013-08-09 12:48:18 -04007872 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007873 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007874 dprintk("--> %s\n", __func__);
7875 task = rpc_run_task(&task_setup);
7876
7877 if (IS_ERR(task))
7878 status = PTR_ERR(task);
7879 else {
7880 status = task->tk_status;
7881 rpc_put_task(task);
7882 }
7883 dprintk("<-- %s return %d\n", __func__, status);
7884
7885 return status;
7886}
7887
Andy Adamsonfc931582009-04-01 09:22:31 -04007888/*
7889 * Initialize the values to be used by the client in CREATE_SESSION
7890 * If nfs4_init_session set the fore channel request and response sizes,
7891 * use them.
7892 *
7893 * Set the back channel max_resp_sz_cached to zero to force the client to
7894 * always set csa_cachethis to FALSE because the current implementation
7895 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7896 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007897static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7898 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007899{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007900 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007901 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007902
Andy Adamson18aad3d2013-06-26 12:21:49 -04007903 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7904 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7905
Andy Adamsonfc931582009-04-01 09:22:31 -04007906 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007907 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7908 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007909 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007910 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007911
7912 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007913 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007914 __func__,
7915 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007916 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007917
7918 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007919 args->bc_attrs.max_rqst_sz = max_bc_payload;
7920 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007921 args->bc_attrs.max_resp_sz_cached = 0;
7922 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007923 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007924
7925 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7926 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7927 __func__,
7928 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7929 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7930 args->bc_attrs.max_reqs);
7931}
7932
Trond Myklebust79969dd2015-02-18 11:30:18 -08007933static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7934 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007935{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007936 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007937 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007938
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007939 if (rcvd->max_resp_sz > sent->max_resp_sz)
7940 return -EINVAL;
7941 /*
7942 * Our requested max_ops is the minimum we need; we're not
7943 * prepared to break up compounds into smaller pieces than that.
7944 * So, no point even trying to continue if the server won't
7945 * cooperate:
7946 */
7947 if (rcvd->max_ops < sent->max_ops)
7948 return -EINVAL;
7949 if (rcvd->max_reqs == 0)
7950 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007951 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7952 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007953 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007954}
7955
Trond Myklebust79969dd2015-02-18 11:30:18 -08007956static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7957 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007958{
7959 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007960 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007961
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007962 if (!(res->flags & SESSION4_BACK_CHAN))
7963 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007964 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7965 return -EINVAL;
7966 if (rcvd->max_resp_sz < sent->max_resp_sz)
7967 return -EINVAL;
7968 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7969 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007970 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007971 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007972 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007973 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007974out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007975 return 0;
7976}
Andy Adamson8d353012009-04-01 09:22:32 -04007977
Andy Adamson8d353012009-04-01 09:22:32 -04007978static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007979 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007980{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007981 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007982
Trond Myklebust79969dd2015-02-18 11:30:18 -08007983 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007984 if (ret)
7985 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007986 return nfs4_verify_back_channel_attrs(args, res);
7987}
7988
7989static void nfs4_update_session(struct nfs4_session *session,
7990 struct nfs41_create_session_res *res)
7991{
7992 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007993 /* Mark client id and session as being confirmed */
7994 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7995 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007996 session->flags = res->flags;
7997 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007998 if (res->flags & SESSION4_BACK_CHAN)
7999 memcpy(&session->bc_attrs, &res->bc_attrs,
8000 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008001}
8002
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008003static int _nfs4_proc_create_session(struct nfs_client *clp,
8004 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008005{
8006 struct nfs4_session *session = clp->cl_session;
8007 struct nfs41_create_session_args args = {
8008 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008009 .clientid = clp->cl_clientid,
8010 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008011 .cb_program = NFS4_CALLBACK,
8012 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008013 struct nfs41_create_session_res res;
8014
Andy Adamsonfc931582009-04-01 09:22:31 -04008015 struct rpc_message msg = {
8016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8017 .rpc_argp = &args,
8018 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008019 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008020 };
8021 int status;
8022
Chuck Lever6b26cc82016-05-02 14:40:40 -04008023 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008024 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008025
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008026 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008027 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008028
Trond Myklebustb519d402016-09-11 14:50:01 -04008029 switch (status) {
8030 case -NFS4ERR_STALE_CLIENTID:
8031 case -NFS4ERR_DELAY:
8032 case -ETIMEDOUT:
8033 case -EACCES:
8034 case -EAGAIN:
8035 goto out;
8036 };
8037
8038 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008039 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008040 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008041 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008042 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008043 if (status)
8044 goto out;
8045 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008046 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008047out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008048 return status;
8049}
8050
8051/*
8052 * Issues a CREATE_SESSION operation to the server.
8053 * It is the responsibility of the caller to verify the session is
8054 * expired before calling this routine.
8055 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008056int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008057{
8058 int status;
8059 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008060 struct nfs4_session *session = clp->cl_session;
8061
8062 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8063
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008064 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008065 if (status)
8066 goto out;
8067
Andy Adamsonaacd5532011-11-09 13:58:21 -05008068 /* Init or reset the session slot tables */
8069 status = nfs4_setup_session_slot_tables(session);
8070 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008071 if (status)
8072 goto out;
8073
8074 ptr = (unsigned *)&session->sess_id.data[0];
8075 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8076 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008077out:
8078 dprintk("<-- %s\n", __func__);
8079 return status;
8080}
8081
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008082/*
8083 * Issue the over-the-wire RPC DESTROY_SESSION.
8084 * The caller must serialize access to this routine.
8085 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008086int nfs4_proc_destroy_session(struct nfs4_session *session,
8087 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008088{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008089 struct rpc_message msg = {
8090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8091 .rpc_argp = session,
8092 .rpc_cred = cred,
8093 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008094 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008095
8096 dprintk("--> nfs4_proc_destroy_session\n");
8097
8098 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008099 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8100 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008101
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008102 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008103 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008104
8105 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008106 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008107 "Session has been destroyed regardless...\n", status);
8108
8109 dprintk("<-- nfs4_proc_destroy_session\n");
8110 return status;
8111}
8112
Trond Myklebust7b38c362012-05-23 13:23:31 -04008113/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008114 * Renew the cl_session lease.
8115 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008116struct nfs4_sequence_data {
8117 struct nfs_client *clp;
8118 struct nfs4_sequence_args args;
8119 struct nfs4_sequence_res res;
8120};
8121
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008122static void nfs41_sequence_release(void *data)
8123{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008124 struct nfs4_sequence_data *calldata = data;
8125 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008126
Alexandros Batsakis71358402010-02-05 03:45:05 -08008127 if (atomic_read(&clp->cl_count) > 1)
8128 nfs4_schedule_state_renewal(clp);
8129 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008130 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008131}
8132
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008133static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8134{
8135 switch(task->tk_status) {
8136 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008137 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8138 return -EAGAIN;
8139 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008140 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008141 }
8142 return 0;
8143}
8144
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008145static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008146{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008147 struct nfs4_sequence_data *calldata = data;
8148 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008149
Trond Myklebust14516c32010-07-31 14:29:06 -04008150 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8151 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008152
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008153 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008154 if (task->tk_status < 0) {
8155 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008156 if (atomic_read(&clp->cl_count) == 1)
8157 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008158
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008159 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8160 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008161 return;
8162 }
8163 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008164 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008165out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008166 dprintk("<-- %s\n", __func__);
8167}
8168
8169static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8170{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008171 struct nfs4_sequence_data *calldata = data;
8172 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008173 struct nfs4_sequence_args *args;
8174 struct nfs4_sequence_res *res;
8175
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008176 args = task->tk_msg.rpc_argp;
8177 res = task->tk_msg.rpc_resp;
8178
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008179 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008180}
8181
8182static const struct rpc_call_ops nfs41_sequence_ops = {
8183 .rpc_call_done = nfs41_sequence_call_done,
8184 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008185 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008186};
8187
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008188static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8189 struct rpc_cred *cred,
8190 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008191{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008192 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008193 struct rpc_message msg = {
8194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8195 .rpc_cred = cred,
8196 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008197 struct rpc_task_setup task_setup_data = {
8198 .rpc_client = clp->cl_rpcclient,
8199 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008200 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008201 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008202 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008203
Alexandros Batsakis71358402010-02-05 03:45:05 -08008204 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008205 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008206 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008207 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008208 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008209 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008210 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008211 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008212 if (is_privileged)
8213 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008214 msg.rpc_argp = &calldata->args;
8215 msg.rpc_resp = &calldata->res;
8216 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008217 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008218
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008219 return rpc_run_task(&task_setup_data);
8220}
8221
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008222static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008223{
8224 struct rpc_task *task;
8225 int ret = 0;
8226
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008227 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008228 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008229 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008230 if (IS_ERR(task))
8231 ret = PTR_ERR(task);
8232 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008233 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008234 dprintk("<-- %s status=%d\n", __func__, ret);
8235 return ret;
8236}
8237
8238static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8239{
8240 struct rpc_task *task;
8241 int ret;
8242
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008243 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008244 if (IS_ERR(task)) {
8245 ret = PTR_ERR(task);
8246 goto out;
8247 }
8248 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008249 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008250 ret = task->tk_status;
8251 rpc_put_task(task);
8252out:
8253 dprintk("<-- %s status=%d\n", __func__, ret);
8254 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008255}
8256
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008257struct nfs4_reclaim_complete_data {
8258 struct nfs_client *clp;
8259 struct nfs41_reclaim_complete_args arg;
8260 struct nfs41_reclaim_complete_res res;
8261};
8262
8263static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8264{
8265 struct nfs4_reclaim_complete_data *calldata = data;
8266
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008267 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008268 &calldata->arg.seq_args,
8269 &calldata->res.seq_res,
8270 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008271}
8272
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008273static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8274{
8275 switch(task->tk_status) {
8276 case 0:
8277 case -NFS4ERR_COMPLETE_ALREADY:
8278 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8279 break;
8280 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008281 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008282 /* fall through */
8283 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008284 return -EAGAIN;
8285 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008286 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008287 }
8288 return 0;
8289}
8290
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008291static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8292{
8293 struct nfs4_reclaim_complete_data *calldata = data;
8294 struct nfs_client *clp = calldata->clp;
8295 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8296
8297 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008298 if (!nfs41_sequence_done(task, res))
8299 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008300
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008301 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008302 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8303 rpc_restart_call_prepare(task);
8304 return;
8305 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008306 dprintk("<-- %s\n", __func__);
8307}
8308
8309static void nfs4_free_reclaim_complete_data(void *data)
8310{
8311 struct nfs4_reclaim_complete_data *calldata = data;
8312
8313 kfree(calldata);
8314}
8315
8316static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8317 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8318 .rpc_call_done = nfs4_reclaim_complete_done,
8319 .rpc_release = nfs4_free_reclaim_complete_data,
8320};
8321
8322/*
8323 * Issue a global reclaim complete.
8324 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008325static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8326 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008327{
8328 struct nfs4_reclaim_complete_data *calldata;
8329 struct rpc_task *task;
8330 struct rpc_message msg = {
8331 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008332 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008333 };
8334 struct rpc_task_setup task_setup_data = {
8335 .rpc_client = clp->cl_rpcclient,
8336 .rpc_message = &msg,
8337 .callback_ops = &nfs4_reclaim_complete_call_ops,
8338 .flags = RPC_TASK_ASYNC,
8339 };
8340 int status = -ENOMEM;
8341
8342 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008343 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008344 if (calldata == NULL)
8345 goto out;
8346 calldata->clp = clp;
8347 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008348
Chuck Levera9c92d62013-08-09 12:48:18 -04008349 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008350 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008351 msg.rpc_argp = &calldata->arg;
8352 msg.rpc_resp = &calldata->res;
8353 task_setup_data.callback_data = calldata;
8354 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008355 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008356 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008357 goto out;
8358 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008359 status = nfs4_wait_for_completion_rpc_task(task);
8360 if (status == 0)
8361 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008362 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008363 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008364out:
8365 dprintk("<-- %s status=%d\n", __func__, status);
8366 return status;
8367}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008368
8369static void
8370nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8371{
8372 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008373 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008374
8375 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008376 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008377 &lgp->res.seq_res, task);
8378 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008379}
8380
8381static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8382{
8383 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008384
8385 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008386 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008387 dprintk("<-- %s\n", __func__);
8388}
8389
8390static int
8391nfs4_layoutget_handle_exception(struct rpc_task *task,
8392 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8393{
Trond Myklebustee314c22012-10-01 17:25:48 -07008394 struct inode *inode = lgp->args.inode;
8395 struct nfs_server *server = NFS_SERVER(inode);
8396 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008397 int nfs4err = task->tk_status;
8398 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008399 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008400
Boaz Harroshed7e5422014-01-22 20:34:54 +02008401 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008402
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008403 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008404 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008405 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008406
8407 /*
8408 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8409 * on the file. set tk_status to -ENODATA to tell upper layer to
8410 * retry go inband.
8411 */
8412 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008413 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008414 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008415 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008416 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8417 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8418 */
8419 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008420 status = -EOVERFLOW;
8421 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008422 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008423 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008424 * (or clients) writing to the same RAID stripe except when
8425 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008426 *
8427 * Treat it like we would RECALLCONFLICT -- we retry for a little
8428 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008429 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008430 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008431 if (lgp->args.minlength == 0) {
8432 status = -EOVERFLOW;
8433 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008434 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008435 status = -EBUSY;
8436 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008437 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008438 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008439 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008440 case -NFS4ERR_DELEG_REVOKED:
8441 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008442 case -NFS4ERR_EXPIRED:
8443 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008444 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008445 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008446 lo = NFS_I(inode)->layout;
8447 /* If the open stateid was bad, then recover it. */
8448 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8449 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008450 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008451 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008452 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008453 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008454 break;
8455 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008456
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008457 /*
8458 * Mark the bad layout state as invalid, then retry
8459 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008460 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008461 spin_unlock(&inode->i_lock);
8462 pnfs_free_lseg_list(&head);
8463 status = -EAGAIN;
8464 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008465 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008466
Trond Myklebust8ac09252017-01-23 22:44:12 -05008467 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008468 err = nfs4_handle_exception(server, nfs4err, exception);
8469 if (!status) {
8470 if (exception->retry)
8471 status = -EAGAIN;
8472 else
8473 status = err;
8474 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008475out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008476 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008477 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008478}
8479
Idan Kedar85541162012-08-02 11:47:10 +03008480static size_t max_response_pages(struct nfs_server *server)
8481{
8482 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8483 return nfs_page_array_len(0, max_resp_sz);
8484}
8485
8486static void nfs4_free_pages(struct page **pages, size_t size)
8487{
8488 int i;
8489
8490 if (!pages)
8491 return;
8492
8493 for (i = 0; i < size; i++) {
8494 if (!pages[i])
8495 break;
8496 __free_page(pages[i]);
8497 }
8498 kfree(pages);
8499}
8500
8501static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8502{
8503 struct page **pages;
8504 int i;
8505
8506 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8507 if (!pages) {
8508 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8509 return NULL;
8510 }
8511
8512 for (i = 0; i < size; i++) {
8513 pages[i] = alloc_page(gfp_flags);
8514 if (!pages[i]) {
8515 dprintk("%s: failed to allocate page\n", __func__);
8516 nfs4_free_pages(pages, size);
8517 return NULL;
8518 }
8519 }
8520
8521 return pages;
8522}
8523
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008524static void nfs4_layoutget_release(void *calldata)
8525{
8526 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008527 struct inode *inode = lgp->args.inode;
8528 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008529 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008530
8531 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008532 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008533 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008534 put_nfs_open_context(lgp->args.ctx);
8535 kfree(calldata);
8536 dprintk("<-- %s\n", __func__);
8537}
8538
8539static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8540 .rpc_call_prepare = nfs4_layoutget_prepare,
8541 .rpc_call_done = nfs4_layoutget_done,
8542 .rpc_release = nfs4_layoutget_release,
8543};
8544
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008545struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008546nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008547{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008548 struct inode *inode = lgp->args.inode;
8549 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008550 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008551 struct rpc_task *task;
8552 struct rpc_message msg = {
8553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8554 .rpc_argp = &lgp->args,
8555 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008556 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008557 };
8558 struct rpc_task_setup task_setup_data = {
8559 .rpc_client = server->client,
8560 .rpc_message = &msg,
8561 .callback_ops = &nfs4_layoutget_call_ops,
8562 .callback_data = lgp,
8563 .flags = RPC_TASK_ASYNC,
8564 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008565 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008566 struct nfs4_exception exception = {
8567 .inode = inode,
8568 .timeout = *timeout,
8569 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008570 int status = 0;
8571
8572 dprintk("--> %s\n", __func__);
8573
Peng Tao4bd5a9802014-11-17 11:05:17 +08008574 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8575 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8576
Idan Kedar85541162012-08-02 11:47:10 +03008577 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8578 if (!lgp->args.layout.pages) {
8579 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008580 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008581 }
8582 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8583
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008584 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008585 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008586 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008587
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008588 task = rpc_run_task(&task_setup_data);
8589 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008590 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008591 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008592 if (status == 0) {
8593 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8594 *timeout = exception.timeout;
8595 }
8596
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008597 trace_nfs4_layoutget(lgp->args.ctx,
8598 &lgp->args.range,
8599 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008600 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008601 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008602
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008603 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8604 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008605 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008606 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008607 rpc_put_task(task);
8608 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008609 if (status)
8610 return ERR_PTR(status);
8611 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008612}
8613
Benny Halevycbe82602011-05-22 19:52:37 +03008614static void
8615nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8616{
8617 struct nfs4_layoutreturn *lrp = calldata;
8618
8619 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008620 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008621 &lrp->args.seq_args,
8622 &lrp->res.seq_res,
8623 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008624}
8625
8626static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8627{
8628 struct nfs4_layoutreturn *lrp = calldata;
8629 struct nfs_server *server;
8630
8631 dprintk("--> %s\n", __func__);
8632
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008633 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008634 return;
8635
8636 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008637 switch (task->tk_status) {
8638 default:
8639 task->tk_status = 0;
8640 case 0:
8641 break;
8642 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008643 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008644 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008645 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008646 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008647 return;
8648 }
Benny Halevycbe82602011-05-22 19:52:37 +03008649 dprintk("<-- %s\n", __func__);
8650}
8651
8652static void nfs4_layoutreturn_release(void *calldata)
8653{
8654 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008655 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008656
8657 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008658 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008659 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008660 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008661 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8662 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008663 pnfs_put_layout_hdr(lrp->args.layout);
8664 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008665 kfree(calldata);
8666 dprintk("<-- %s\n", __func__);
8667}
8668
8669static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8670 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8671 .rpc_call_done = nfs4_layoutreturn_done,
8672 .rpc_release = nfs4_layoutreturn_release,
8673};
8674
Peng Tao6c166052014-11-17 09:30:40 +08008675int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008676{
8677 struct rpc_task *task;
8678 struct rpc_message msg = {
8679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8680 .rpc_argp = &lrp->args,
8681 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008682 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008683 };
8684 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008685 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008686 .rpc_message = &msg,
8687 .callback_ops = &nfs4_layoutreturn_call_ops,
8688 .callback_data = lrp,
8689 };
Peng Tao6c166052014-11-17 09:30:40 +08008690 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008691
Andrew Elble99ade3c2015-12-02 09:39:51 -05008692 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8693 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8694 &task_setup_data.rpc_client, &msg);
8695
Benny Halevycbe82602011-05-22 19:52:37 +03008696 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008697 if (!sync) {
8698 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8699 if (!lrp->inode) {
8700 nfs4_layoutreturn_release(lrp);
8701 return -EAGAIN;
8702 }
8703 task_setup_data.flags |= RPC_TASK_ASYNC;
8704 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008705 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008706 task = rpc_run_task(&task_setup_data);
8707 if (IS_ERR(task))
8708 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008709 if (sync)
8710 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008711 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008712 dprintk("<-- %s status=%d\n", __func__, status);
8713 rpc_put_task(task);
8714 return status;
8715}
8716
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008717static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008718_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8719 struct pnfs_device *pdev,
8720 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008721{
8722 struct nfs4_getdeviceinfo_args args = {
8723 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008724 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8725 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008726 };
8727 struct nfs4_getdeviceinfo_res res = {
8728 .pdev = pdev,
8729 };
8730 struct rpc_message msg = {
8731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8732 .rpc_argp = &args,
8733 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008734 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008735 };
8736 int status;
8737
8738 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008739 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008740 if (res.notification & ~args.notify_types)
8741 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008742 if (res.notification != args.notify_types)
8743 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008744
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008745 dprintk("<-- %s status=%d\n", __func__, status);
8746
8747 return status;
8748}
8749
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008750int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8751 struct pnfs_device *pdev,
8752 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008753{
8754 struct nfs4_exception exception = { };
8755 int err;
8756
8757 do {
8758 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008759 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008760 &exception);
8761 } while (exception.retry);
8762 return err;
8763}
8764EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8765
Andy Adamson863a3c62011-03-23 13:27:54 +00008766static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8767{
8768 struct nfs4_layoutcommit_data *data = calldata;
8769 struct nfs_server *server = NFS_SERVER(data->args.inode);
8770
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008771 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008772 &data->args.seq_args,
8773 &data->res.seq_res,
8774 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008775}
8776
8777static void
8778nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8779{
8780 struct nfs4_layoutcommit_data *data = calldata;
8781 struct nfs_server *server = NFS_SERVER(data->args.inode);
8782
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008783 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008784 return;
8785
8786 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008787 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8788 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8789 case -NFS4ERR_BADLAYOUT: /* no layout */
8790 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008791 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008792 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008793 break;
8794 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008795 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008796 rpc_restart_call_prepare(task);
8797 return;
8798 }
8799 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008800}
8801
8802static void nfs4_layoutcommit_release(void *calldata)
8803{
8804 struct nfs4_layoutcommit_data *data = calldata;
8805
Andy Adamsondb29c082011-07-30 20:52:38 -04008806 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008807 nfs_post_op_update_inode_force_wcc(data->args.inode,
8808 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008809 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008810 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008811 kfree(data);
8812}
8813
8814static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8815 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8816 .rpc_call_done = nfs4_layoutcommit_done,
8817 .rpc_release = nfs4_layoutcommit_release,
8818};
8819
8820int
Andy Adamsonef311532011-03-12 02:58:10 -05008821nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008822{
8823 struct rpc_message msg = {
8824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8825 .rpc_argp = &data->args,
8826 .rpc_resp = &data->res,
8827 .rpc_cred = data->cred,
8828 };
8829 struct rpc_task_setup task_setup_data = {
8830 .task = &data->task,
8831 .rpc_client = NFS_CLIENT(data->args.inode),
8832 .rpc_message = &msg,
8833 .callback_ops = &nfs4_layoutcommit_ops,
8834 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008835 };
8836 struct rpc_task *task;
8837 int status = 0;
8838
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008839 dprintk("NFS: initiating layoutcommit call. sync %d "
8840 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008841 data->args.lastbytewritten,
8842 data->args.inode->i_ino);
8843
Trond Myklebust472e2592015-02-05 16:50:30 -05008844 if (!sync) {
8845 data->inode = nfs_igrab_and_active(data->args.inode);
8846 if (data->inode == NULL) {
8847 nfs4_layoutcommit_release(data);
8848 return -EAGAIN;
8849 }
8850 task_setup_data.flags = RPC_TASK_ASYNC;
8851 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008852 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008853 task = rpc_run_task(&task_setup_data);
8854 if (IS_ERR(task))
8855 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008856 if (sync)
8857 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008858 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008859 dprintk("%s: status %d\n", __func__, status);
8860 rpc_put_task(task);
8861 return status;
8862}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008863
Andy Adamson97431202013-08-08 10:57:56 -04008864/**
8865 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8866 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8867 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008868static int
8869_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008870 struct nfs_fsinfo *info,
8871 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008872{
8873 struct nfs41_secinfo_no_name_args args = {
8874 .style = SECINFO_STYLE_CURRENT_FH,
8875 };
8876 struct nfs4_secinfo_res res = {
8877 .flavors = flavors,
8878 };
8879 struct rpc_message msg = {
8880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8881 .rpc_argp = &args,
8882 .rpc_resp = &res,
8883 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008884 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008885 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008886 int status;
8887
8888 if (use_integrity) {
8889 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008890 cred = nfs4_get_clid_cred(server->nfs_client);
8891 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008892 }
8893
8894 dprintk("--> %s\n", __func__);
8895 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8896 &res.seq_res, 0);
8897 dprintk("<-- %s status=%d\n", __func__, status);
8898
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008899 if (cred)
8900 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008901
8902 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008903}
8904
8905static int
8906nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8907 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8908{
8909 struct nfs4_exception exception = { };
8910 int err;
8911 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008912 /* first try using integrity protection */
8913 err = -NFS4ERR_WRONGSEC;
8914
8915 /* try to use integrity protection with machine cred */
8916 if (_nfs4_is_integrity_protected(server->nfs_client))
8917 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8918 flavors, true);
8919
8920 /*
8921 * if unable to use integrity protection, or SECINFO with
8922 * integrity protection returns NFS4ERR_WRONGSEC (which is
8923 * disallowed by spec, but exists in deployed servers) use
8924 * the current filesystem's rpc_client and the user cred.
8925 */
8926 if (err == -NFS4ERR_WRONGSEC)
8927 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8928 flavors, false);
8929
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008930 switch (err) {
8931 case 0:
8932 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008933 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008934 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008935 default:
8936 err = nfs4_handle_exception(server, err, &exception);
8937 }
8938 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008939out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008940 return err;
8941}
8942
8943static int
8944nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8945 struct nfs_fsinfo *info)
8946{
8947 int err;
8948 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008949 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008950 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008951 struct nfs4_secinfo4 *secinfo;
8952 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008953
8954 page = alloc_page(GFP_KERNEL);
8955 if (!page) {
8956 err = -ENOMEM;
8957 goto out;
8958 }
8959
8960 flavors = page_address(page);
8961 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8962
8963 /*
8964 * Fall back on "guess and check" method if
8965 * the server doesn't support SECINFO_NO_NAME
8966 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008967 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008968 err = nfs4_find_root_sec(server, fhandle, info);
8969 goto out_freepage;
8970 }
8971 if (err)
8972 goto out_freepage;
8973
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008974 for (i = 0; i < flavors->num_flavors; i++) {
8975 secinfo = &flavors->flavors[i];
8976
8977 switch (secinfo->flavor) {
8978 case RPC_AUTH_NULL:
8979 case RPC_AUTH_UNIX:
8980 case RPC_AUTH_GSS:
8981 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8982 &secinfo->flavor_info);
8983 break;
8984 default:
8985 flavor = RPC_AUTH_MAXFLAVOR;
8986 break;
8987 }
8988
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008989 if (!nfs_auth_info_match(&server->auth_info, flavor))
8990 flavor = RPC_AUTH_MAXFLAVOR;
8991
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008992 if (flavor != RPC_AUTH_MAXFLAVOR) {
8993 err = nfs4_lookup_root_sec(server, fhandle,
8994 info, flavor);
8995 if (!err)
8996 break;
8997 }
8998 }
8999
9000 if (flavor == RPC_AUTH_MAXFLAVOR)
9001 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009002
9003out_freepage:
9004 put_page(page);
9005 if (err == -EACCES)
9006 return -EPERM;
9007out:
9008 return err;
9009}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009010
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009011static int _nfs41_test_stateid(struct nfs_server *server,
9012 nfs4_stateid *stateid,
9013 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009014{
9015 int status;
9016 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009017 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009018 };
9019 struct nfs41_test_stateid_res res;
9020 struct rpc_message msg = {
9021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9022 .rpc_argp = &args,
9023 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009024 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009025 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009026 struct rpc_clnt *rpc_client = server->client;
9027
9028 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9029 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009030
Chuck Lever38527b12012-07-11 16:30:23 -04009031 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009032 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009033 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009034 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009035 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009036 if (status != NFS_OK) {
9037 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009038 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009039 }
9040 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009041 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009042}
9043
Trond Myklebust43912bb2016-09-22 13:38:56 -04009044static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9045 int err, struct nfs4_exception *exception)
9046{
9047 exception->retry = 0;
9048 switch(err) {
9049 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009050 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009051 nfs4_handle_exception(server, err, exception);
9052 break;
9053 case -NFS4ERR_BADSESSION:
9054 case -NFS4ERR_BADSLOT:
9055 case -NFS4ERR_BAD_HIGH_SLOT:
9056 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9057 case -NFS4ERR_DEADSESSION:
9058 nfs4_do_handle_exception(server, err, exception);
9059 }
9060}
9061
Chuck Lever38527b12012-07-11 16:30:23 -04009062/**
9063 * nfs41_test_stateid - perform a TEST_STATEID operation
9064 *
9065 * @server: server / transport on which to perform the operation
9066 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009067 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009068 *
9069 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9070 * Otherwise a negative NFS4ERR value is returned if the operation
9071 * failed or the state ID is not currently valid.
9072 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009073static int nfs41_test_stateid(struct nfs_server *server,
9074 nfs4_stateid *stateid,
9075 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009076{
9077 struct nfs4_exception exception = { };
9078 int err;
9079 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009080 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009081 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009082 } while (exception.retry);
9083 return err;
9084}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009085
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009086struct nfs_free_stateid_data {
9087 struct nfs_server *server;
9088 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009089 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009090};
9091
9092static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9093{
9094 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009095 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009096 &data->args.seq_args,
9097 &data->res.seq_res,
9098 task);
9099}
9100
9101static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9102{
9103 struct nfs_free_stateid_data *data = calldata;
9104
9105 nfs41_sequence_done(task, &data->res.seq_res);
9106
9107 switch (task->tk_status) {
9108 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009109 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009110 rpc_restart_call_prepare(task);
9111 }
9112}
9113
9114static void nfs41_free_stateid_release(void *calldata)
9115{
9116 kfree(calldata);
9117}
9118
Trond Myklebust17f26b12013-08-21 15:48:42 -04009119static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009120 .rpc_call_prepare = nfs41_free_stateid_prepare,
9121 .rpc_call_done = nfs41_free_stateid_done,
9122 .rpc_release = nfs41_free_stateid_release,
9123};
9124
9125static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009126 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009127 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009128 bool privileged)
9129{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009130 struct rpc_message msg = {
9131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009132 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009133 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009134 struct rpc_task_setup task_setup = {
9135 .rpc_client = server->client,
9136 .rpc_message = &msg,
9137 .callback_ops = &nfs41_free_stateid_ops,
9138 .flags = RPC_TASK_ASYNC,
9139 };
9140 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009141
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009142 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9143 &task_setup.rpc_client, &msg);
9144
Chuck Lever38527b12012-07-11 16:30:23 -04009145 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009146 data = kmalloc(sizeof(*data), GFP_NOFS);
9147 if (!data)
9148 return ERR_PTR(-ENOMEM);
9149 data->server = server;
9150 nfs4_stateid_copy(&data->args.stateid, stateid);
9151
9152 task_setup.callback_data = data;
9153
9154 msg.rpc_argp = &data->args;
9155 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009156 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009157 if (privileged)
9158 nfs4_set_sequence_privileged(&data->args.seq_args);
9159
9160 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009161}
9162
Chuck Lever38527b12012-07-11 16:30:23 -04009163/**
9164 * nfs41_free_stateid - perform a FREE_STATEID operation
9165 *
9166 * @server: server / transport on which to perform the operation
9167 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009168 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009169 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009170 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009171 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009172 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009173static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009174 const nfs4_stateid *stateid,
9175 struct rpc_cred *cred,
9176 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009177{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009178 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009179
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009180 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009181 if (IS_ERR(task))
9182 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009183 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009184 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009185}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009186
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009187static void
9188nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009189{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009190 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009191
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009192 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009193 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009194}
9195
Trond Myklebust36281ca2012-03-04 18:13:56 -05009196static bool nfs41_match_stateid(const nfs4_stateid *s1,
9197 const nfs4_stateid *s2)
9198{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009199 if (s1->type != s2->type)
9200 return false;
9201
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009202 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009203 return false;
9204
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009205 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009206 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009207 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009208 return true;
9209
9210 return false;
9211}
9212
Andy Adamson557134a2009-04-01 09:21:53 -04009213#endif /* CONFIG_NFS_V4_1 */
9214
Trond Myklebust36281ca2012-03-04 18:13:56 -05009215static bool nfs4_match_stateid(const nfs4_stateid *s1,
9216 const nfs4_stateid *s2)
9217{
Trond Myklebustf597c532012-03-04 18:13:56 -05009218 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009219}
9220
9221
Trond Myklebust17280172012-03-11 13:11:00 -04009222static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009223 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009224 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225 .recover_open = nfs4_open_reclaim,
9226 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009227 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009228 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229};
9230
Andy Adamson591d71c2009-04-01 09:22:47 -04009231#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009232static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009233 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9234 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9235 .recover_open = nfs4_open_reclaim,
9236 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009237 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009238 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009239 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009240};
9241#endif /* CONFIG_NFS_V4_1 */
9242
Trond Myklebust17280172012-03-11 13:11:00 -04009243static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009244 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009245 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009246 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009248 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249};
9250
Andy Adamson591d71c2009-04-01 09:22:47 -04009251#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009252static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009253 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9254 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009255 .recover_open = nfs41_open_expired,
9256 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009257 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009258};
9259#endif /* CONFIG_NFS_V4_1 */
9260
Trond Myklebust17280172012-03-11 13:11:00 -04009261static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009262 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009263 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009264 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009265};
9266
9267#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009268static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009269 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009270 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009271 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009272};
9273#endif
9274
Chuck Leverec011fe2013-10-17 14:12:39 -04009275static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009276 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009277 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009278};
9279
9280#if defined(CONFIG_NFS_V4_1)
9281static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009282 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009283 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009284};
9285#endif /* CONFIG_NFS_V4_1 */
9286
Trond Myklebust97dc1352010-06-16 09:52:26 -04009287static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9288 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009289 .init_caps = NFS_CAP_READDIRPLUS
9290 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009291 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009292 .init_client = nfs40_init_client,
9293 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009294 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009295 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009296 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009297 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009298 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009299 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009300 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9301 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9302 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009303 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009304};
9305
9306#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009307static struct nfs_seqid *
9308nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9309{
9310 return NULL;
9311}
9312
Trond Myklebust97dc1352010-06-16 09:52:26 -04009313static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9314 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009315 .init_caps = NFS_CAP_READDIRPLUS
9316 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009317 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009318 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009319 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009320 .init_client = nfs41_init_client,
9321 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009322 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009323 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009324 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009325 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009326 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009327 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009328 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009329 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9330 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9331 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009332 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009333};
9334#endif
9335
Steve Dickson42c2c422013-05-22 12:50:38 -04009336#if defined(CONFIG_NFS_V4_2)
9337static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9338 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009339 .init_caps = NFS_CAP_READDIRPLUS
9340 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009341 | NFS_CAP_POSIX_LOCK
9342 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009343 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009344 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009345 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009346 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009347 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009348 | NFS_CAP_LAYOUTSTATS
9349 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009350 .init_client = nfs41_init_client,
9351 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009352 .match_stateid = nfs41_match_stateid,
9353 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009354 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009355 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009356 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009357 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009358 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009359 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9360 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9361 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009362 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009363};
9364#endif
9365
Trond Myklebust97dc1352010-06-16 09:52:26 -04009366const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9367 [0] = &nfs_v4_0_minor_ops,
9368#if defined(CONFIG_NFS_V4_1)
9369 [1] = &nfs_v4_1_minor_ops,
9370#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009371#if defined(CONFIG_NFS_V4_2)
9372 [2] = &nfs_v4_2_minor_ops,
9373#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009374};
9375
Trond Myklebust13997822016-07-24 17:10:52 -04009376static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009377{
9378 ssize_t error, error2;
9379
9380 error = generic_listxattr(dentry, list, size);
9381 if (error < 0)
9382 return error;
9383 if (list) {
9384 list += error;
9385 size -= error;
9386 }
9387
9388 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9389 if (error2 < 0)
9390 return error2;
9391 return error + error2;
9392}
9393
Trond Myklebust17f26b12013-08-21 15:48:42 -04009394static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009395 .create = nfs_create,
9396 .lookup = nfs_lookup,
9397 .atomic_open = nfs_atomic_open,
9398 .link = nfs_link,
9399 .unlink = nfs_unlink,
9400 .symlink = nfs_symlink,
9401 .mkdir = nfs_mkdir,
9402 .rmdir = nfs_rmdir,
9403 .mknod = nfs_mknod,
9404 .rename = nfs_rename,
9405 .permission = nfs_permission,
9406 .getattr = nfs_getattr,
9407 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009408 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009409};
9410
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009411static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009412 .permission = nfs_permission,
9413 .getattr = nfs_getattr,
9414 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009415 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009416};
9417
David Howells509de812006-08-22 20:06:11 -04009418const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419 .version = 4, /* protocol version */
9420 .dentry_ops = &nfs4_dentry_operations,
9421 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009422 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009423 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009425 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009426 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009427 .getattr = nfs4_proc_getattr,
9428 .setattr = nfs4_proc_setattr,
9429 .lookup = nfs4_proc_lookup,
9430 .access = nfs4_proc_access,
9431 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432 .create = nfs4_proc_create,
9433 .remove = nfs4_proc_remove,
9434 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009435 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009437 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009438 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009439 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 .link = nfs4_proc_link,
9441 .symlink = nfs4_proc_symlink,
9442 .mkdir = nfs4_proc_mkdir,
9443 .rmdir = nfs4_proc_remove,
9444 .readdir = nfs4_proc_readdir,
9445 .mknod = nfs4_proc_mknod,
9446 .statfs = nfs4_proc_statfs,
9447 .fsinfo = nfs4_proc_fsinfo,
9448 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009449 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009451 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009453 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009455 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009457 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009458 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009460 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009461 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009462 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009463 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009464 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009465 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009466 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009467 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009468 .create_server = nfs4_create_server,
9469 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470};
9471
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009472static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009473 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009474 .list = nfs4_xattr_list_nfs4_acl,
9475 .get = nfs4_xattr_get_nfs4_acl,
9476 .set = nfs4_xattr_set_nfs4_acl,
9477};
9478
9479const struct xattr_handler *nfs4_xattr_handlers[] = {
9480 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009481#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9482 &nfs4_xattr_nfs4_label_handler,
9483#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009484 NULL
9485};
9486
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487/*
9488 * Local variables:
9489 * c-basic-offset: 8
9490 * End:
9491 */